Last active
August 25, 2022 19:44
-
-
Save alexanmtz/b909dcc332697c74f8822bb03228cef5 to your computer and use it in GitHub Desktop.
Erro ao chamar send_email: *** ActiveRecord::RecordInvalid Exception: A validação falhou: User é obrigatório(a)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Payment::PostProcessing::CreditCard | |
def initialize(transaction) | |
@transaction = transaction | |
end | |
def after_payment(order) | |
@order = order | |
register_amount_paid | |
@order.pay! | |
save_credit_card | |
send_email | |
end | |
private | |
def register_amount_paid | |
@order.update_column('paid_centavos', @transaction.paid_amount) | |
end | |
def save_credit_card | |
card = {} | |
card['brand'] = @transaction.card.brand | |
card['first_digits'] = @transaction.card.first_digits | |
card['last_digits'] = @transaction.card.last_digits | |
card['holder_name'] = @transaction.card.holder_name | |
card['country'] = @transaction.card.country | |
@order.card_info = card | |
@order.save! | |
end | |
def send_email | |
letter = Email::Letter.create!( | |
context: 'order_completed', | |
object: @order, | |
user_email: @order.shipping_email, | |
email_template_id: Email::Template.find_by!(name: 'order_completed').id | |
) | |
begin | |
letter.send_email | |
rescue SparkPostRails::DeliveryException => e | |
"We can't send you email because #{e.service_description}" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Email::Letter < ApplicationRecord | |
belongs_to :object, polymorphic: true, inverse_of: :email_letters | |
belongs_to :user | |
belongs_to :email_template, class_name: 'Email::Template' | |
validates :user_email, presence: true, unless: :user_id? | |
validates :context, presence: true, inclusion: { in: %w(admin_updated_store admin_deleted_store user_signed_up | |
user_created_store user_created_campaign product_was_sold order_completed order_canceled first_sale boleto_paid boleto_waiting | |
payment_request_user payment_request_admin payment_request_confirmed order_completed_vendor) } | |
validates :status, presence: true, inclusion: { in: %w(init sending sent has_error) } | |
validates :subject, presence: true, length: { minimum: 5, maximum: 60 }, unless: :email_template_id? | |
validates :body, presence: true, length: { minimum: 30 }, unless: :email_template_id? | |
include AASM | |
aasm column: :status do | |
state :init, initial: true | |
state :sending | |
state :sent | |
state :has_error | |
event :start_sending do | |
transitions from: :init, to: :sending | |
end | |
event :sending_was_successful do | |
transitions from: :sending, to: :sent | |
end | |
event :sending_was_unsuccessful do | |
transitions from: :sending, to: :has_error | |
end | |
end | |
def send_email | |
self.start_sending! | |
response = NotifyUserMailer.letter(self).deliver! | |
if response['total_accepted_recipients'] >= 1 | |
self.update_attribute(:sparkpost_id, response['id']) | |
self.sending_was_successful! | |
else | |
self.sending_was_unsuccessful! | |
end | |
end | |
def replace_substitutions(text) | |
Rails.application.routes.default_url_options[:host] = ENV['SITE_HOST'] | |
case context | |
when 'admin_updated_store', 'admin_deleted_store', 'user_created_store' | |
text.gsub!('%store.title%', object.title) | |
text.gsub!('%store.name%', object.title) | |
text.gsub!('%store.url%', Rails.application.routes.url_helpers.store_url(object.slug)) | |
text.gsub!('%store.dashboard_url%', Rails.application.routes.url_helpers.edit_dashboard_store_url(uuid: object.uuid)) | |
when 'user_created_campaign' | |
text.gsub!('%campaign.title%', object.title) | |
text.gsub!('%campaign.name%', object.title) | |
text.gsub!('%campaign.url%', Rails.application.routes.url_helpers.campaign_url(id: object.slug)) | |
text.gsub!('%campaign.dashboard_url%', Rails.application.routes.url_helpers.edit_dashboard_campaign_url(uuid: object.uuid)) | |
when 'order_completed' | |
text.gsub!('%order.list_of_products%', '<ul>' + object.order_products.map { |p| "<li>(#{p.quantity}x) #{p.product.campaign.title}</li>" }.join + '</ul>') | |
text.gsub!('%order.subtotal%', ActionController::Base.helpers.humanized_money_with_symbol(object.common_price)) | |
text.gsub!('%order.shipping_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.shipping_price)) | |
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.total_price)) | |
when 'boleto_waiting' | |
text.gsub!('%order.boleto_url%', object.boleto_url) | |
text.gsub!('%order.list_of_products%', '<ul>' + object.order_products.map { |p| "<li>(#{p.quantity}x) #{p.product.campaign.title}</li>" }.join + '</ul>') | |
text.gsub!('%order.subtotal%', ActionController::Base.helpers.humanized_money_with_symbol(object.common_price)) | |
text.gsub!('%order.shipping_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.shipping_price)) | |
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.total_price)) | |
when 'payment_request_user' | |
text.gsub!('%request.amount%', ActionController::Base.helpers.humanized_money_with_symbol((object.amount_centavos)/100.0)) | |
when 'payment_request_admin' | |
text.gsub!('%request.amount%', ActionController::Base.helpers.humanized_money_with_symbol(object.amount_centavos)) | |
when 'order_completed_vendor' | |
order_products = object.order_products.joins(product: :campaign).where(campaigns: { user_id: user.id }) | |
text.gsub!('%order.list_of_products%', '<ul>' + order_products.map { |p| "<li>#{p.product.campaign.title}</li>" }.join + '</ul>') | |
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(order_products.sum(:price_centavos).to_f/100)) | |
end | |
if context != 'order_completed' && | |
context != 'boleto_waiting' && | |
context != 'boleto_paid' && | |
context != 'payment_request_user' && | |
context != 'payment_request_admin' && | |
context != 'payment_request_confirmed' | |
text.gsub!('%user.name%', user.name) | |
text.gsub!('%user.full_name%', user.name) | |
text.gsub!('%user.first_name%', user.first_name.to_s) | |
text.gsub!('%user.last_name%', user.last_name.to_s) | |
text.gsub!('%user.email%', user.email) | |
text.gsub!('%user.full_email%', user.full_email) | |
end | |
text | |
end | |
end | |
# admin_updated_store store store.user | |
# admin_deleted_store store store.user | |
# user_signed_up user user | |
# user_created_store store store.user | |
# user_created_campaign campaign campaign.user | |
# product_was_sold product product.user | |
# order_completed order email | |
# order_canceled order email | |
# first_sale product product.user |
Mesmo problema aqui
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemplo (o erro acontece mesmo salvando um user_id):