If your website is built in PHP, integrating a payment gateway is mostly a matter of wiring up three steps: create a payment, send the customer to a hosted checkout page, and verify the result when the gateway calls you back. This walkthrough covers that flow and the part developers most often get wrong: verifying the IPN (Instant Payment Notification) signature.
The hosted checkout flow in PHP
With hosted checkout, your website never handles payment details directly. The flow looks like this:
- Your customer places an order on your site. You store the order in your database, including the exact amount and a unique identifier for the order.
- Your PHP code creates a payment with the gateway and redirects the customer to the hosted checkout page, where they pay with JazzCash, Easypaisa, or Raast QR.
- After the payment, the gateway sends an IPN to a callback URL on your server, and the customer is redirected back to your site.
- Your callback handler verifies the IPN signature and the amount, then marks the order as paid.
That fourth step is the one that protects your money. Never trust a return-page redirect alone: a redirect happens in the customer’s browser, and it can be faked, skipped, or replayed. The IPN is a server-to-server message, and the signature is what proves it really came from the gateway.
Limits to know before you initiate a payment
Before you write the integration, keep these limits in mind:
- identifier: maximum 20 characters, and it must be globally unique. Use your order ID if it fits, or a short unique code.
- customer_name: maximum 30 characters.
- customer_email: maximum 30 characters.
Trim or shorten values before sending them, and keep the identifier short from the start so you never have to guess which order an IPN belongs to. For the exact endpoints, request fields, and response formats, see the SwichPay API documentation.
Verifying the IPN signature
Every IPN carries a signature computed from the payment details and your secret API key. Your handler must recompute it and compare it with the value the gateway sent. The formulas are:
- signature = strtoupper(hash_hmac('sha256', amount . identifier, secret_api_key)), where amount is the raw amount string exactly as received.
- status_signature = strtoupper(hash_hmac('sha256', status . '|' . identifier . '|' . amount, secret_api_key)).
Here is a minimal callback handler:
<?php
// Verify the IPN signature before trusting the payment.
$secret = 'your-secret-api-key';
$amount = $_POST['amount']; // raw amount string, exactly as received
$identifier = $_POST['identifier'];
$status = $_POST['status'];
$signature = strtoupper(hash_hmac('sha256', $amount . $identifier, $secret));
$statusSignature = strtoupper(hash_hmac('sha256', $status . '|' . $identifier . '|' . $amount, $secret));
if (!hash_equals($signature, $_POST['signature'])) {
exit('Invalid signature');
}
// Confirm the amount matches the amount stored for this order.
$order = findOrder($identifier);
if ($order['amount'] !== $amount) {
exit('Amount mismatch');
}
markOrderPaid($identifier);
Two rules to follow every time. First, use hash_equals for the comparison, never ==, so the check is not vulnerable to timing attacks. Second, verify the amount against the amount stored for the order in your own database, not the amount in the IPN alone. A valid signature on the wrong amount is still a problem.
Testing before you go live
Once your handler is in place, run a small real payment through it end to end and confirm three things: the IPN arrives, the signature verifies, and the order is marked paid only after verification. Test the failure paths too: send a request with a wrong signature and confirm your handler rejects it, and try an IPN with a tampered amount and confirm it is refused. If you are new to webhooks, our IPN and webhook guide explains the concepts in more detail, and the PHP integration basics guide covers the wider setup for PHP websites.
What is an IPN in payment gateway integration?
An IPN, or Instant Payment Notification, is a server-to-server message the gateway sends to your callback URL when a payment changes status. It tells your system the payment result independently of the customer’s browser, so it is the reliable source of truth for marking an order paid.
Why should I not trust the return page redirect?
The redirect happens in the customer’s browser, so it can be manipulated, skipped, or replayed. Only the IPN, verified with its signature and checked against your stored order amount, proves the payment actually happened.
What happens if my identifier is longer than 20 characters?
It will be rejected when you initiate the payment. Keep identifiers short and unique from the start, and design your order IDs to fit within the 20-character limit.
Start accepting payments with SwichPay
Open a merchant account, connect your website with one API and get notified the moment a customer pays.
