Skip to content

Back URL handling ​

After the payment (or when the customer cancels, or time runs out) SimplePay redirects the customer to redirectUrl / SIMPLEPAY_REDIRECT_URL (or the matching urls entry) with two query parameters:

https://example.com/payment/back?r=eyJyIjowLCJ0Ijo1MDQy...&s=El%2Bq...
  • r: Base64-encoded JSON with the result
  • s: the signature of that JSON

Verify it ​

ts
import { getPaymentResponse } from 'simplepay-js-sdk'

const url = new URL(request.url)
const r = url.searchParams.get('r') ?? ''
const s = url.searchParams.get('s') ?? ''

const result = getPaymentResponse(r, s)

getPaymentResponse:

  1. decodes r,
  2. finds the SIMPLEPAY_MERCHANT_ID_* variable holding its merchant ID, and that currency's key,
  3. checks the s signature (timing-safe comparison),
  4. returns the result.

It is synchronous and makes no network call.

The result ​

ts
{
  responseCode: 0,
  transactionId: '504233881',
  event: 'SUCCESS',
  merchantId: 'P123456',
  orderRef: 'order-12',
  tokens: undefined, // card registration tokens for recurring payments
}
eventMeaningWhat to show
SUCCESSSuccessful payment (card authorised)“Thank you, your payment is being processed / succeeded”
FAILFailed paymentError message, retry option, the transactionId
TIMEOUTPayment time ran out“Time ran out”, start a new payment
CANCELThe customer cancelled“You cancelled the payment”

event values are uppercase.

The IPN closes the order

The back URL is feedback for the customer only. The customer may close the browser before returning, or open an old back URL by hand. Set the order to paid from the IPN. If the IPN has not arrived yet, say on the SUCCESS page that the payment is being processed.

On failed payments

SimplePay expects you to show the customer the SimplePay transaction ID on a failed payment and tell them they can ask their bank. Exact wording: SimplePay documentation.

Errors ​

getPaymentResponse throws when:

  • r is not valid Base64 / JSON (SyntaxError)
  • the merchant ID is in no SIMPLEPAY_MERCHANT_ID_* variable: Merchant id not found in the environment: P123456
  • the currency has no key: Missing SimplePay configuration for HUF
  • the signature is wrong: Invalid response signature

Answer these with a 400 or a generic error page, and leave the order alone.

Space instead of + ​

If your framework decodes the query string with application/x-www-form-urlencoded rules, the + signs in the s signature become spaces. getPaymentResponse handles this: it turns every space back into + before checking.

Recurring payments ​

For card registration (startRecurringPayment) the result's tokens field may hold tokens if SimplePay sends them. You can also save the tokens from the response of the payment start, see Recurring payments.

Unofficial community SDK. Not affiliated with or endorsed by SimplePay Zrt. MIT License.