Class: OffsitePayments::Integrations::PayFast::Notification

Inherits:
Notification
  • Object
show all
Includes:
ActiveMerchant::PostsData, Common
Defined in:
lib/offsite_payments/integrations/pay_fast.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 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

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, #iso_currency, #test?, #valid_sender?

Constructor Details

This class inherits a constructor from OffsitePayments::Notification

Instance Method Details

#acknowledge(authcode = nil) ⇒ 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

252
253
254
255
256
257
258
259
260
261
262
# 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

#amountObject

The net amount credited to the receiver's account.


214
215
216
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 214

def amount
  params['amount_net']
end

#complete?Boolean

Was the transaction complete?


179
180
181
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 179

def complete?
  status == "Completed"
end

#currencyObject


228
229
230
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 228

def currency
  nil
end

#empty!Object

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


233
234
235
236
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 233

def empty!
  super
  @params  = ActiveSupport::OrderedHash.new
end

#feeObject

The total in fees which was deducted from the amount.


209
210
211
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 209

def fee
  params['amount_fee']
end

#grossObject

The total amount which the payer paid.


204
205
206
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 204

def gross
  params['amount_gross']
end

#item_idObject

Id of this transaction (uniq Shopify transaction id)


199
200
201
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 199

def item_id
  params['m_payment_id']
end

#item_nameObject

The name of the item being charged for.


219
220
221
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 219

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.


224
225
226
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 224

def merchant_id
  params['merchant_id']
end

#statusObject

Status of transaction. List of possible values:

COMPLETE

185
186
187
188
189
190
191
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 185

def status
  if params['payment_status'] == "COMPLETE"
    "Completed"
  else
    "Failed"
  end
end

#transaction_idObject

Id of this transaction (uniq PayFast transaction id)


194
195
196
# File 'lib/offsite_payments/integrations/pay_fast.rb', line 194

def transaction_id
  params['pf_payment_id']
end