Payment Gateway API Documentation
Overview
The Payment Gateway API allows you to process payments, manage transactions, and handle refunds programmatically. This RESTful API uses JSON for request and response payloads.
Base URL: https://api.paymentgateway.com/v1
Authentication: Bearer Token
Getting Started
Prerequisites
Before you begin, ensure you have:
- An active account with Payment Gateway
- API credentials (API Key and Secret)
- Basic understanding of RESTful APIs
- A development environment capable of making HTTPS requests
Quick Start
- Sign up for an API account
- Generate your API credentials from the dashboard
- Make your first API call using the examples below
- Review the response and error codes
Authentication
All API requests require authentication using Bearer tokens. Include your API key in the Authorization header:
curl -X GET https://api.paymentgateway.com/v1/transactions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json"
Generating an API Key
const response = await fetch('https://api.paymentgateway.com/v1/auth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'your_client_id',
client_secret: 'your_client_secret'
})
});
const data = await response.json();
console.log(data.access_token);
Core Endpoints
Create Payment
Process a new payment transaction.
Endpoint: POST /payments
Request Body:
{
"amount": 5000,
"currency": "USD",
"payment_method": "card",
"customer": {
"email": "customer@example.com",
"name": "John Doe"
},
"card": {
"number": "4242424242424242",
"exp_month": 12,
"exp_year": 2025,
"cvc": "123"
},
"description": "Order #12345"
}
Response:
{
"id": "pay_1234567890",
"status": "succeeded",
"amount": 5000,
"currency": "USD",
"created": 1699564800,
"customer": {
"id": "cus_abc123",
"email": "customer@example.com"
}
}
List Transactions
Retrieve a paginated list of all transactions.
Endpoint: GET /transactions
Query Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| limit | integer | No | Number of results (1-100, default 10) |
| offset | integer | No | Number of items to skip (default 0) |
| status | string | No | Filter by status: pending, succeeded, failed |
| from_date | string | No | ISO 8601 date format (YYYY-MM-DD) |
| to_date | string | No | ISO 8601 date format (YYYY-MM-DD) |
Example Request:
import requests
url = "https://api.paymentgateway.com/v1/transactions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
params = {
"limit": 20,
"status": "succeeded"
}
response = requests.get(url, headers=headers, params=params)
print(response.json())
Refund Payment
Issue a full or partial refund for a completed payment.
Endpoint: POST /payments/{payment_id}/refund
Path Parameters:
payment_id(string, required): The ID of the payment to refund
Request Body:
{
"amount": 2500,
"reason": "customer_request"
}
Data Models
Payment Object
| Field | Type | Description |
|---|---|---|
| id | string | Unique identifier for the payment |
| amount | integer | Amount in cents (e.g., 5000 = $50.00) |
| currency | string | Three-letter ISO currency code (USD, EUR) |
| status | string | Payment status: pending, succeeded, failed |
| created | integer | Unix timestamp of creation |
| description | string | Optional description of the payment |
Customer Object
| Field | Type | Description |
|---|---|---|
| id | string | Unique customer identifier |
| string | Customer’s email address | |
| name | string | Customer’s full name |
| phone | string | Customer’s phone number |
Error Codes
The API uses standard HTTP status codes and returns detailed error messages:
| Status Code | Error Type | Description |
|---|---|---|
| 200 | Success | Request succeeded |
| 400 | Bad Request | Invalid request parameters |
| 401 | Unauthorized | Invalid or missing API key |
| 403 | Forbidden | Insufficient permissions |
| 404 | Not Found | Resource doesn’t exist |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Error | Server error |
Error Response Format:
{
"error": {
"type": "invalid_request_error",
"code": "amount_too_small",
"message": "Amount must be at least $0.50 USD",
"param": "amount"
}
}
Rate Limiting
API requests are limited to:
- 100 requests per minute for standard accounts
- 500 requests per minute for premium accounts
Rate limit information is included in response headers:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1699564800
Webhooks
Subscribe to events to receive real-time updates about payments and transactions.
Supported Events
payment.succeededpayment.failedrefund.createdcustomer.created
Webhook Payload Example
{
"id": "evt_1234567890",
"type": "payment.succeeded",
"created": 1699564800,
"data": {
"object": {
"id": "pay_1234567890",
"amount": 5000,
"currency": "USD",
"status": "succeeded"
}
}
}
Verifying Webhook Signatures
const crypto = require('crypto');
function verifyWebhookSignature(payload, signature, secret) {
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
}
SDK Libraries
Official SDKs are available for multiple languages:
- Node.js:
npm install paymentgateway-node - Python:
pip install paymentgateway - Ruby:
gem install paymentgateway - PHP:
composer require paymentgateway/paymentgateway-php
Node.js Example
const PaymentGateway = require('paymentgateway-node');
const gateway = new PaymentGateway('YOUR_API_KEY');
async function createPayment() {
const payment = await gateway.payments.create({
amount: 5000,
currency: 'USD',
payment_method: 'card',
customer: {
email: 'customer@example.com'
}
});
console.log(payment);
}
Testing
Use these test card numbers in sandbox mode:
| Card Number | Result |
|---|---|
| 4242424242424242 | Successful payment |
| 4000000000000002 | Card declined |
| 4000000000009995 | Insufficient funds |
| 4000000000000069 | Expired card |
Sandbox Base URL: https://sandbox.paymentgateway.com/v1
Architecture Diagram
Figure 1: High-level architecture showing client applications, API gateway, payment processor, and database interactions.
Support
For additional help:
- Documentation: https://docs.paymentgateway.com
- Support Email: support@paymentgateway.com
- Status Page: https://status.paymentgateway.com
- Community Forum: https://community.paymentgateway.com
Changelog
Version 1.2.0 (2024-11-15)
- Added support for Apple Pay and Google Pay
- Improved error messages for declined cards
- New webhook event:
customer.updated
Version 1.1.0 (2024-10-01)
- Added refund endpoint
- Increased rate limits for premium accounts
- Bug fixes and performance improvements
Version 1.0.0 (2024-09-01)
- Initial release
- Core payment processing
- Customer management
- Basic webhook support