Connect With Us

Integrating Afterpay Payment in a Rails Project

By Umesh P Kafle on Sep, 07, 2018

Afterpay is a payment gateway which allow consumer to ‘Buy Now, Enjoy Now and Pay Later’. Afterpay is 100% interest free and easy to pay by its installment process. These all can be done by registering in Afterpay with the personal and card details. Consumer can buy stuff upto $1000 and pay that amount with 4 equal installments that will be due every 2 weeks. Consumer can make repayments to Afterpay any time BEFORE the due date. Otherwise, Afterpay will automatically try process payments on the scheduled dates from registered card. If a payment is not processed on or before the due date, late fees will apply – initial $10 late fee, and a further $7 if the payment remains unpaid 7 days after the due date.

Required information for integration in Rails application

To get started with the integration, the credentials we need are as follows:

  • Merchant ID
  • Merchant Key
  • Merchant Name
  • User Agent.

These all credentials will be provided by the afterpay team once you get in touch with their customer assistance team.

Test Merchant account

Need to get register with email and password to get the sandbox merchant credentials, which will allow to create order, capture payment (create charge) and create refund. Credentials to pay via afterpay will be:

  • Your personal details.
  • Your card details
    • Card number (4242424242424242 for test)
    • Card expiry date
    • PCV code

Integration process in Rails Application

As, there is not any gem available to integrate afterpay in Rails application, need to access the afterpay API using its curl. Further API documentation will be available in https://docs.afterpay.com/au-online-api-v1.html#api-endpoint

Configuration

To get the authorization token need to encode MERCHANT_ID:MERCHANT_KEY into Base64. So, the actual authorization code will be ‘Basic + encoded authorization.’

API operation

Let’s create a separate service for the afterpay.

require 'rest-client'
require 'json'
class AfterPayHandler
end

Create Order

Once, you got the authorization token and and the values as in https://docs.afterpay.com/au-online-api-v1.html#orders this will redirect you to the afterpay payment portal if you have required and called the afterpay javascript with the order token,  where you need to add your login and payment credentials. Where you can set the redirect_url after success and fail. If the ordering process got succeeded you will get “order token” and “token expiry date” as response.

def create_order
  order = {totalAmount: {...}}
  user_agent = ENV['AFTERPAY_USER_AGENT']
  authorization = "Basic" + " " + ENV['AFTERPAY_API_KEY']
  url = "https://api-sandbox.afterpay.com/v1/orders"
  header = {content_type: "application/json", user_agent: user_agent,
              authorization: authorization, accept: 'application/json'}
  response = RestClient::Request.execute(method: :post, url: url, payload: JSON.dump(order), headers: header)
end

Capture Payment

After you get the order token from the create order API you can proceed for the capture payment process. Here its a bit tricky cause the token has only of 30min lifetime. So, it would be better if you capture payment as soon as the order created. For the capturing payment you can go through

https://docs.afterpay.com/au-online-api-v1.html#capture-payment .

def capture_payment
  token = ENV['TOKEN']
  user_agent = ENV['AFTERPAY_USER_AGENT']
  authorization = "Basic" + " " + ENV['AFTERPAY_API_KEY']
  url = "https://api-sandbox.afterpay.com/v1/payments/capture"
  header = {content_type: "application/json", user_agent: user_agent,
            authorization: authorization, accept: 'application/json'}
  response = RestClient::Request.execute(method: :post, url: url, payload: JSON.dump({token: token}), headers: header)
end

 

Refund

Refund API will refund the captured order amount.

def refund
  body = { amount: { amount:payment_amount.to_s, currency: "AUD" }}
  user_agent = ENV['AFTERPAY_USER_AGENT']
  authorization = "Basic" + " " + ENV['AFTERPAY_API_KEY']
  url = "https://api-sandbox.afterpay.com/v1/payments/#{@reservation.charge_id}/refund"
  header = {content_type: "application/json",
            authorization: authorization, accept: 'application/json'}
  response = RestClient::Request.execute(method: :post, url: url, payload: JSON.dump(body), headers: header)
end

For more details on refunding you can follow:

https://docs.afterpay.com/au-online-api-v1.html#create-refund

Share This:

Latest Posts


Integrating Afterpay Payment in a Rails Project By Umesh P Kafle on Sep, 07, 2018

Switching to React Native from Ruby on Rails By Kamal Pandey on Aug, 29, 2018

Jyaasa in IT Career Fair 2018 By Sonika Thapa on Jun, 12, 2018

Encrypted secrets in Ruby on Rails By Rasna Shakya on Apr, 20, 2018

Rails Payment Integration with zipPay (Buy Now and Pay Later) By Nelson Suwal on Mar, 23, 2018