Class: ActiveMerchant::Billing::Integrations::Paypal::Notification

Inherits:
Notification
  • Object
show all
Includes:
PostsData
Defined in:
lib/active_merchant/billing/integrations/paypal/notification.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Notification

#params, #raw

Instance Method Summary collapse

Methods included from PostsData

included, #ssl_get, #ssl_post

Methods inherited from Notification

#amount, #empty!, #gross_cents, #initialize, #valid_sender?

Constructor Details

This class inherits a constructor from ActiveMerchant::Billing::Integrations::Notification

Instance Method Details

#accountObject



120
121
122
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 120

def 
  params['business'] || params['receiver_email']
end

#acknowledgeObject

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

Raises:

  • (StandardError)


138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 138

def acknowledge
  payload =  raw

  response = ssl_post(Paypal.service_url + '?cmd=_notify-validate', payload, 
    'Content-Length' => "#{payload.size}",
    'User-Agent'     => "Active Merchant -- http://activemerchant.org"
  )
  
  raise StandardError.new("Faulty paypal result: #{response}") unless ["VERIFIED", "INVALID"].include?(response)

  response == "VERIFIED"
end

#complete?Boolean

Was the transaction complete?

Returns:

  • (Boolean)


48
49
50
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 48

def complete?
  status == "Completed"
end

#currencyObject

What currency have we been dealing with



99
100
101
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 99

def currency
  params['mc_currency']
end

#feeObject

the markup paypal charges for the transaction



94
95
96
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 94

def fee
  params['mc_fee']
end

#grossObject

the money amount we received in X.2 decimal.



89
90
91
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 89

def gross
  params['mc_gross']
end

#invoiceObject

This is the invoice which you passed to paypal



111
112
113
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 111

def invoice
  params['invoice']
end

#item_idObject

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



106
107
108
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 106

def item_id
  params['item_number'] || params['custom']
end

#received_atObject

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



56
57
58
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 56

def received_at
  Time.parse params['payment_date']
end

#statusObject

Status of transaction. List of possible values:

Canceled-Reversal
Completed
Denied
Expired
Failed
In-Progress
Partially-Refunded
Pending
Processed
Refunded
Reversed
Voided


73
74
75
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 73

def status
  params['payment_status']
end

#test?Boolean

Was this a test transaction?

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 116

def test?
  params['test_ipn'] == '1'
end

#transaction_idObject

Id of this transaction (paypal number)



78
79
80
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 78

def transaction_id
  params['txn_id']
end

#typeObject

What type of transaction are we dealing with?

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


84
85
86
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 84

def type
  params['txn_type']
end