React SDK
Accept payments in your React app with the Credo React SDK.
The official React SDK for integrating Credo payments into your web application.
Installation
npm install react-credo
# or
yarn add react-credoQuick start
Import the hook and configure your payment:
import { useCredoPayment } from 'react-credo';
const PaymentComponent = () => {
const config = {
key: '0PUBxxxxxxxxxxxxxxxxxxxxxxxx', // Your public key
customerFirstName: 'Ciroma',
customerLastName: 'Chukwuma',
email: 'ciroma@example.com',
amount: 100000, // NGN 1,000 in kobo
currency: 'NGN',
bearer: 0, // 0 = customer pays fee, 1 = merchant pays
reference: 'unique-ref-123', // Generate unique reference per transaction
customerPhoneNumber: '2348032698425',
callbackUrl: 'https://yoursite.com/payment/success',
onClose: () => console.log('Payment closed'),
callBack: (response) => {
console.log('Payment successful:', response);
// Verify transaction on your backend
}
};
const initializePayment = useCredoPayment(config);
return (
<button onClick={initializePayment}>
Pay Now
</button>
);
};Configuration
| Prop | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Your Credo public key (0PUB... for test, 1PUB... for live) |
amount | number | Yes | Amount in lowest unit (kobo/cents) |
email | string | Yes | Customer email address |
customerFirstName | string | Yes | Customer first name |
customerLastName | string | Yes | Customer last name |
currency | string | No | NGN, USD (default: NGN) |
bearer | number | No | 0 (customer pays fee) or 1 (merchant pays) |
reference | string | No | Unique transaction reference (auto-generated if not provided) |
customerPhoneNumber | string | No | Customer phone number |
callbackUrl | string | No | URL for redirect after payment |
metadata | object | No | Custom transaction data |
onClose | () => void | No | Called when payment modal closes |
callBack | (response) => void | No | Called when payment completes |
Response object
The callBack receives:
{
transRef: string; // Transaction reference for verification
status: string; // Transaction status
amount: number; // Amount paid
currency: string; // Currency code
callbackUrl: string; // Your callback URL
}Important
Always verify the transaction status on your backend using transRef before granting access to goods or services.
Verify transaction
const handlePayment = async (response) => {
const { transRef } = response;
// Call your backend to verify
const result = await fetch(`https://your-api.com/verify/${transRef}`);
const data = await result.json();
if (data.status === 'successful') {
// Grant access
window.location.href = '/payment-success';
}
};Backend verification:
const verify = await fetch(
`https://api.credocentral.com/transaction/${transRef}/verify`,
{ headers: { 'Authorization': 'YOUR_SECRET_KEY' } }
);Environments
- Test: Public keys starting with
0PUB(usesapi.credodemo.com) - Live: Public keys starting with
1PUB(usesapi.credocentral.com)
Before deploying
Remember to switch from your test key (0PUB...) to your live key (1PUB...) when deploying to production.
Metadata
Attach custom data to transactions:
const config = {
// ... other config
metadata: {
customFields: [
{ variable_name: 'orderId', value: 'ORD-123', display_name: 'Order ID' },
{ variable_name: 'userId', value: 'user_456', display_name: 'User ID' }
]
}
};Next steps
Was this page helpful?
Last updated on
