简体   繁体   中英

PayPal IPN Send Email

I have a controller that handles PayPal's IPN callback. 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:

    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:

    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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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