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-credo

Quick 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

PropTypeRequiredDescription
keystringYesYour Credo public key (0PUB... for test, 1PUB... for live)
amountnumberYesAmount in lowest unit (kobo/cents)
emailstringYesCustomer email address
customerFirstNamestringYesCustomer first name
customerLastNamestringYesCustomer last name
currencystringNoNGN, USD (default: NGN)
bearernumberNo0 (customer pays fee) or 1 (merchant pays)
referencestringNoUnique transaction reference (auto-generated if not provided)
customerPhoneNumberstringNoCustomer phone number
callbackUrlstringNoURL for redirect after payment
metadataobjectNoCustom transaction data
onClose() => voidNoCalled when payment modal closes
callBack(response) => voidNoCalled 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 (uses api.credodemo.com)
  • Live: Public keys starting with 1PUB (uses api.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

On this page