Skip to main content

Payment lifecycle example

One script ties together payment creation, status, refunds list, and instruments — useful to sanity-check your sandbox credentials end-to-end.
This script shows a typical flow: create a payment, poll state, list refunds, and inspect payment instruments. Copy it into your project and run it with gopay-sdk installed and sandbox credentials in the environment.

Script

import { Currency, type PaymentResponse, createGoPayClient } from 'gopay-sdk';

async function createDemoPayment(): Promise<PaymentResponse> {
  const client = createGoPayClient({
    goid: 8123456789,
    clientId: process.env.GOPAY_CLIENT_ID ?? 'YOUR_CLIENT_ID',
    clientSecret: process.env.GOPAY_CLIENT_SECRET ?? 'YOUR_CLIENT_SECRET',
    gatewayUrl: process.env.GOPAY_GATEWAY_URL ?? 'https://gw.sandbox.gopay.com/api',
  });

  const payment = await client.createPayment({
    amount: 19900,
    currency: Currency.CZK,
    order_number: `LIFECYCLE-${Date.now()}`,
    order_description: 'Lifecycle demo payment',
    payer: {
      contact: {
        email: 'john.doe@example.com',
      },
    },
    callback: {
      return_url: 'https://example.com/return',
      notification_url: 'https://example.com/notify',
    },
  });

  if (!payment.id) {
    throw new Error('Payment creation did not return payment id.');
  }

  console.log('1) Payment created:', payment.id, payment.state);

  const status = await client.getPayment(payment.id);
  console.log('2) Payment inquiry state:', status.state, status.sub_state);

  const refunds = await client.getPaymentRefunds(payment.id);
  console.log('3) Refund history records:', refunds.length);

  const instruments = await client.getPaymentInstruments(8123456789, Currency.CZK);
  console.log('4) Payment instruments available:', instruments.enabledPaymentInstruments.length);

  const allInstruments = await client.getPaymentInstrumentsAll(8123456789);
  const instrumentCount = Array.isArray(allInstruments.enabledPaymentInstruments)
    ? allInstruments.enabledPaymentInstruments.length
    : Object.keys(allInstruments.enabledPaymentInstruments).length;
  console.log('5) All payment instruments records:', instrumentCount);

  return payment;
}

createDemoPayment().catch((error) => {
  console.error('Lifecycle example failed:', error);
  process.exit(1);
});

Run it

Use the same environment variables as in Basic usage. Run with your TypeScript/Node toolchain of choice.