Class: Paypal::Notification
- Inherits:
-
Object
- Object
- Paypal::Notification
- Defined in:
- lib/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
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
Constant Summary collapse
- @@ipn_url =
'http://www.sandbox.paypal.com/cgi-bin/webscr'
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
-
#acknowledge ⇒ Object
Acknowledge the transaction to paypal.
-
#amount ⇒ Object
This combines the gross and currency and returns a proper Money object.
-
#complete? ⇒ Boolean
Was the transaction complete?.
-
#currency ⇒ Object
What currency have we been dealing with.
-
#empty! ⇒ Object
reset the notification.
-
#fee ⇒ Object
the markup paypal charges for the transaction.
-
#gross ⇒ Object
the money amount we received in X.2 decimal.
-
#initialize(post) ⇒ Notification
constructor
Creates a new paypal object.
-
#invoice ⇒ Object
This is the custom field which you passed to paypal.
-
#item_id ⇒ Object
This is the item number which we submitted to paypal.
-
#received_at ⇒ Object
When was this payment received by the client.
-
#status ⇒ Object
Whats the status of this 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.
Constructor Details
#initialize(post) ⇒ Notification
Creates a new paypal object. Pass the raw html you got from paypal in. In a rails application this looks something like this
def paypal_ipn
paypal = Paypal::Notification.new(request.raw_post)
...
end
60 61 62 63 |
# File 'lib/notification.rb', line 60 def initialize(post) empty! parse(post) end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
39 40 41 |
# File 'lib/notification.rb', line 39 def params @params end |
#raw ⇒ Object
Returns the value of attribute raw.
40 41 42 |
# File 'lib/notification.rb', line 40 def raw @raw end |
Instance Method Details
#acknowledge ⇒ 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
151 152 153 154 155 156 157 158 |
# File 'lib/notification.rb', line 151 def acknowledge uri = URI.parse(self.class.ipn_url) status = nil Net::HTTP.start(uri.host, uri.port) do |request| status = request.post(uri.path, raw + "&cmd=_notify-validate").body end status == "VERIFIED" end |
#amount ⇒ Object
This combines the gross and currency and returns a proper Money object. this requires the money library located at dist.leetsoft.com/api/money
126 127 128 129 |
# File 'lib/notification.rb', line 126 def amount amount = gross.sub(/[^\d]/, '').to_i Money.new(amount, currency) end |
#complete? ⇒ Boolean
Was the transaction complete?
66 67 68 |
# File 'lib/notification.rb', line 66 def complete? status == "Completed" end |
#currency ⇒ Object
What currency have we been dealing with
105 106 107 |
# File 'lib/notification.rb', line 105 def currency params['mc_currency'] end |
#empty! ⇒ Object
reset the notification.
132 133 134 135 |
# File 'lib/notification.rb', line 132 def empty! @params = Hash.new @raw = "" end |
#fee ⇒ Object
the markup paypal charges for the transaction
100 101 102 |
# File 'lib/notification.rb', line 100 def fee params['mc_fee'] end |
#gross ⇒ Object
the money amount we received in X.2 decimal.
95 96 97 |
# File 'lib/notification.rb', line 95 def gross params['mc_gross'] end |
#invoice ⇒ Object
This is the custom field which you passed to paypal
115 116 117 |
# File 'lib/notification.rb', line 115 def invoice params['invoice'] end |
#item_id ⇒ Object
This is the item number which we submitted to paypal
110 111 112 |
# File 'lib/notification.rb', line 110 def item_id params['item_number'] 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
74 75 76 |
# File 'lib/notification.rb', line 74 def received_at Time.parse params['payment_date'] end |
#status ⇒ Object
Whats the status of this transaction?
79 80 81 |
# File 'lib/notification.rb', line 79 def status params['payment_status'] end |
#transaction_id ⇒ Object
Id of this transaction (paypal number)
84 85 86 |
# File 'lib/notification.rb', line 84 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.
90 91 92 |
# File 'lib/notification.rb', line 90 def type params['txn_type'] end |