Class ActiveMerchant::Billing::Integrations::Paypal::Notification
In: lib/active_merchant/billing/integrations/paypal/notification.rb
Parent: ActiveMerchant::Billing::Integrations::Notification

Parser and handler for incoming Instant payment notifications from paypal. The Example shows a typical handler in a rails application. Note that this is an example, please read the Paypal API documentation for all the details on creating a safe payment controller.

Example

  class BackendController < ApplicationController
    include ActiveMerchant::Billing::Integrations

    def paypal_ipn
      notify = Paypal::Notification.new(request.raw_post)

      order = Order.find(notify.item_id)

      if notify.acknowledge
        begin

          if notify.complete? and order.total == notify.amount
            order.status = 'success'

            shop.ship(order)
          else
            logger.error("Failed to verify Paypal's notification, please investigate")
          end

        rescue => e
          order.status        = 'failed'
          raise
        ensure
          order.save
        end
      end

      render :nothing
    end
  end

Methods

account   acknowledge   complete?   currency   fee   gross   invoice   item_id   received_at   status   test?   transaction_id   type  

Included Modules

PostsData

Public Instance methods

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 120
120:           def account
121:             params['business'] || params['receiver_email']
122:           end

Acknowledge the transaction to paypal. This method has to be called after a new ipn arrives. Paypal will verify that all the information we received are correct and will return a ok or a fail.

Example:

  def paypal_ipn
    notify = PaypalNotification.new(request.raw_post)

    if notify.acknowledge
      ... process order ... if notify.complete?
    else
      ... log possible hacking attempt ...
    end

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 138
138:           def acknowledge
139:             payload =  raw
140: 
141:             response = ssl_post(Paypal.service_url + '?cmd=_notify-validate', payload, 
142:               'Content-Length' => "#{payload.size}",
143:               'User-Agent'     => "Active Merchant -- http://activemerchant.org"
144:             )
145:             
146:             raise StandardError.new("Faulty paypal result: #{response}") unless ["VERIFIED", "INVALID"].include?(response)
147: 
148:             response == "VERIFIED"
149:           end

Was the transaction complete?

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 48
48:           def complete?
49:             status == "Completed"
50:           end

What currency have we been dealing with

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 99
 99:           def currency
100:             params['mc_currency']
101:           end

the markup paypal charges for the transaction

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 94
94:           def fee
95:             params['mc_fee']
96:           end

the money amount we received in X.2 decimal.

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 89
89:           def gross
90:             params['mc_gross']
91:           end

This is the invoice which you passed to paypal

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 111
111:           def invoice
112:             params['invoice']
113:           end

This is the item number which we submitted to paypal The custom field is also mapped to item_id because PayPal doesn‘t return item_number in dispute notifications

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 106
106:           def item_id
107:             params['item_number'] || params['custom']
108:           end

When was this payment received by the client. sometimes it can happen that we get the notification much later. One possible scenario is that our web application was down. In this case paypal tries several times an hour to inform us about the notification

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 56
56:           def received_at
57:             Time.parse params['payment_date']
58:           end

Status of transaction. List of possible values: Canceled-Reversal:: Completed:: Denied:: Expired:: Failed:: In-Progress:: Partially-Refunded:: Pending:: Processed:: Refunded:: Reversed:: Voided::

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 73
73:           def status
74:             params['payment_status']
75:           end

Was this a test transaction?

[Source]

     # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 116
116:           def test?
117:             params['test_ipn'] == '1'
118:           end

Id of this transaction (paypal number)

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 78
78:           def transaction_id
79:             params['txn_id']
80:           end

What type of transaction are we dealing with?

 "cart" "send_money" "web_accept" are possible here.

[Source]

    # File lib/active_merchant/billing/integrations/paypal/notification.rb, line 84
84:           def type
85:             params['txn_type']
86:           end

[Validate]