Class: ActiveMerchant::Billing::Integrations::PayFast::Notification
- Inherits:
-
Notification
- Object
- Notification
- ActiveMerchant::Billing::Integrations::PayFast::Notification
- Includes:
- Common, PostsData
- Defined in:
- lib/active_merchant/billing/integrations/pay_fast/notification.rb
Overview
Parser and handler for incoming ITN from PayFast. The Example shows a typical handler in a rails application.
Example
class BackendController < ApplicationController
include ActiveMerchant::Billing::Integrations
def pay_fast_itn
notify = PayFast::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
Instance Method Summary collapse
-
#acknowledge ⇒ Object
Acknowledge the transaction to PayFast.
-
#amount ⇒ Object
The net amount credited to the receiver’s account.
-
#complete? ⇒ Boolean
Was the transaction complete?.
- #currency ⇒ Object
-
#empty! ⇒ Object
Generated hash depends on params order so use OrderedHash instead of Hash.
-
#fee ⇒ Object
The total in fees which was deducated from the amount.
-
#gross ⇒ Object
The total amount which the payer paid.
-
#item_id ⇒ Object
Id of this transaction (uniq Shopify transaction id).
-
#item_name ⇒ Object
The name of the item being charged for.
-
#merchant_id ⇒ Object
The Merchant ID as given by the PayFast system.
-
#status ⇒ Object
Status of transaction.
-
#transaction_id ⇒ Object
Id of this transaction (uniq PayFast transaction id).
Methods included from Common
#generate_signature, #notify_signature_string, #request_attributes, #request_signature_string
Methods inherited from Notification
#gross_cents, #initialize, #test?, #valid_sender?
Constructor Details
This class inherits a constructor from ActiveMerchant::Billing::Integrations::Notification
Instance Method Details
#acknowledge ⇒ Object
Acknowledge the transaction to PayFast. This method has to be called after a new ITN arrives. PayFast will verify that all the information we received are correct and will return a VERIFIED or INVALID status.
Example:
def pay_fast_itn
notify = PayFastNotification.new(request.raw_post)
if notify.acknowledge
... process order ... if notify.complete?
else
... log possible hacking attempt ...
end
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 119 def acknowledge if params[PayFast.signature_parameter_name] == generate_signature(:notify) response = ssl_post(PayFast.validate_service_url, notify_signature_string, 'Content-Type' => "application/x-www-form-urlencoded", 'Content-Length' => "#{notify_signature_string.size}" ) raise StandardError.new("Faulty PayFast result: #{response}") unless ['VALID', 'INVALID'].include?(response) response == "VALID" end end |
#amount ⇒ Object
The net amount credited to the receiver’s account.
82 83 84 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 82 def amount params['amount_net'] end |
#complete? ⇒ Boolean
Was the transaction complete?
47 48 49 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 47 def complete? status == "Completed" end |
#currency ⇒ Object
96 97 98 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 96 def currency nil end |
#empty! ⇒ Object
Generated hash depends on params order so use OrderedHash instead of Hash
100 101 102 103 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 100 def empty! super @params = ActiveSupport::OrderedHash.new end |
#fee ⇒ Object
The total in fees which was deducated from the amount.
77 78 79 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 77 def fee params['amount_fee'] end |
#gross ⇒ Object
The total amount which the payer paid.
72 73 74 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 72 def gross params['amount_gross'] end |
#item_id ⇒ Object
Id of this transaction (uniq Shopify transaction id)
67 68 69 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 67 def item_id params['m_payment_id'] end |
#item_name ⇒ Object
The name of the item being charged for.
87 88 89 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 87 def item_name params['item_name'] end |
#merchant_id ⇒ Object
The Merchant ID as given by the PayFast system. Used to uniquely identify the receiver’s account.
92 93 94 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 92 def merchant_id params['merchant_id'] end |
#status ⇒ Object
Status of transaction. List of possible values:
COMPLETE
53 54 55 56 57 58 59 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 53 def status if params['payment_status'] == "COMPLETE" "Completed" else "Failed" end end |
#transaction_id ⇒ Object
Id of this transaction (uniq PayFast transaction id)
62 63 64 |
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 62 def transaction_id params['pf_payment_id'] end |