Skip to main content

Integration Examples

1. Simple charge

Charge a customer's card for a one-time payment.

curl

curl https://api.peakgateway.co/api/v1/transactions \
  -H "Authorization: Bearer tpk_your_test_key" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "SALE",
    "amount": 2500,
    "currency": "USD",
    "cardNumber": "4111111111111111",
    "expirationDate": "1230",
    "cvv2": "123",
    "cardDataSource": "MANUAL",
    "transactionIndustryType": "EC"
  }'

TypeScript

import { GatewayClient } from '@gateway/sdk';

const client = new GatewayClient({ apiKey: process.env.GATEWAY_API_KEY });

const transaction = await client.transactions.create({
  type: 'SALE',
  amount: 2500,
  currency: 'USD',
  cardNumber: '4111111111111111',
  expirationDate: '1230',
  cvv2: '123',
  cardDataSource: 'MANUAL',
  transactionIndustryType: 'EC',
});

console.log(`Transaction ${transaction.transactionId} — ${transaction.status}`);

Kotlin

import com.myriad.gateway.api.TransactionsApi
import com.myriad.gateway.model.CreateTransactionRequest

val client = GatewayClient(apiKey = System.getenv("GATEWAY_API_KEY"))
val api = TransactionsApi(client)

val txn = api.createTransaction(CreateTransactionRequest(
    type = "SALE",
    amount = 2500,
    currency = "USD",
    cardNumber = "4111111111111111",
    expirationDate = "1230",
    cvv2 = "123",
))

println("Transaction ${txn.transactionId} — ${txn.status}")

2. Subscription setup

Create a monthly recurring billing subscription.

curl

# 1. Create a subscription
curl https://api.peakgateway.co/api/v1/subscriptions \
  -H "Authorization: Bearer tpk_your_test_key" \
  -H "Content-Type: application/json" \
  -d '{
    "merchantId": "merch_123",
    "customerId": "cust_456",
    "amount": 1999,
    "currency": "USD",
    "interval": "monthly",
    "intervalCount": 1,
    "cardNumber": "4111111111111111",
    "expirationDate": "1230",
    "cvv2": "123",
    "startDate": "2026-04-01"
  }'

TypeScript

const subscription = await client.subscriptions.create({
  merchantId: 'merch_123',
  customerId: 'cust_456',
  amount: 1999,
  currency: 'USD',
  interval: 'monthly',
  intervalCount: 1,
  cardNumber: '4111111111111111',
  expirationDate: '1230',
  cvv2: '123',
  startDate: '2026-04-01',
});

console.log(`Subscription ${subscription.subscriptionId} — next billing: ${subscription.nextBillingDate}`);

// Cancel later
await client.subscriptions.cancel(subscription.subscriptionId);

Kotlin

val subscription = api.createSubscription(CreateSubscriptionRequest(
    merchantId = "merch_123",
    customerId = "cust_456",
    amount = 1999,
    currency = "USD",
    interval = "monthly",
    intervalCount = 1,
    cardNumber = "4111111111111111",
    expirationDate = "1230",
))

println("Next billing: ${subscription.nextBillingDate}")

3. Hosted checkout

Create a checkout session and redirect the customer to complete payment.

curl

curl https://api.peakgateway.co/api/v1/checkout-sessions \
  -H "Authorization: Bearer tpk_your_test_key" \
  -H "Content-Type: application/json" \
  -d '{
    "merchantId": "merch_123",
    "amount": 4999,
    "currency": "USD",
    "successUrl": "https://yourapp.com/success",
    "cancelUrl": "https://yourapp.com/cancel",
    "lineItems": [
      { "description": "Pro Plan", "quantity": 1, "unitAmount": 4999 }
    ],
    "expiresAt": "2026-04-01T13:00:00Z"
  }'

Response:

{
  "sessionId": "cs_01j9abc",
  "checkoutUrl": "https://checkout.peakpos.co/cs_01j9abc",
  "status": "PENDING",
  "expiresAt": "2026-04-01T13:00:00Z"
}

Redirect the customer to checkoutUrl. On success, they are sent to your successUrl with ?session_id=cs_01j9abc appended.

TypeScript

const session = await client.checkoutSessions.create({
  merchantId: 'merch_123',
  amount: 4999,
  currency: 'USD',
  successUrl: 'https://yourapp.com/success',
  cancelUrl: 'https://yourapp.com/cancel',
  lineItems: [{ description: 'Pro Plan', quantity: 1, unitAmount: 4999 }],
});

// Redirect customer
res.redirect(session.checkoutUrl);

4. Receiving webhooks

Handle incoming events from Peak Gateway securely.

TypeScript (Express)

import express from 'express';
import { WebhookVerifier } from '@gateway/sdk';

const app = express();
const verifier = new WebhookVerifier(process.env.WEBHOOK_SECRET!);

// Use raw body for signature verification
app.post('/webhooks/gateway', express.raw({ type: 'application/json' }), async (req, res) => {
  const signature = req.headers['x-gateway-signature'] as string | undefined;
  const timestamp = req.headers['x-gateway-timestamp'] as string | undefined;

  if (!signature || !timestamp) {
    console.error('Missing webhook signature or timestamp');
    return res.status(400).send('Bad Request');
  }

  const rawBody = req.body instanceof Buffer ? req.body.toString('utf8') : String(req.body ?? '');

  let event;
  try {
    event = await verifier.verify(rawBody, signature, timestamp);
  } catch (err) {
    console.error('Invalid webhook signature');
    return res.status(401).send('Unauthorized');
  }

  switch (event.eventType) {
    case 'payment.completed':
      console.log(`Payment ${event.transaction.transactionId} succeeded`);
      // update your DB, send receipt, etc.
      break;
    case 'payment.declined':
      console.log(`Payment declined: ${event.transaction.responseMessage}`);
      break;
    case 'subscription.billed':
      console.log(`Subscription ${event.subscription.subscriptionId} billed`);
      break;
    case 'subscription.payment_failed':
      console.log(`Subscription billing failed — notifying customer`);
      break;
  }

  res.sendStatus(200); // Always acknowledge
});

5. Card tokenization

Tokenize a card once and reuse the token for future charges without storing card data on your servers.

Generate a token

const tokenResponse = await client.networkTokens.generate({
  cardNumber: '4111111111111111',
  expirationDate: '1230',
  cardDataSource: 'MANUAL',
});

const token = tokenResponse.token;
// Store `token` in your database — it's safe to persist

Charge with a token

const transaction = await client.transactions.create({
  type: 'SALE',
  amount: 2500,
  currency: 'USD',
  tokenId: token,
  transactionIndustryType: 'EC',
});

Check token status

const status = await client.networkTokens.getStatus(token);
console.log(status.tokenStatus); // "ACTIVE" | "INACTIVE" | "EXPIRED"