Payment Services
The Payment Services module allows you to process payments through multiple channels, including checkout pages, direct card charges, bank transfers, and mobile money.
Available Payment Services
- Checkout - Hosted payment page
- Direct Charge - Direct payment processing
Checkout
The Checkout service provides a hosted payment page that you can redirect your customers to.
Initialize Payment Checkout
Creates a payment checkout session and returns a URL for the customer to complete the payment.
const checkoutResponse = await client.payment.checkout.initialize({
first_name: "John",
last_name: "Doe",
email: "johndoe@gmail.com",
amount: "100",
currency: "NGN",
reference: client.generateReference(),
redirectUrl: "https://yourwebsite.com/callback",
title: "Payment for Order",
description: "Order #12345",
logoUrl: "https://yourwebsite.com/logo.png"
});
// Redirect the customer to the checkout URL
console.log('Checkout URL:', checkoutResponse.data.checkout_url);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
first_name | string | Yes | Customer’s first name |
last_name | string | Yes | Customer’s last name |
email | string | Yes | Customer’s email address |
amount | string | Yes | Payment amount |
currency | string | Yes | Payment currency (e.g., NGN ) |
reference | string | Yes | Unique transaction reference |
redirectUrl | string | Yes | Callback URL after payment |
title | string | Yes | Payment title |
description | string | Yes | Payment description |
logoUrl | string | No | URL of your logo |
Response
{
status: "success",
message: "Checkout initialized successfully",
data: {
checkout_url: "https://checkout.cashonrails.com/pay/abcdef12345",
transaction_reference: "COR_1234567890",
amount: "100",
currency: "NGN"
}
}
Verify Transaction
After a customer completes payment, you should verify the transaction status:
const verificationResponse = await client.payment.checkout.verify('transaction_reference');
if (verificationResponse.data.status === 'successful') {
// Process order fulfillment
console.log('Payment successful!');
} else {
// Handle failed payment
console.log('Payment failed:', verificationResponse.data.status);
}
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
transaction_reference | string | Yes | Reference of the transaction to verify |
Response
{
status: "success",
message: "Transaction verified",
data: {
reference: "COR_1234567890",
amount: "100",
currency: "NGN",
status: "successful", // or "failed", "pending"
payment_method: "card",
paid_at: "2023-05-15T10:30:00Z",
customer: {
email: "johndoe@gmail.com",
name: "John Doe"
}
}
}
Direct Charge
The Direct Charge service allows you to process payments directly without redirecting customers to a hosted page.
Card Payments
Process a payment directly using card details:
const cardPayment = await client.payment.directCharge.card({
email: "test@example.com",
amount: "100",
currency: "NGN",
card: {
number: "4111111111111111",
cvv: "123",
expiryMonth: "12",
expiryYear: "25",
pin: "1234"
}
});
console.log('Card Payment Response:', cardPayment);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
email | string | Yes | Customer’s email address |
amount | string | Yes | Payment amount |
currency | string | Yes | Payment currency (e.g., NGN ) |
card.number | string | Yes | Card number |
card.cvv | string | Yes | Card CVV |
card.expiryMonth | string | Yes | Card expiry month (MM) |
card.expiryYear | string | Yes | Card expiry year (YY) |
card.pin | string | Yes | Card PIN |
Bank Transfers
Initialize a direct bank transfer:
const bankTransfer = await client.payment.directCharge.initBankTransfer({
account_number: "1234567890",
bank_code: "000013",
amount: "5000",
currency: "NGN",
email: "johndoe@gmail.com",
reference: client.generateReference()
});
console.log('Bank Transfer Response:', bankTransfer);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
account_number | string | Yes | Customer’s account number |
bank_code | string | Yes | Bank code |
amount | string | Yes | Transfer amount |
currency | string | Yes | Transfer currency |
email | string | Yes | Customer’s email |
reference | string | Yes | Unique reference |
Mobile Money
Process a payment using mobile money services:
const mobileMoneyPayment = await client.payment.directCharge.chargeWithPhone({
phone_number: "08012345678",
provider: "mtn",
amount: "2000",
currency: "NGN",
email: "johndoe@gmail.com",
reference: client.generateReference()
});
console.log('Mobile Money Payment Response:', mobileMoneyPayment);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
phone_number | string | Yes | Customer’s phone number |
provider | string | Yes | Mobile money provider (e.g., mtn ) |
amount | string | Yes | Payment amount |
currency | string | Yes | Payment currency |
email | string | Yes | Customer’s email |
reference | string | Yes | Unique reference |
List Available Banks
Retrieve a list of available banks for payment processing:
const banks = await client.payment.directCharge.listAvailableBanks();
console.log('Available Banks:', banks.data);
Response
{
status: "success",
message: "Banks retrieved successfully",
data: [
{
code: "000013",
name: "First Bank of Nigeria"
},
{
code: "000014",
name: "Guaranty Trust Bank"
},
// More banks...
]
}
Best Practices for Payments
- Always verify transactions after completion
- Generate unique references for each transaction
- Store transaction references in your database
- Handle failed payments gracefully
- Implement proper error handling for all payment operations
- Use webhooks for asynchronous payment notifications
Error Handling for Payments
try {
const checkout = await client.payment.checkout.initialize({
// Payment details
});
// Process successful checkout
console.log('Checkout URL:', checkout.data.checkout_url);
} catch (error) {
// Handle payment errors
console.error('Payment initialization failed:', error.message);
if (error.code === 'INVALID_AMOUNT') {
console.error('Invalid payment amount');
} else if (error.code === 'CURRENCY_NOT_SUPPORTED') {
console.error('Currency not supported');
}
}
For more details on error handling, see the Error Handling documentation.
Related Resources
- Transfers - Send money to bank accounts
- Reserved Accounts - Create and manage dedicated accounts
- Customer Management - Manage customer profiles