Prerequisites
To use PaymentsDS with the JavaScript SDK you need to have:
Node.js 10+NPM or Yarn
Below, you will find examples of the PaymentsDS use cases:
Receive money from a mobile account to a business account
import { Client } from "@paymentsds/mpesa"const client = new Client({apiKey: "<REPLACE>", // API KeypublicKey: "<REPLACE>", // Public KeyserviceProviderCode: "<REPLACE>", // input_ServiceProviderCode})const paymentData = {from: "841234567", // input_CustomerMSISDNreference: "11114", // input_ThirdPartyReferencetransation: "T12344CC", // input_TransactionReferenceamount: "10", // input_Amount}client.receive(paymentData).then(r => {console.log(r.data)}).catch(e => {console.log(e)})
Send money from a business account to a mobile account
import { Client } from "@paymentsds/mpesa"const client = new Client({apiKey: "<REPLACE>", // API KeypublicKey: "<REPLACE>", // Public KeyserviceProviderCode: "<REPLACE>", // input_ServiceProviderCode})const paymentData = {to: "841234567", // input_CustomerMSISDNreference: "11114", // input_ThirdPartyReferencetransation: "T12344CC", // input_TransactionReferenceamount: "10", // input_Amount}client.send(paymentData).then(r => {console.log(r.data)}).catch(e => {console.log(e)})
Send money from a business account to a another business account
import { Client } from "@paymentsds/mpesa"const client = new Client({apiKey: "<REPLACE>", // API KeypublicKey: "<REPLACE>", // Public KeyserviceProviderCode: "<REPLACE>", // input_ServiceProviderCode})const paymentData = {to: "979797", // input_ReceiverPartyCodereference: "11114", // input_ThirdPartyReferencetransation: "T12344CC", // input_TransactionReferenceamount: "10", // input_Amount}client.send(paymentData).then(r => {console.log(r.data)}).catch(e => {console.log(e)})
Revert a transaction
import { Client } from '@paymentsds/mpesa'const client = new Client({apiKey: '<REPLACE>', // API KeypublicKey: '<REPLACE>', // Public KeyserviceProviderCode: '<REPLACE>' // input_ServiceProviderCode,initiatorIdentifier: '<REPLACE>' // input_InitiatorIdentifier,securityIdentifier: '<REPLACE>' // input_SecurityCredential});const reversionData = {reference: '11114', // input_ThirdPartyReferencetransation: 'T12344CC', // input_TransactionIDamount: '10' // input_ReversalAmount};client.revert(reversionData).then(r => {// Handle success scenario}).catch(e =>{// Handle failure scenario});
Query the status of a transaction
import { Client } from '@paymentsds/mpesa'const client = new Client({apiKey: '<REPLACE>', // API KeypublicKey: '<REPLACE>', // Public KeyserviceProviderCode: '<REPLACE>' // input_ServiceProviderCode,initiatorIdentifier: '<REPLACE>' // input_InitiatorIdentifier,securityIdentifier: '<REPLACE>' // input_SecurityCredential});const reversionData = {reference: '11114', // input_ThirdPartyReferencesubject: '5C1400CVRO', // input_QueryReference};client.query(reversionData).then(r => {// Handle success scenario}).catch(e => {// Handle failure scenario});