PayPal IPN发送电子邮件

时间:2013-11-27 19:00:09

标签: ruby-on-rails email paypal paypal-ipn

我有一个处理PayPal的IPN回调的控制器。我想将与会者标记为“付费”,并在他们成功付款时向他们发送确认电子邮件。

支付标记的操作正在运行,但电子邮件未发送。

这是我的控制器:

    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

这是我的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

这是奇怪的事情,在另一个没有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

1 个答案:

答案 0 :(得分:1)

我从您的问题中提取您的答案并将其发布在此处以供将来参考。

这需要两个动作(标记付费和发送邮件)。它已作为after_create方法移动到模型中。

以下是模型:

    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