Payment Gateway Integration in PHP: A Code Walkthrough

Payment Gateway Integration in PHP: A Code Walkthrough

Integrating a payment gateway into a PHP website sounds like a big job, but a hosted checkout keeps it simple. Your site does not handle card numbers or wallet credentials at all. Instead, your PHP code creates a payment, sends the customer to a secure checkout page, and then listens for a server-to-server notification, called an IPN, that confirms the result. This walkthrough covers that flow step by step, with a working signature check you can adapt for your own project.

How Hosted Checkout Works

The flow has three parts, and each one is short:

  1. Create the payment. Your server sends the order details to the gateway and receives a checkout URL in return.
  2. Redirect the customer. You send the customer to that checkout URL, where they choose how to pay, for example with JazzCash, Easypaisa or Raast QR.
  3. Verify the IPN. After the payment, the gateway calls a callback URL on your server. Your code verifies the signature on that callback before marking the order as paid.

Because the customer pays on the gateway’s page, you never store sensitive payment details, and your PCI burden stays small. Your job is to get the three steps right, especially the third one.

Step 1: Create the Payment

When a customer reaches checkout on your site, your PHP code builds a payment request. The exact fields and endpoint are listed in the ZurvaPay API documentation, but a few limits are worth knowing before you write the code. The payment identifier must be at most 20 characters long, and it must be globally unique, so a short order reference or a trimmed unique id works well. The customer name is limited to 30 characters and the customer email to 30 characters, so trim or shorten long values before sending them.

Store the order in your own database first, with its amount and status set to pending, and only then create the payment. If anything goes wrong later, you have a record to reconcile against. Keep the amount exactly as you sent it, because you will need it again during verification.

Step 2: Redirect the Customer

Once the gateway returns a checkout URL, redirect the customer to it with a normal HTTP redirect. Do not open it in an iframe unless the documentation says so, and do not try to embed the checkout inside your own page. The customer completes the payment on the gateway’s secure page and is then sent back to your site.

That return redirect is a convenience for the customer, not proof of payment. A customer can close the browser before returning, or the redirect can be interrupted. Your order status must never change based on the return page alone. The real confirmation comes from the IPN in the next step.

Step 3: Verify the IPN Signature

The IPN is a server-to-server callback. Anyone who knows your callback URL could send a fake request to it, so you must verify the signature on every callback before trusting it. ZurvaPay signs the callback with HMAC-SHA256 using your secret API key. The signature is computed as strtoupper(hash_hmac('sha256', amount . identifier, secret_api_key)), where amount is the raw amount string exactly as sent. A second signature covers the status: strtoupper(hash_hmac('sha256', status . '|' . identifier . '|' . amount, secret_api_key)).

Here is a compact verification handler in PHP. It checks both signatures with hash_equals, which compares strings in constant time, and then checks the amount against the amount stored for the order:

<?php
$secret = 'YOUR_SECRET_API_KEY';
$amount = $_POST['amount'];
$id = $_POST['identifier'];
$expected = strtoupper(hash_hmac('sha256', $amount . $id, $secret));
if (!hash_equals($expected, $_POST['signature'])) { exit('Bad signature'); }
$status = strtoupper(hash_hmac('sha256', $_POST['status'] . '|' . $id . '|' . $amount, $secret));
if (!hash_equals($status, $_POST['status_signature'])) { exit('Bad status signature'); }
if ($amount !== $storedAmount) { exit('Amount mismatch'); }
// Verified: mark the order paid.
?>

Two points matter here. First, never trust a return-page redirect alone; always verify the signature and confirm that the amount matches your stored order amount. A valid signature with a different amount still means something is wrong. Second, keep your secret API key on the server only. Never put it in JavaScript, in a mobile app, or in a public repository.

If you are new to callbacks, our IPN and webhook guide explains the concepts in more depth, and the PHP integration basics guide covers the wider setup, including plugins for WordPress and WooCommerce if you would rather not write the integration by hand.

Common Mistakes to Avoid

  • Marking orders paid on the return page. The redirect can be faked or skipped. Only the verified IPN confirms payment.
  • Comparing amounts as floats. Compare the raw amount string against the stored amount string, exactly as sent.
  • Reusing identifiers. Each identifier must be globally unique. Reusing one can mix up two different orders.
  • Skipping the status signature. Verify both signatures, not just the first, so a tampered status cannot slip through.
  • Logging the secret key. Never write your secret API key to logs, error pages or debug output.

Do I need to handle card details in my PHP code?

No. With hosted checkout, the customer pays on the gateway’s secure page. Your PHP code only creates the payment, redirects the customer, and verifies the IPN callback.

What happens if the customer closes the browser before returning?

Nothing breaks. The IPN is sent server to server, independent of the customer’s browser, so your callback still receives the payment result and can update the order.

Why must I check the amount as well as the signature?

The signature proves the callback came from the gateway, but the amount check proves the payment matches your order. Both checks together protect you from tampered or mismatched callbacks.

#payment gateway integration in PHP#PHP payment gateway code#IPN signature verification#hash_hmac payment verification#ZurvaPay API

Start accepting payments with ZurvaPay

Open a merchant account, connect your website with one API and get notified the moment a customer pays.