This documentation page contains everything you need to know about integrating to Klasha’s Payment Gateway. Once integration is done, you can begin to process payments from anywhere in the world in seconds.
Whilst you start your integration, please remember to reach out to our Compliance team to go through the KYB process and get your business verified. This is a mandatory process before you can start using our production platform.
If you are going to be implementing the direct charge API, you would require additional verification as this is only available to businesses that are PCI-DSS certified.
Payment collection
As a business owner, here is a way we make it possible for you to collect payments from customers all over the world:
In all the following APIs, you can simply substitute the desired currency to the path variable {{gateway}}.
Before you begin
Get your API keys
Find your keys on the Klasha Dashboard → Settings → Generate API Keys (here)
Klasha Dashboard, Generate API Keys page
Encryption algorithm
Since we are treating payment data, the communication from merchant to APIs must be encrypted. We are using a standard 3DES technology with Padding. Please find some code snippets below for the 3DES encryption:
#Ensure you have the pyDes library installed (pip install pyDes)from pyDes import triple_des, PAD_PKCS5, CBCimport base64defencrypt_3des(data,key): des =triple_des(key, CBC, pad=None, padmode=PAD_PKCS5) encrypted_data = des.encrypt(data)return base64.b64encode(encrypted_data).decode()# Example usgae:if__name__=="__main__":# key (24 bytes) key =b'Use your 24 bytes key here' data ="Hello, Klasha!" encrypted_text =encrypt_3des(data, key)print("Encrypted text:", encrypted_text)
function encrypt3DES($messageToEncrypt, $secret) {
$iv = substr($secret, 0, 8); // Get the IV (first 8 bytes of the secret key)
$cipher = "des-ede3-cbc"; // 3DES encryption with CBC mode
$options = OPENSSL_RAW_DATA;
// Encrypt the message
$encryptedMessageBytes = openssl_encrypt($messageToEncrypt, $cipher, $secret, $options, $iv);
return base64_encode($encryptedMessageBytes);
}
// NOTE: You need crypto-js.min.js to use CryptoJSfunctionencrypt3DES(messageToEncrypt, secret) {try {if (secret.length<24) {console.error("Secret key must be at least 24 characters long.");return; }// Use the first 24 characters of the secret key for 3DESconstkey=CryptoJS.enc.Utf8.parse(secret.substring(0,24));// Use the first 8 characters of the secret key for the IVconstiv=CryptoJS.enc.Utf8.parse(secret.substring(0,8));// Encrypt the messageconstencrypted=CryptoJS.TripleDES.encrypt(messageToEncrypt, key, { iv: iv, mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7, });// Return the Base64-encoded encrypted messagereturnencrypted.toString(); } catch (error) {console.error(error); }}
constcrypto=require('crypto');/** * Encrypt a message using 3DES with CBC mode and PKCS5 padding. * @param{string} messageToEncrypt - The plaintext message to encrypt. * @param{Buffer} secret - The 24-byte secret key. * @returns{string} The Base64 encoded encrypted message. */functionencrypt3DES(messageToEncrypt, secret) {if (secret.length!==24) {thrownewError('Secret must be exactly 24 bytes.'); }// Derive the IV from the first 8 bytes of the secret keyconstiv=secret.slice(0,8); // First 8 bytes are used as IV// Create cipherconstcipher=crypto.createCipheriv('des-ede3-cbc', secret, iv);// Encrypt the messagelet encrypted =cipher.update(messageToEncrypt,'utf8','base64'); encrypted +=cipher.final('base64');return encrypted;}// Sample UsagefunctiontestEncrypt3DES() {constjsonObject= { id:1, name:'John Doe', role:'Senior Backend Engineer', skills: ['Java','Node.js','AWS'] };// Convert JSON object to stringconstmessage=JSON.stringify(jsonObject);// 24-byte secret keyconstsecretKey='24-byte key'; // replace with your 24-byte encryption keyconsole.log('Original JSON Object:', jsonObject);console.log('Secret Key (Base64):',secretKey.toString('base64'));// Encrypt the JSON stringconstencryptedMessage=encrypt3DES(message, secretKey);console.log('Encrypted Message (Base64):', encryptedMessage);}testEncrypt3DES();
Card payments
To accept a card payment, you need to integrate with the following flow:
First, initiate the card payment
Then charge the card
And at the end validate the payment (to provide OTP and/or PIN)
You can find more information on the Postman link as well as other APIs.
You’d need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.
This is for cards that are covered by 3DS flows.{"tx_ref":"klasha-add-bank-card-1697954ttt947238348","redirectUrl":"https://coreflutterwavestaging.com/flwmpgs/trxauth?hid=712b85a8542649e68c19b1c80d81aadc","data": {"meta": {"authorization": {"mode":"redirect","redirect":"https://coreflutterwavestaging.com/flwmpgs/trxauth?hid=712b85a8542649e68c19b1c80d81aadc" } } }}
You can find more information on the Postman link as well as other APIs.
You’d need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.
Headers
Name
Value
Content-Type
application/json
x-auth-token
Your merchant public key
Request Body (encrypted)
{"message":"encrypted-body"}
Request Body (plain)
The tx-ref is contained in the successful response of the initiate payment call.
{"message":"success","error":null,"data": {"tx_ref":"test910-on2007u047e-2910tytrr76","message":"Please enter the OTP sent to your mobile number 080****** and email te**@rave**.com","status":"pending" }}
You can find more information on the Postman link as well as other APIs.
You would need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.
Headers
Name
Value
Content-Type
application/json
x-auth-token
Your merchant public key
Request Body (encrypted)
{"message":"encrypted-body"}
Request Body (plain)
The tx-ref is contained in the successful response of the initiate payment call.
You can find more information on the Postman link as well as other APIs.
You’d need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.
{"message":"success","error":null,"data": {"tx_ref":"MC-15856767458ffdgddfefhqweert","meta": {"authorization": {"mode":"banktransfer","transfer_note":"Please make a bank transfer to Klasha - Collection","transfer_amount":500.0,"transfer_bank":"WEMA BANK","account_expiration":"2024-05-23T13:55:13.105","transfer_account":"8574551243" } },"message":"Charge initiated","status":"success" }}
{"status":"success","message":"Charge initiated","data": {"tx_ref":"test_xfer_29071707","message":"Transaction in progress","meta": {"authorization": {"mode":"redirect","redirect":"https://stagingpay.ozow.com/b1c1bb59-bf46-42ee-bc76-ccdf63e3f453/Secure","validate_instructions":"" } } }}
You can find more information on the Postman link as well as other APIs.
You would need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.
{"message":"success","error":null,"data": {"tx_ref":"test910-on2007u047e-291076","data": {"amount":"10","charged_amount":"10","currency":"KES","customer": {"name":"John Doe","phone_number":"+254710000000","email":"email@klasha.com" },"status":"pending" },"message":"Transaction in progress","status":"pending" }}
{"tx_ref":"test910-on2007u047e-mcnvmlbhgmjfo","data":null,"message":"Invalid phone number format","status":"error"}
Initiate a refund
POST{{env_url}}/nucleus/refund/initiate/v3
You would need to pass, as header the x-auth-token. This can be obtained from your merchant dashboard → Settings → Generate API keys → Merchant public key.