UNPKG

paychangu-js

Version:

Paychangu JavaScript SDK for easy integration of payment services

182 lines (133 loc) 3.37 kB
# PayChangu JavaScript SDK The official JavaScript SDK for integrating PayChangu payment gateway into your web applications. ## Installation Install the package using npm: ```bash npm install paychangu-js ``` Or using yarn: ```bash yarn add paychangu-js ``` ## Usage Import the necessary modules from the SDK: ```javascript import { PopupSDK, LevelSDK, VerificationSDK, generateTxRef } from 'paychangu-js'; ``` ## Features ### Popup SDK The Popup SDK allows you to integrate an inline popup payment flow. ```javascript const popupSDK = PopupSDK; // Configure and initiate the popup popupSDK.initiate(config); ``` ### Level SDK The Level SDK provides functionality for level-based payments. ```javascript const levelSDK = LevelSDK; // Make a level-based payment const response = await levelSDK.makePayment(levelConfig); ``` ### Verification SDK The Verification SDK allows you to verify transactions. ```javascript const verificationSDK = VerificationSDK; // Verify a transaction const verificationResponse = await verificationSDK.verifyTransaction(txRef); ``` ## Configuration ### PaychanguConfig ```typescript interface PaychanguConfig { public_key: string; tx_ref: string; amount: number; currency: string; callback_url: string; return_url: string; customer: Customer; customization: Customization; meta?: Record<string, unknown>; } ``` ### LevelConfig ```typescript interface LevelConfig { amount: number; currency: string; email: string; first_name: string; last_name: string; callback_url: string; return_url: string; tx_ref: string; customization?: { title?: string; description?: string; }; meta?: Record<string, unknown>; } ``` ## Helper Functions ### generateTxRef Generate a unique transaction reference: ```javascript const txRef = generateTxRef(); ``` ## Types The SDK exports the following types for TypeScript users: - `PaychanguConfig` - `LevelConfig` - `LevelResponse` - `VerificationResponse` - `Customer` - `Customization` ## Examples ### Initiating a Popup Payment ```javascript import { PopupSDK, generateTxRef } from 'paychangu-js'; const config = { public_key: 'your_public_key', tx_ref: generateTxRef(), amount: 1000, currency: 'MWK', callback_url: 'https://your-callback-url.com', return_url: 'https://your-return-url.com', customer: { email: 'customer@example.com', first_name: 'John', last_name: 'Doe' }, customization: { title: 'Payment for Product X', description: 'This is a test payment' } }; PopupSDK.initiate(config); ``` ### Making a Level-based Payment ```javascript import { LevelSDK, generateTxRef } from 'paychangu-js'; const levelConfig = { amount: 1000, currency: 'MWK', email: 'customer@example.com', first_name: 'John', last_name: 'Doe', callback_url: 'https://your-callback-url.com', return_url: 'https://your-return-url.com', tx_ref: generateTxRef() }; const response = await LevelSDK.makePayment(levelConfig); console.log(response); ``` ### Verifying a Transaction ```javascript import { VerificationSDK } from 'paychangu-js'; const txRef = 'your_transaction_reference'; const verificationResponse = await VerificationSDK.verifyTransaction(txRef); console.log(verificationResponse); ``` ## Support For support or questions, please contact our support team at dev@paychangu.com.