Hunter
Hunter

Reputation: 1687

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: '[email protected]'

      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

Upvotes: 0

Views: 188

Answers (1)

Andy
Andy

Reputation: 50630

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

Upvotes: 1

Related Questions