
Node.JS SDK for Paynow Zimbabwe's API
Sign in to Paynow and get integration details
Before you can start making requests to Paynow's API, you need to get an integration ID and integration Key from Paynow.
See Documentation Generating Integration Key and Viewing integration ID
Documentation
See the Paynow QuickStart.
Prerequisites
This library has a set of prerequisites that must be met for it to work
- Node version 0.6.0 and above
- NPM (node's package manager, used to install the node library)
Installation
Install the library using NPM or yarn
$ npm install --save paynow
$ yarn add paynow
Usage example
Importing library
const { Paynow } = require("paynow");
Create an instance of the Paynow class optionally setting the result and return url(s)
let paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");
paynow.resultUrl = "http://example.com/gateways/paynow/update";
paynow.returnUrl = "http://example.com/return?gateway=paynow";
The Integration ID and Key can be optionally loaded from PAYNOW_INTEGRATION_ID and PAYNOW_INTEGRATION_KEY environment variables (respectively). An instance of the Paynow class can then be created using the following:
let paynow = new Paynow();
Create a new payment passing in the reference for that payment (e.g invoice id, or anything that you can use to identify the transaction.
let payment = paynow.createPayment("Invoice 35");
You can then start adding items to the payment
payment.add("Bananas", 2.5);
payment.add("Apples", 3.4);
Once you're done building up your cart and you're finally ready to send your payment to Paynow, you can use the send method in the paynow object.
paynow.send(payment);
The send method will return a Promise<InitResponse>, the InitResponse object being the response from Paynow and it will contain some useful information like whether the request was successful or not. If it was, for example, it contains the url to redirect the user so they can make the payment. You can view the full list of data contained in the response in our wiki
If request was successful, you should consider saving the poll url sent from Paynow in your database
paynow.send(payment).then(response => {
if (response.success) {
let link = response.redirectUrl;
}
});
Mobile Transactions
If you want to send an express (mobile) checkout request instead, the only thing that differs is the last step. You make a call to the sendMobile in the paynow object
instead of the send method.
The sendMobile method unlike the send method takes in two additional arguments i.e The phone number to send the payment request to and the mobile money method to use for the request. Note that currently only Ecocash and OneMoney are supported
paynow.sendMobile(payment, '0777000000', 'ecocash').then(response => {
});
The response object is almost identical to the one you get if you send a normal request. With a few differences, firstly, you don't get a url to redirect to. Instead you instructions (which ideally should be shown to the user instructing them how to make payment on their mobile phone)
paynow.sendMobile(
payment,
'0777000000',
'ecocash'
).then(function(response) {
if(response.success) {
let instructions = response.instructions
let pollUrl = response.pollUrl;
console.log(instructions)
} else {
console.log(response.error)
}
}).catch(ex => {
console.log('Your application has broken an axle', ex)
});
Checking transaction status
The SDK exposes a handy method that you can use to check the status of a transaction. Once you have instantiated the Paynow class.
let status = paynow.pollTransaction(pollUrl);
if (status.paid()) {
} else {
console.log("Why you no pay?");
}
Full Usage Example
const { Paynow } = require("paynow");
let paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");
paynow.resultUrl = "http://example.com/gateways/paynow/update";
paynow.returnUrl = "http://example.com/return?gateway=paynow";
let payment = paynow.createPayment("Invoice 35");
payment.add("Bananas", 2.5);
payment.add("Apples", 3.4);
paynow.send(payment).then( (response) => {
if(response.success) {
let link = response.redirectUrl;
let pollUrl = response.pollUrl;
}
});
Development
Fork this repository and clone to local machine