Skip to main content

Verify IPN Call

When the payment is completed, VNPay will send an IPN (Instant Payment Notification) call to the IPN URL that you have set up. To verify the IPN call, you can use the VNPay library.

Verify IPN

import { VerifyIpnCall } from 'vnpay';

/* ... */

const verify: VerifyIpnCall = vnpay.verifyIpnCall(req.query);

Properties of the VerifyIpnCall

Information after verification and returned by VNPay

PropertyData TypeDescription
isSuccessbooleanThe result of the payment order
isVerifiedbooleanThe result of verifying the integrity of the data received from VNPay
messagestringVerification message
vnp_AmountnumberThe payment amount, automatically calculated by the library
......Other parameters that VNPay will return, refer here

See more properties VNPay will return at VNPay.

tip

The parameters that VNPay returns are also in the VerifyIpnCall.

Usage

Using logger

  • Similar to when creating a payment URL, you can use a logger to log IPN verification information see here.

With Express

Steps to verify the return URL in Express:

  1. Create a route to handle the return URL
  2. Verify the return URL
  3. Handle the information returned from VNPay
  4. Update the order status in your database
  5. Update the status back to VNPay to let them know that you have confirmed the order
controllers/payment.controller.ts
import {
IpnFailChecksum,
IpnOrderNotFound,
IpnInvalidAmount,
InpOrderAlreadyConfirmed,
IpnUnknownError,
IpnSuccess,
} from 'vnpay';

/* ... */

app.get('/vnpay-ipn', async (req, res) => {
try {
const verify: VerifyReturnUrl = vnpay.verifyIpnCall(req.query);
if (!verify.isVerified) {
return res.json(IpnFailChecksum);
}

// Find the order in your database
const foundOrder = await findOrderById(verify.vnp_TxnRef); // Method to find an order by id, you need to implement it

// If the order is not found or the order code does not match
if (!foundOrder || verify.vnp_TxnRef !== foundOrder.orderId) {
return res.json(IpnOrderNotFound);
}

// If the payment amount does not match
if (verify.vnp_Amount !== foundOrder.amount) {
return res.json(IpnInvalidAmount);
}

// If the order has been confirmed before
if (foundOrder.status === 'completed') {
return res.json(InpOrderAlreadyConfirmed);
}

/**
* After verifying the order is complete,
* you can update the order status in your database
*/
foundOrder.status = 'completed';
await updateOrder(foundOrder); // Function to update the order status, you need to implement it

// Then update the status back to VNPay to let them know that you have confirmed the order
return res.json(IpnSuccess);
} catch (error) {
/**
* Handle exceptions
* For example, insufficient data, invalid data, database update failure
*/
console.log(`verify error: ${error}`);
return res.json(IpnUnknownError);
}
});