Class: ActiveMerchant::Billing::Integrations::Paypal::Notification
- Inherits:
-
Notification
- Object
- Notification
- ActiveMerchant::Billing::Integrations::Paypal::Notification
- 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)
if notify.masspay?
masspay_items = notify.items
end
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
Instance Method Summary collapse
- #account ⇒ Object
-
#acknowledge(authcode = nil) ⇒ Object
Acknowledge the transaction to paypal.
-
#complete? ⇒ Boolean
Was the transaction complete?.
-
#currency ⇒ Object
What currency have we been dealing with.
-
#fee ⇒ Object
the markup paypal charges for the transaction.
-
#gross ⇒ Object
the money amount we received in X.2 decimal.
-
#initialize(post, options = {}) ⇒ Notification
constructor
A new instance of Notification.
-
#invoice ⇒ Object
This is the invoice which you passed to paypal.
-
#item_id ⇒ Object
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.
-
#masspay? ⇒ Boolean
Is it a masspay notification?.
-
#received_at ⇒ Object
When was this payment received by the client.
-
#status ⇒ Object
Status of transaction.
-
#test? ⇒ Boolean
Was this a test transaction?.
-
#transaction_id ⇒ Object
Id of this transaction (paypal number).
-
#type ⇒ Object
What type of transaction are we dealing with? “cart” “send_money” “web_accept” are possible here.
Methods inherited from Notification
#amount, #empty!, #gross_cents, #valid_sender?
Constructor Details
#initialize(post, options = {}) ⇒ Notification
Returns a new instance of Notification.
52 53 54 55 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 52 def initialize(post, = {}) super extend MassPayNotification if masspay? end |
Instance Method Details
#account ⇒ Object
143 144 145 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 143 def account params['business'] || params['receiver_email'] end |
#acknowledge(authcode = nil) ⇒ Object
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
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 161 def acknowledge(authcode = nil) 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?
58 59 60 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 58 def complete? status == "Completed" end |
#currency ⇒ Object
What currency have we been dealing with
122 123 124 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 122 def currency params['mc_currency'] end |
#fee ⇒ Object
the markup paypal charges for the transaction
117 118 119 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 117 def fee params['mc_fee'] end |
#gross ⇒ Object
the money amount we received in X.2 decimal.
112 113 114 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 112 def gross params['mc_gross'] end |
#invoice ⇒ Object
This is the invoice which you passed to paypal
134 135 136 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 134 def invoice params['invoice'] end |
#item_id ⇒ Object
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
129 130 131 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 129 def item_id params['item_number'] || params['custom'] end |
#masspay? ⇒ Boolean
Is it a masspay notification?
63 64 65 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 63 def masspay? type == "masspay" end |
#received_at ⇒ Object
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
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 71 def received_at parsed_time_fields = DateTime._strptime(params['payment_date'], "%H:%M:%S %b %d, %Y %Z") Time.gm( parsed_time_fields[:year], parsed_time_fields[:mon], parsed_time_fields[:mday], parsed_time_fields[:hour], parsed_time_fields[:min], parsed_time_fields[:sec] ) + Time.zone_offset(parsed_time_fields[:zone]) end |
#status ⇒ Object
Status of transaction. List of possible values:
Canceled-Reversal
Completed
Denied
Expired
Failed
In-Progress
Partially-Refunded
Pending
Processed
Refunded
Reversed
Voided
96 97 98 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 96 def status params['payment_status'] end |
#test? ⇒ Boolean
Was this a test transaction?
139 140 141 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 139 def test? params['test_ipn'] == '1' end |
#transaction_id ⇒ Object
Id of this transaction (paypal number)
101 102 103 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 101 def transaction_id params['txn_id'] end |
#type ⇒ Object
What type of transaction are we dealing with?
"cart" "send_money" "web_accept" are possible here.
107 108 109 |
# File 'lib/active_merchant/billing/integrations/paypal/notification.rb', line 107 def type params['txn_type'] end |