Skip to content

Paynow Integration with NodeJS

So we finished the boring parts and today we are going to do the following

1. Initialize Paynow

Before you can do anything you have to include PayNow and initialize it first

JavaScript
// include paynow here
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";

This creates a PayNow instance, your keys should be correct or it will raise Integration Exception on checkout. Errors can be annoying.

2. Creating a payment

The next stage involves adding a payment to the PayNow instance you have created:

JavaScript
let invoice_name = "Invoice #100"
let user_email = "john@example.co.zw" # get email from post request here
let payment = paynow.createPayment(invoice_name, user_email)

This adds a payment to your instance you create in step 1. Once payment succeeds PayNow will send an email to the customer for transparence and reference purposes.

##3. Add some products

If you have one product you can add it like this:

JavaScript
payment.add('Sadza and Beans', 1.25)

If you have more than one products then you can add them like this

JavaScript
let products = [
  { name: 'Rice', price: 3.0 },
  { name: 'Potatoes', price: 10.0 },
  { name: 'Knife', price: 4.0 },
  <em>// Add more products as needed</em>
]

products.forEach((product) => {
  payment.add(product.name, product.price)
})

If those products have quantities or amounts then it will be something like this

JavaScript
let products = [
  { name: 'Rice', price: 3.0, qty: 2 },
  { name: 'Potatoes', price: 10.0, qty: 1 },
  { name: 'Knife', price: 4.0, qty: 1 },
  <em>// Add more products as needed</em>
]

let total = 0
let tax = 0.01

products.forEach((product) => {
  let item_total = product.price * product.qty
  let tax_amount = item_total * tax
  item_total += tax_amount
  total += item_total
  payment.add(product.name, item_total)
})

You could add things like coupons, discounts, promotions and what what but well I will leave that you. Code wise you can also use things such as classes but well thats up to you.

Sending payment to PayNow

After adding payment the next stage is sending the payment to payment to PayNow

JavaScript
let response = paynow.sendMobile(payment, '077777777', 'ecocash').then((response) => {
  // Handle response
})

Here were requesting for a mobile transaction to occur between our Paynow account and 0777777777 ‘s Ecocash.

The number you get from user through forms or from database. The platform(‘ecocash’) can be retrieved from user through forms or by auto detection

JavaScript
let phone = '07777777'
let platform = ''

if (phone.startsWith('071')) {
  platform = 'onemoney'
} else if (phone.startsWith('073')) {
  platform = 'telecash'
} else {
  platform = 'ecocash'
}

// Then
let response = paynow.sendMobile(payment, phone, platform).then((response) => {
  // Handle response
})

Your users will love this 😎😎😎

So this part alone should send a message to your customer mobile asking for payment:

Alt Text

Of course there will be some small differences.

Ok that’s it for this one in the next tutorial we will talk about checking payments.

image