ecocash
Version: 
Ecocash Javascript SDK
200 lines (135 loc) • 4.15 kB
Markdown
This is a Javascript SDK library for Ecocash Open API.
```bash
npm i ecocash
```
> [!NOTE]
> You can use whichever package manager you prefer, it should work.
Run build and start scripts concurrently:
```bash
npm run dev
```
Or run build and start scripts separately:
```bash
npm run build
npm run start
```
The basic follow is as follows:
You can create an Ecocash object as follows: 
```javascript
const Ecocash = require("ecocash");
const merchant = new Ecocash("<apiKey>", "<merchant code>");
```
```javascript
const response = await merchant.initPayment("26377854266", 20.05, "bread");
```
Response:
```javascript
{
    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):
- `SUCCESS`
- `PENDING_VALIDATION`
```javascript
const transaction = await merchant.lookupTransaction(response.sourceReference, response.phone);
if (transaction.paymentSuccess) {
    console.log("Transaction successful");
}
``` 
Response:
```javascript
{
    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:
```javascript
merchant.pollTransaction(response, strategy, options);
```
Where:
- response: The response from the `initPayment` method
- 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:
```javascript
// 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:
```javascript
// 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:
```javascript
// Poll for transaction status with simple strategy
const transaction = await merchant.pollTransaction(response, PollStrategies.SIMPLE);
if (transaction.paymentSuccess) {
    console.log("Transaction successful");
}
```
```javascript
const response = await merchant.refundPayment({
    reference: "reference",
    phone: "263778548266",
    amount: 20.05,
    clientName: "bread",
    reason: "The bread was rotten"
});
```
```javascript
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:
```javascript
const merchant = new Ecocash("<apiKey>", "<merchant>", "live");     
``` 
OR
```javascript
const merchant = new Ecocash("<apiKey>", "<merchant>");     
merchant.setLiveMode();
```
Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) for more information.
- [Ecocash](https://ecocash.co.zw/)
- [LordSky](https://pypi.org/user/lordskyzw/)