Integration Examples
Complete integration examples and patterns for common use cases. Use these as starting points for your implementation.
Complete Payment Flow
This example shows the complete flow from tokenization to payment processing.
JavaScript / Node.js Example
Full payment processing flow with error handling
// Complete payment flow example
async function processPayment(cardData, amount, currency) {
try {
// Step 1: Tokenize card data
const tokenResponse = await fetch('https://api.demo-gateway.com/v1/tokens', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_SECRET_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
card_number: cardData.number,
expiry_month: cardData.expiryMonth,
expiry_year: cardData.expiryYear,
cvv: cardData.cvv,
cardholder_name: cardData.name,
}),
});
const { token_id } = await tokenResponse.json();
// Step 2: Create payment with token
const paymentResponse = await fetch('https://api.demo-gateway.com/v1/payments', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_SECRET_KEY',
'Content-Type': 'application/json',
'Idempotency-Key': generateIdempotencyKey(),
},
body: JSON.stringify({
token_id,
amount,
currency,
description: 'Order #12345',
}),
});
const payment = await paymentResponse.json();
// Step 3: Handle response
if (payment.status === 'succeeded') {
return { success: true, payment_id: payment.payment_id };
} else if (payment.status === 'pending') {
// Poll for status or wait for webhook
return { success: true, pending: true, payment_id: payment.payment_id };
} else {
return { success: false, error: payment.error };
}
} catch (error) {
return { success: false, error: error.message };
}
}Integration Patterns
One-Time Payment
Process a single payment for an order. Tokenize card, create payment, handle result.
View API →Saved Cards
Save customer cards for faster checkout. Use vaulting API to store tokens.
View API →Webhook Processing
Handle payment status updates via webhooks for real-time notifications.
View Guide →Refund Processing
Process refunds for completed payments, full or partial amounts.
View API →Error Handling
Best Practices
Handle All Error Cases
- Network errors and timeouts
- API errors (4xx, 5xx responses)
- Invalid card data
- Insufficient funds
- 3D Secure requirements
User-Friendly Messages
Don't expose technical error details to users. Map error codes to friendly messages.
Retry Logic
Implement exponential backoff for transient errors. Use idempotency keys for safe retries.