JavaScript checkout SDK

Learn how to use the Payfi JavaScript checkout SDK.

Getting Started

📘

Live Base URL

https://api.payfi.ng/v1

📘

Staging Base URL

https://core-dev.payfi.ng/v1

📘

Live SDK URL

https://payfi-sdk-js.s3.amazonaws.com/payfi.js

📘

Staging SDK URL

https://payfi-sdk-js-staging.s3.eu-west-1.amazonaws.com/payfi.js

Parameters Table

ParamRequiredDescription
apiKeyYESPublic key attached to your account.
amountYESAmount in Kobo.
referenceYESA unique transaction reference.
callbackNOFunction that runs when payment is successful.
onCloseNOFunction called if the customer closes the payment window.
metaNOYou can add the order information to the meta object.
The order is an array as the user may purchase multiple items at a time
e.g meta={order:[{name, id, category, price, quantity}]} Check the table below for the order properties
Order paramDescription
nameThe name of the item the user is trying to purchase
idThe order Id, if any
categoryThe category of the order the user is trying to purchase, if any
priceThe amount of the purchase
quantityThe number of each item the user is purchasing

Step 1

First initialize the SDK as shown below:

const payfi = new Payfi({
  apiKey: 'pk_ce082ddd-c15f-',
  callbackUrl: "http://test.com",
  meta: {
    order: [
   		{
    		name: '',
        id: '',
        category: '',
        price: '',
        quantity: '',
    	}
    ]
  },
  callback: reference => {
    document.getElementById("btn").removeAttribute("disabled")
    console.log('callback called', reference);
  },
  onClose: () => {
    document.getElementById("btn").removeAttribute("disabled")
    console.log('modal close')
  }
});

Step 2

📘

Initiate payment process passing the amount and reference.

payfi.pay({ amount, reference: ${Date.now()} })

step 3

On the callback function, the verify transaction endpoint needs to be called to verify the transaction.
Make a HTTP POST endpoint + /merchant/purchase/verify-by-reference

{
  "payfi-sec-key":"secret key"
}

Body

{
  "reference":"testing-now-igho"
}

Example Implementation

<script src="https://payfi-sdk-js.s3.amazonaws.com/payfi.js"></script>
  <script type="text/javascript">
     let amount;
     const buttonElement = document.getElementById("btn");
     const handleInputChange = e => {
       amount = document.getElementById("amount").value;
     }
     const handleFormSubmit = () => {
       payfi.pay({ amount, reference: `${Date.now()}` })
     }
     const payfi = new Payfi(
       {
         apiKey: 'pk_ce082ddd-c15f',
         callbackUrl: "http://test.com",
         meta: {
          order: [
            {
              name: 'leather shoe',
              id: 'order_1234',
              category: 'Men shoe',
              price: 40000,
              quantity: '1',
            }
          ]
 				 },
         callback: reference => {
          console.log('callback called', reference);
         },
         onClose: () => {
           console.log('modal close')
          }
        });
  </script>