Sign in

Payments

How to integrate Popup Checkout

View as Markdown

In a nutshell

Initialize a Paystack transaction on your server and complete it in the browser with Popup Checkout.

For an overview of how Checkout works, see Checkout.

Copy linkInstall InlineJS

Use either the CDN script or the package:

<script src="https://js.paystack.co/v2/inline.js"></script>
npm i @paystack/inline-js
import Paystack from '@paystack/inline-js'

const popup = new Paystack()

Copy linkInitialize the transaction on your backend

Create the transaction from your server and return the access_code to the browser.

Danger

Never call the Paystack API directly from your frontend code. Doing so exposes your secret key to anyone who views your page source or intercepts the request. All Paystack API calls must be made from your server, and your frontend should only receive what your server chooses to pass back.

const https = require('https')

const params = JSON.stringify({
  email: '[email protected]',
  amount: '500000',
  callback_url: 'https://your-app.example.com/payment/callback'
})

const options = {
  hostname: 'api.paystack.co',
  port: 443,
  path: '/transaction/initialize',
  method: 'POST',
  headers: {
    Authorization: 'Bearer YOUR_SECRET_KEY',
    'Content-Type': 'application/json'
  }
}

const req = https.request(options, (res) => {
  let data = ''

  res.on('data', (chunk) => {
    data += chunk
  })

  res.on('end', () => {
    console.log(JSON.parse(data))
  })
})

req.write(params)
req.end()

A successful response returns:

{
  "status": true,
  "message": "Authorization URL created",
  "data": {
    "authorization_url": "https://checkout.paystack.com/nkdks46nymizns7",
    "access_code": "nkdks46nymizns7",
    "reference": "nms6uvr1pl"
  }
}

Copy linkOpen Popup with the access code

Call your backend endpoint, then resume the transaction in the browser.

import Paystack from '@paystack/inline-js'

const popup = new Paystack()

async function startPayment() {
  const response = await fetch('/api/initialize-payment', { method: 'POST' })
  const { access_code } = await response.json()

  popup.resumeTransaction(access_code)
}

Copy linkHandle the callback URL

Set a callback_url when you initialize the transaction. Paystack appends the transaction reference to that URL after the payment attempt.

app.get('/payment/callback', async (req, res) => {
  const { reference } = req.query
  res.redirect(`/payment/status?reference=${reference}`)
})

Do not fulfil the order from the callback alone.

Copy linkVerify the transaction

Verify the returned reference from your server:

const https = require('https')

const options = {
  hostname: 'api.paystack.co',
  port: 443,
  path: '/transaction/verify/:reference',
  method: 'GET',
  headers: {
    Authorization: 'Bearer YOUR_SECRET_KEY'
  }
}

https.request(options, (res) => {
  let data = ''

  res.on('data', (chunk) => {
    data += chunk
  })

  res.on('end', () => {
    console.log(JSON.parse(data))
  })
}).end()

Check response.data.status and response.data.amount before you deliver value.

Copy linkHandle the webhook

Use the charge.success webhook as your primary signal to fulfill orders, grant access, or update your database. The customer can close the page before your callback runs, but the webhook still arrives.

Add your webhook URL on the Paystack Dashboard, and verify the signature on every event with your webhook signing secret.

{
  "event": "charge.success",
  "data": {
    "status": "success",
    "reference": "re4lyvq3s3",
    "amount": 40333,
    "channel": "card"
  }
}

Use the verify endpoint for callback handling, reconciliation, and cases where you need to confirm the final state manually.

Was this page helpful?