class OffsitePayments::Integrations::Paypal::Notification
end
end
render :nothing
end
end
order.save
ensure
raise
order.status = ‘failed’
rescue => e
end
logger.error(“Failed to verify Paypal’s notification, please investigate”)
else
shop.ship(order)
order.status = ‘success’
if notify.complete? and order.total == notify.amount
begin
if notify.acknowledge
order = Order.find(notify.item_id)
end
masspay_items = notify.items
if notify.masspay?
notify = Paypal::Notification.new(request.raw_post)
def paypal_ipn
include OffsitePayments::Integrations
class BackendController < ApplicationController
Example
on creating a safe payment controller.
is an example, please read the Paypal API documentation for all the details
The Example shows a typical handler in a rails application. Note that this
Parser and handler for incoming Instant payment notifications from paypal.
def account
def account params['business'] || params['receiver_email'] end
def acknowledge(authcode = nil)
... log possible hacking attempt ...
else
... process order ... if notify.complete?
if notify.acknowledge
notify = PaypalNotification.new(request.raw_post)
def paypal_ipn
Example:
ok or a fail.
ipn arrives. Paypal will verify that all the information we received are correct and will return a
Acknowledge the transaction to paypal. This method has to be called after a new
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
def complete?
def complete? status == "Completed" end
def currency
def currency params['mc_currency'] end
def fee
def fee params['mc_fee'] end
def gross
def gross params['mc_gross'] end
def initialize(post, options = {})
def initialize(post, options = {}) super extend MassPayNotification if masspay? end
def invoice
def invoice params['invoice'] end
def item_id
The custom field is also mapped to item_id because PayPal
This is the item number which we submitted to paypal
def item_id params['item_number'] || params['custom'] end
def masspay?
def masspay? type == "masspay" end
def received_at
One possible scenario is that our web application was down. In this case paypal tries several
sometimes it can happen that we get the notification much later.
When was this payment received by the client.
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
def status
Reversed::
Refunded::
Processed::
Pending::
Partially-Refunded::
In-Progress::
Failed::
Expired::
Denied::
Completed::
Canceled-Reversal::
Status of transaction. List of possible values:
def status params['payment_status'] end
def test?
def test? params['test_ipn'] == '1' end
def transaction_id
def transaction_id params['txn_id'] end
def type
What type of transaction are we dealing with?
def type params['txn_type'] end