Class: ActiveMerchant::Billing::Integrations::PayFast::Notification

Inherits:
Notification
  • Object
show all
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

#params, #raw

Instance Method Summary collapse

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

#acknowledgeObject

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

#amountObject

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?

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_merchant/billing/integrations/pay_fast/notification.rb', line 47

def complete?
  status == "Completed"
end

#currencyObject



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

#feeObject

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

#grossObject

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_idObject

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_nameObject

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_idObject

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

#statusObject

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_idObject

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