This is a Javascript SDK library for Ecocash Open API.
npm i ecocashNote
You can use whichever package manager you prefer, it should work.
Run build and start scripts concurrently:
npm run devOr run build and start scripts separately:
npm run build
npm run startThe basic follow is as follows:
You can create an Ecocash object as follows:
const Ecocash = require("ecocash");
const merchant = new Ecocash("<apiKey>", "<merchant code>");const response = await merchant.initPayment("26377854266", 20.05, "bread");Response:
{
phone: '26377854266',
amount: 20.05,
reason: 'bread',
currency: 'USD',
sourceReference: '325a802f-943e-47c2-addf-010285f09cea'
}You can poll for transaction status as follows:
Known statuses(for now):
SUCCESSPENDING_VALIDATION
const transaction = await merchant.lookupTransaction(response.sourceReference, response.phone);
if (transaction.paymentSuccess) {
console.log("Transaction successful");
}Response:
{
amount: { amount: 100, currency: 'USD' },
customerMsisdn: '263778548832',
reference: '325a802f-943e-47c2-addf-010285f09cea',
ecocashReference: 'MP250908.1537.A22242',
status: 'SUCCESS',
transactionDateTime: '2025-09-08 15:37:16'
}Note
The default lookupTransaction method requires you to add poll logic by yourself. But with the pollTransaction method, you can poll for transaction status as follows:
merchant.pollTransaction(response, strategy, options);Where:
- response: The response from the
initPaymentmethod - strategy: The strategy to use for polling (SIMPLE, INTERVAL, BACKOFF)
- options: The options to use for polling (interval, backoff, multiplier, sleep)
NB: The default strategy is INTERVAL.
NB: Options are optional and default to { interval: 10, multiplier: 2, sleep: 1000 }.
Interval strategy:
// Poll for transaction status with interval
const transaction = await merchant.pollTransaction(response, PollStrategies.INTERVAL, { interval: 10 });
if (transaction.paymentSuccess) {
console.log("Transaction successful");
}Backoff strategy:
// Poll for transaction status with backoff
const transaction = await merchant.pollTransaction(response, PollStrategies.BACKOFF, { multiplier: 2, interval: 15, sleep: 2000 });
if (transaction.paymentSuccess) {
console.log("Transaction successful");
}Simple strategy:
// Poll for transaction status with simple strategy
const transaction = await merchant.pollTransaction(response, PollStrategies.SIMPLE);
if (transaction.paymentSuccess) {
console.log("Transaction successful");
}const response = await merchant.refundPayment({
reference: "reference",
phone: "263778548266",
amount: 20.05,
clientName: "bread",
reason: "The bread was rotten"
});const Ecocash = require("ecocash");
const merchant = new Ecocash("apiKey", "merchant");
const response = await merchant.initPayment("26377854266", 20.05, "bread");
console.log(response);
// Poll for transaction status
const transaction = await merchant.pollTransaction(response);
if (transaction.paymentSuccess) {
console.log("Transaction successful");
}To go live, you can set the mode to "live" as follows:
const merchant = new Ecocash("<apiKey>", "<merchant>", "live"); OR
const merchant = new Ecocash("<apiKey>", "<merchant>");
merchant.setLiveMode();Contributions are welcome! Please read our contributing guidelines for more information.