Giter Site home page Giter Site logo

paynow / paynow-nodejs-sdk Goto Github PK

View Code? Open in Web Editor NEW
29.0 7.0 22.0 152 KB

NodeJS SDK for Zimbabwe's leading payments gateway, Paynow

Home Page: https://www.paynow.co.zw

TypeScript 99.58% JavaScript 0.42%
paynow-zimbabwe payment paynow-api payment-gateway payment-integration online-payments online-payment-system

paynow-nodejs-sdk's Introduction

Build Status

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

  1. Node version 0.6.0 and above
  2. 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 return url can be set at later stages. 
You might want to do this if you want to pass data to the return url (like the reference of the transaction) */

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

// Passing in the name of the item and the price of the item
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.

// Save the response from paynow in a variable
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 => {
  // Check if request was successful
  if (response.success) {
    // Get the link to redirect the user to, then use it as you see fit
    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 => {
  // Handle 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(
    
    // The payment to send to Paynow
    payment, 

    // The phone number making payment
    '0777000000',
    
    // The mobile money method to use.
    'ecocash' 

).then(function(response) {
    if(response.success) {
        // These are the instructions to show the user. 
        // Instruction for how the user can make payment
        let instructions = response.instructions // Get Payment instructions for the selected mobile money method

        // Get poll url for the transaction. This is the url used to check the status of the transaction. 
        // You might want to save this, we recommend you do it
        let pollUrl = response.pollUrl; 

        console.log(instructions)

    } else {
        console.log(response.error)
    }
}).catch(ex => {
    // Ahhhhhhhhhhhhhhh
    // *freak out*
    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.

// Check the status of the transaction with the specified pollUrl
// Now you see why you need to save that url ;-)
let status = paynow.pollTransaction(pollUrl);

if (status.paid()) {
  // Yay! Transaction was paid for
} else {
  console.log("Why you no pay?");
}

Full Usage Example

// Require in the Paynow class
const { Paynow } = require("paynow");

// Create instance of Paynow class
let paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");

// Set return and result urls
paynow.resultUrl = "http://example.com/gateways/paynow/update";
paynow.returnUrl = "http://example.com/return?gateway=paynow";

// Create a new payment
let payment = paynow.createPayment("Invoice 35");

// Add items to the payment list passing in the name of the item and it's price
payment.add("Bananas", 2.5);
payment.add("Apples", 3.4);

// Send off the payment to Paynow
paynow.send(payment).then( (response) => {

    // Check if request was successful
    if(response.success) {
        // Get the link to redirect the user to, then use it as you see fit
        let link = response.redirectUrl;

        // Save poll url, maybe (recommended)?
        let pollUrl = response.pollUrl;
    }

});

Development

Fork this repository and clone to local machine

paynow-nodejs-sdk's People

Contributors

bzmp125 avatar iammerus avatar michaeldera avatar okandas avatar spaghettiwews avatar webdevprojects-zw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

paynow-nodejs-sdk's Issues

What is the versioning policy

So if I want to make changes to the SDK that would change some of the changes that would affect the way the SDK work, what is the policy for going about that?

Is it possible to have a separate branch for development of a version that can affect how the current one is working?

Confirm payment made

I am struggling to find a way to confirm when payment for mobile has been initiated, pollUrl returns undefined, How can I confirm when payment has been cancelled, or was successful for MOBILE

An error occured while initiating transaction Error: Hashes do not match!

mobile payement not working

An error occured while initiating transaction Error: Hashes do not match!
at Paynow.parse (C:\Users\USER\Documents\projects\nodejs\league\leaguebackend\node_modules\paynow\dist\paynow.js:101:23)
at C:\Users\USER\Documents\projects\nodejs\league\leaguebackend\node_modules\paynow\dist\paynow.js:87:26
at processTicksAndRejections (internal/process/task_queues.js:95:5)
paynow
paynow

Exposure of project secrets

I'm really concerned about the secrets of the project being exposed to everyone.
After attempting to read the Paynow NodeJS codebase so that I can collaborate if needed. I saw that in the root folder of the SDK project, secrets e.g api_key
Screenshot_20230130_160337
were being exposed to the public. Would you mind confirming whether this is necessary and why? I have attached a screenshot from the travis.yml file.

Update to ES6 compatible SDK

Is your feature request related to a problem? Please describe.
Currently the SDK doesn't support the import syntax which is now common in modern Javascript frameworks. Thus the SDK cannot natively work in frameworks like Svelte.

Describe the solution you'd like
Update the framwork to use import, not just with Typescript but with vanilla Javascript.

Describe alternatives you've considered
None that I can think of.

Additional context

Attempting to fetch transaction status using pollTransaction() throwing a rejected promise error

When trying to fetch, the status of a transaction using its pollUrl by calling pollTransaction, the SDK is throwing an error "Invalid URI '/'". Seems to be occurring once the request has been passed to the HTTP client

To Reproduce
Steps to reproduce the behavior:

  1. Initiate a new transaction
  2. Get the pollUrl from the InitResponse object
  3. Call pollTransaction on the Paynow instance passing in the pollUrl
  4. See error

Expected behavior
The SDK was supposed to return a PromiseLike<StatusResponse> showing the current state of the transaction

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.