How to integrate payfort in rails API, Swift and kotlin

PayFort is a trusted online payment gateway enabling businesses, governments, SMEs,startups and institutions with innovative payment options for both the banked and non-banked online shoppers.

Image for post
Image for post

This article will walk you through the entire process of integrating Payfort with a Rails API.

We’ll concentrate on the Ruby on Rails back-end, so you’ll need to create the payment form manually in your app.

There are many approaches to implement Payfort getaway one of these approaches we followed is to let the front-end authorise the payment status
After customer submit the order front-end will use SDK Operation

The Mobile SDK allows the Merchant’s application to process Authorization and Purchase operations. But in our case we need Authorization only.

Authorization: Authorization operation holds an amount from the Customer’s credit card account for a period of time until the Merchant capture or voids the transaction.
Purchase: Purchase operation holds an amount from the Customer’s credit card account and puts in merchant account.

We used Authorization operation because provider might reject the order that submitted by the customer and we don’t want to capture the amount until order been in progress

These steps are from Payfort Doc guides you to know,to start building an integration with PayFort:

Step 1: Access your test account You have to make sure that you have an access to a test account, its a full test environment allow you to simulate and process simulation transactions.

Step 2: make sure that you are using the correct integration type Before building the integration, you need to make sure that you are selecting and using the

proper parameters in the API calls as per the required integration type.

All the mandatory parameters mentioned under every section in the API document.

Step 3: Create the transaction request Process the valid API request depends on transaction parameters included, you need to check the documentation and read every parameter possible values in order to reduce the errors in processing the transaction.

Step 4: Process the transaction response After every payment, PayFort return the transaction response on the URL configured in your account under Technical Settings channel configuration.

For step 4 we will not need to use payfort web-hooks because if there’s some issue with our server like server is down we will not be able to track the payment process so instead we can use other operation called query option mean that you can make query to payfort and track the payment status

Front-end developer will send request to payfort that tells `Hey!! please keep this amount with you till we make another request that will tell you either to capture the amount or void it

If the payment request succeed then will update the order record in DB
and telling that `transaction_status` has success otherwise if request field thee will update the server `transaction_status` has failed this will help Back-end developer to track the payment status in payfort side

Let’s get in code

Client Side(IOS) request

You can see above how to make request in swift writing by one of our developers Aman aggarwal

Client Side(Android) request

You can see above how to make request in Kotlin writing by one of our developers MohammedSDev

You can get the source code of android here! MohammedSDev

Server Side -API requests

After request has been sent from client-side, server side has either capture the amount or void

We used rails Callbacks to track the order if the order updated, provider accepted the order and order moved from pending to in_progress capture the amount

If the provider rejected the order will void the amount

NOTE: We used same API to make a request but we changed the command

References:

Note: You can get the sha request phrase from payfort dhashboard
string = Digest::SHA256.hexdigest(“sha request phrase#{string.gsub(/&/, “”)}sha request phrase”)
params.store(‘signature’, string)

uri = URI(“https://sbpaymentservices.payfort.com/FortAPI/paymentApi")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.path, ‘Content-Type’ => ‘application/json’)
req.body = params.to_json
res = http.request(req)
end
end

Self-Taught Back-End Developer || Aspiring Full-Stack Developer || Tech enthusiast || Big fans of FC. Barcelona.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store