react-klasha
Version:
React hooks and components for accepting payments with the Klasha payment gateway
139 lines (138 loc) • 4.85 kB
TypeScript
/**
* The `kit` object handed to the Klasha client as the 8th constructor argument.
*
* The fields the shipped `https://js.klasha.com/pay.js` actually reads are
* `businessId`, `currency`, `redirect_url`, `email`, `phone`, `productType`,
* `amount`, `sourceAmount`, `callBack` and `tx_ref`. Anything else is ignored.
*
* `pay.js` **mutates** the object it is given (it assigns `businessId`,
* `currency` and `redirect_url`), so this library builds a fresh `kit` for every
* payment instead of forwarding the caller's object.
*/
export interface KlashaKitOptions {
/** Merchant business id. Assigned by `pay.js` itself if omitted. */
businessId?: string | number;
/** Currency the customer is charged in. */
currency?: string;
/** Where the gateway sends the customer after payment. Assigned by `pay.js`. */
redirect_url?: string;
/** Customer email address. */
email?: string;
/**
* Customer phone number.
*
* This is the key `pay.js` reads — **not** `phone_number`.
*/
phone?: string;
/**
* @deprecated `pay.js` never reads this key, so in 0.0.x the phone number was
* silently dropped. Still accepted, and copied into `phone`.
*/
phone_number?: string;
/** Customer full name. Not read by `pay.js`; forwarded for convenience. */
fullname?: string;
/** Product/payment type shown on the checkout. */
productType?: string;
/** @deprecated `pay.js` reads `productType`. Copied into `productType`. */
paymentType?: string;
/** Amount in the destination currency. Defaults to the top-level `amount`. */
amount?: number;
/** Amount in the source currency, when it differs from `amount`. */
sourceAmount?: number;
/** Called by the gateway when the payment flow completes. */
callBack?: Function;
/** Unique transaction reference. Interpolated into the redirect URL. */
tx_ref?: string;
}
export interface KlashaOptionsProps {
/**
* Use the Klasha test environment.
*
* This flag is the **only** thing that selects the environment: it is
* forwarded as the 9th constructor argument. Versions 0.0.x never passed it,
* so `isTestMode` integrations transacted against the live gateway.
*/
isTestMode?: boolean;
/**
* Merchant's API key (found on the Klasha dashboard).
*/
merchantKey: string;
/**
* Merchant's business id (found on the Klasha dashboard).
*/
businessId: string | number;
/**
* The amount to be paid by the customer.
*/
amount: number;
/**
* The currency the merchant is settled in. Defaults to `NGN`.
*/
destinationCurrency?: string;
/**
* The currency the customer is charged in. Defaults to `NGN`.
*/
sourceCurrency?: string;
/**
* Unique transaction reference. One is generated when omitted.
*/
tx_ref?: string;
/**
* Full name of the customer.
*/
fullname?: string;
/**
* Email address of the customer.
*/
email?: string;
/**
* Phone number of the customer.
*/
phone?: string;
/**
* @deprecated Use `phone`. Accepted as an alias for backwards compatibility.
*/
phone_number?: string;
/**
* URL the gateway calls once the transaction completes.
*/
callbackUrl?: string;
/**
* Description of what is being paid for.
*/
paymentDescription?: string;
/**
* Extra data to keep alongside the transaction.
*/
metadata?: Record<string, any>;
/**
* Extra `kit` fields forwarded to the Klasha client.
*/
kit?: KlashaKitOptions;
/**
* Id of the element the checkout renders into. A unique one is created per
* hook instance when omitted; supply your own only when the checkout has to
* mount somewhere specific.
*/
containerId?: string;
'data-custom-button'?: string;
}
/** The instance `new window.KlashaClient(...)` returns. */
export interface KlashaClientInstance {
init(): void;
}
/**
* The constructor `https://js.klasha.com/pay.js` attaches to `window`.
*
* The argument order is verbatim from the shipped script. Note that the 6th
* parameter is named `countryCode` inside `pay.js` but receives the destination
* currency — that is the mapping the gateway expects, and it is what every
* working integration sends. Klasha's own documentation page describes a
* different, incorrect signature.
*/
export type KlashaClientConstructor = new (merchantKey: string, businessId: string | number, amount: number, containerId: string, callbackUrl: string, countryCode: string, sourceCurrency: string, kit: KlashaKitOptions, isTestMode: boolean) => KlashaClientInstance;
declare global {
interface Window {
KlashaClient?: KlashaClientConstructor;
}
}