Server-side processing

When using the custom payment gateway feature, the payment processing happens outside of Snipcart. From your external checkout page, you need to handle the payment yourself and send a confirmation payload back to Snipcart. Doing so will let us process the order on our end and display it inside the dashboard.

Once you've handled the payment on your end, and processed it through your payment gateway, send the payment details to our payment endpoint.

Serverless function example (Netlify functions/JavaScript)

exports.handler = async function (event, context, callback) {
  // Retrieve payment information (depends on how your application is made)
  const requestBody = JSON.parse(event.body);
  const paymentId = uuid();

  // Process the payment with the gateway of your choice here

  // Confirm payment with the /payment endpoint
  const response = await fetch(
    'https://payment.snipcart.com/api/private/custom-payment-gateway/payment', {
    method: 'POST',
    headers: {
      Authorization: 'Bearer <YOUR_SECRET_API_KEY>',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      paymentSessionId: requestBody.paymentSessionId,
      state: requestBody.state,
      error: requestBody.error,
      transactionId: paymentId,
      instructions: 'Your payment will appear on your statement in the coming days',
      links: { refunds: `<YOUR_REFUND_URL>?transactionId=${paymentId}` },
    }),
  });

  if (response.ok) {
    const body = await response.json();

    return {
      statusCode: 200,
      body: JSON.stringify({ ok: true, returnUrl: body.returnUrl })
    };
  }
}

Next up

→ Redirect customers to Snipcart

Was this article helpful?