Skip to main content
Sosho Studio logo
A paper shop model, a blank bank card, a brass padlock and a small server block linked in a row by copper wire on a sunlit desk
Next.jsPaymentSecurityBackendEcommercePublished

Connecting the Zarinpal Payment Gateway to a Next.js Site: A Secure Implementation Guide

Connecting Zarinpal to Next.js in three steps: payment request, customer return and server-side verification. Fields, response codes, sandbox testing and the mistakes that lose orders.

Quick answer

To connect Zarinpal to Next.js, your server sends a payment request with the merchant_id, amount and callback URL and receives an authority code. The customer goes to Zarinpal's StartPay page and returns to your site with Authority and Status parameters. If Status is OK, the server calls verify with the amount stored on the order; only code 100 (or 101 for a repeated verification) means the payment succeeded. The merchant_id and the amount calculation must always stay on the server.

A Zarinpal payment in three steps

Every Zarinpal payment has three steps. First, your server sends a payment request to Zarinpal and receives a temporary identifier called the authority. Second, the customer is sent with that authority to Zarinpal's payment page, enters their card and, after paying or cancelling, returns to the callback address you specified.

The third step matters most: your server must verify the payment. Until verification succeeds, the customer returning to your site does not prove that any money was paid. Most security mistakes and lost orders happen exactly here.

  • Request: POST to payment.zarinpal.com/pg/v4/payment/request.json and receive the authority
  • Redirect: send the customer to payment.zarinpal.com/pg/StartPay/ followed by the authority
  • Return: the customer comes back to your callback_url with Authority and Status parameters
  • Verify: POST to payment.zarinpal.com/pg/v4/payment/verify.json with the same amount and authority

To put this into practice, explore the scope and real work for these services: Ecommerce Website Design and Custom Web App Development.

Before coding: merchant, currency unit and where the code runs

A live gateway needs a merchant_id: a 36-character string you receive in the Zarinpal panel once your merchant account and site domain are approved. Ask Zarinpal for the current acceptance requirements, as they can change. Treat this identifier like a secret: it must not appear in browser code, the git repository or any variable starting with NEXT_PUBLIC_.

Decide the amount unit on day one. The v4 documentation lets you state the unit with the currency field as IRR (rial) or IRT (toman), and describes the verify amount in rial. The simplest, least error-prone approach is to store amounts as integer rials everywhere (database, cart and gateway) and send the currency explicitly. A factor-of-ten rial/toman mix-up is one of the most common integration bugs.

In Next.js, the request and verification code must run on the server, in a Route Handler (a route.ts file) or a Server Action. If your site does not run on a Next.js server, as with a static export, put these two steps in a separate backend such as a Cloudflare Worker or a Node.js service and call it from the page.

Step one: creating the payment request

The browser should send the server only the order identifier, never the amount. The server reads the order from the database, calculates the final amount itself and then calls Zarinpal. If the amount comes from a browser form or cart, anyone can change it and buy an expensive item for a small sum.

If Zarinpal's response has code 100, store the authority with the order and set the order to “awaiting payment”. The authority should be unique so you can find the order from it on return. Then redirect the customer to the StartPay address.

  • merchant_id: the merchant identifier, read only from a server-side environment variable
  • amount: an integer amount calculated on the server
  • currency: IRR or IRT; send it explicitly
  • callback_url: a full https address on your approved domain
  • description: a short description, such as the order number
  • metadata (optional): the customer's mobile, email or order_id

Step two: the customer returns to the callback_url

Zarinpal sends the customer to your callback address with two parameters, Authority and Status. Status is either OK or NOK. NOK means the payment failed or the customer cancelled; in that case do not call verify, keep the order unpaid and offer a way to try again.

OK does not mean the payment succeeded either; it only means you should verify. Anyone can type these query parameters by hand, so look the order up by Authority in your database and reject the request if no order has that authority.

Step three: verify and record the result

For verify, send the merchant_id, the authority and the amount, reading the amount from the stored order rather than the page address. Code 100 means the payment succeeded with this verification; code 101 means the transaction was already verified once. Any other code means the payment is not verified.

In a successful response, ref_id is the transaction reference number: show it to the customer and store it with the order. card_pan is the masked card number, useful for support; you never receive the full card number and should not look for it in logs.

Customers may refresh the return page or land on it twice. Make the order update conditional, for example “mark as paid only if it is still awaiting payment”. That way two simultaneous requests do not ship twice, send two emails or reduce stock twice. Treat code 101 as success for an order already recorded as paid.

Security and reliability checklist

A payment gateway is where a small mistake turns directly into money. Check these items one by one before going live.

  • The merchant_id lives only in a server environment variable, never in browser code or the repository
  • The amount is always calculated on the server from the stored order
  • An order becomes paid only after a successful verify, never from Status=OK
  • Conditional order updates prevent double recording or double shipping
  • Verify calls have a timeout and limited retries, with errors recorded
  • Logs carry no sensitive data: only the order id, authority and response code
  • The callback_url uses https on the same domain you registered with Zarinpal
  • Rate-limit the payment-creation route so nobody can create thousands of requests

Testing in the sandbox

Zarinpal provides a test environment. According to its sandbox documentation, replace payment.zarinpal.com with sandbox.zarinpal.com in the addresses and use any UUID as the merchant_id. Sandbox authorities start with the letter S. Keep the base address in an environment variable so switching between test and live is only a configuration change.

Do not test only the happy path. Cancel a payment, close the page mid-payment, refresh the return page, open the return address again and change the Authority in the address by hand. Before launch, make one real payment with a small amount and compare the ref_id in the Zarinpal panel with the order on your site.

Common integration mistakes

These mistakes are common in payment integrations, and most disappear once the three steps above are followed.

  • Marking an order paid just because the address shows Status=OK
  • Reading the verify amount from the page address or a form instead of the stored order
  • A rial/toman mismatch between the cart and the gateway
  • Not handling code 101 and showing an error to a customer who refreshed the page
  • A callback_url on a domain or subdomain other than the registered one
  • Putting the merchant_id in browser code or a NEXT_PUBLIC_ variable

Frequently asked questions

Can Zarinpal be used on a static-export site without a Next.js server?

Yes, as long as the request and verify steps run in a separate backend, such as a Cloudflare Worker, a Node.js service or any server that keeps the merchant_id secret. The static page only sends the order id to that backend.

What does code 101 mean in Zarinpal's verify?

It means the transaction was already verified successfully once. It usually happens when the customer refreshes the return page or two requests arrive together. If the order is already recorded as paid, treat it as success and do not process it again.

Should the amount be sent in rial or toman?

Version 4 accepts both through the currency field, but consistency is what matters. We suggest keeping rials throughout the system, sending currency as IRR explicitly and using the same stored amount for verify. Check Zarinpal's current documentation again before launch.

What happens if verify fails because of a network outage?

The order stays “awaiting payment”. Retry verify with a timeout and a few limited attempts, and record the error. If it still fails, tell the customer their payment is being checked and follow the order up in the Zarinpal panel using the authority.

Sources and further reading