简体   繁体   English

PayPal IPN发送电子邮件

[英]PayPal IPN Send Email

I have a controller that handles PayPal's IPN callback. 我有一个处理PayPal IPN回调的控制器。 I want to mark an attendee as 'paid' and send them a confirmation email if they've successfully paid. 我想将与会者标记为“已付款”,如果他们已成功付款,则向他们发送确认电子邮件。

The mark paid action is working but the email is not sending. 标记付款操作正在工作,但电子邮件未发送。

Here's my controller: 这是我的控制器:

    class PaymentNotificationsController < ApplicationController
      protect_from_forgery :except => [:create]

      def create
        PaymentNotification.create!(:params => params, :attendee_id => params[:invoice],  :status => params[:payment_status], :transaction_id => params[:txn_id])
        if params[:payment_status] == 'Complete'
          @attendee = Attendee.find(params[:invoice])
          ## Working
          @attendee.update_attribute(:paid, Time.now)
          ## Not Working
          UserMailer.welcome_email(@attendee).deliver
        end
        render nothing: true
      end
    end

Here's my user_mailer file: 这是我的user_mailer文件:

    class UserMailer < ActionMailer::Base
      default from: 'example@email.com'

      def welcome_email(user)
        @user = user
        email_with_name = "#{@user.first_name} #{@user.last_name} <#{@user.email}>"
        @url  = 'http://example.com'
        mail(
          to: email_with_name,
          subject: 'Welcome to Yadda Yadda'
        )
      end
    end

Here's the weird thing, in another controller that doesn't have PayPal the mailer works: 这很奇怪,在另一个没有PayPal的控制器中,邮件程序正常工作:

    class VendorsController < ApplicationController
      def create
        @vendor = Vendor.new(vendor_params)
        if @vendor.save
          UserMailer.welcome_email(@vendor).deliver
          redirect_to vendor_success_path
        else
          render 'new'
        end
       end
     end

I am pulling your answer out of your question and posting it here for future reference. 我正在从您的问题中拉出您的答案,并将其发布在此处以供将来参考。

This takes two actions (mark paid and send mail). 这将执行两个操作(标记已付款并发送邮件)。 It has been moved to the model as an after_create method. 它已作为after_create方法移至模型。

Here's the model: 这是模型:

    class PaymentNotification < ActiveRecord::Base
      ...
      after_create :mark_attendee_paid

      private

      def mark_attendee_paid
        if status == 'Completed'
          attendee.update_attribute(:paid, Time.now)
          UserMailer.welcome_email(attendee).deliver
        end
      end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM