class OffsitePayments::Integrations::PayFast::Notification

Parser and handler for incoming ITN from PayFast. The Example shows a typical handler in a rails application.

Example

class BackendController < ApplicationController
  include OffsitePayments::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

Public Instance Methods

acknowledge(authcode = nil) click to toggle source

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
# File lib/offsite_payments/integrations/pay_fast.rb, line 252
def acknowledge(authcode = nil)
  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() click to toggle source

The net amount credited to the receiver’s account.

# File lib/offsite_payments/integrations/pay_fast.rb, line 214
def amount
  params['amount_net']
end
complete?() click to toggle source

Was the transaction complete?

# File lib/offsite_payments/integrations/pay_fast.rb, line 179
def complete?
  status == "Completed"
end
currency() click to toggle source
# File lib/offsite_payments/integrations/pay_fast.rb, line 228
def currency
  nil
end
empty!() click to toggle source

Generated hash depends on params order so use OrderedHash instead of Hash

Calls superclass method OffsitePayments::Notification#empty!
# File lib/offsite_payments/integrations/pay_fast.rb, line 233
def empty!
  super
  @params  = ActiveSupport::OrderedHash.new
end
fee() click to toggle source

The total in fees which was deducted from the amount.

# File lib/offsite_payments/integrations/pay_fast.rb, line 209
def fee
  params['amount_fee']
end
gross() click to toggle source

The total amount which the payer paid.

# File lib/offsite_payments/integrations/pay_fast.rb, line 204
def gross
  params['amount_gross']
end
item_id() click to toggle source

Id of this transaction (uniq Shopify transaction id)

# File lib/offsite_payments/integrations/pay_fast.rb, line 199
def item_id
  params['m_payment_id']
end
item_name() click to toggle source

The name of the item being charged for.

# File lib/offsite_payments/integrations/pay_fast.rb, line 219
def item_name
  params['item_name']
end
merchant_id() click to toggle source

The Merchant ID as given by the PayFast system. Used to uniquely identify the receiver’s account.

# File lib/offsite_payments/integrations/pay_fast.rb, line 224
def merchant_id
  params['merchant_id']
end
status() click to toggle source

Status of transaction. List of possible values:

COMPLETE
# File lib/offsite_payments/integrations/pay_fast.rb, line 185
def status
  if params['payment_status'] == "COMPLETE"
    "Completed"
  else
    "Failed"
  end
end
transaction_id() click to toggle source

Id of this transaction (uniq PayFast transaction id)

# File lib/offsite_payments/integrations/pay_fast.rb, line 194
def transaction_id
  params['pf_payment_id']
end