Skip to content
Home ยป Blog ยป Paynow Integration with Java

Paynow Integration with Java

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

Java
// include paynow here
import webdev.core.*;
import webdev.payments.Paynow;
import webdev.payments.Payment;

Paynow paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");
paynow.setResultUrl("http://example.com/gateways/paynow/update");
paynow.setReturnUrl("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:

Java
String invoice_name = "Invoice #100";
String user_email = "john@example.co.zw";
Payment 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:

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

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

Java

import java.util.ArrayList;
import java.util.List;

public class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}


// Somewhere in your code.

List<Product> products = new ArrayList<>();
products.add(new Product("Rice", 3.0));
products.add(new Product("Potatoes", 10.0));
products.add(new Product("Knife", 4.0));


 for (Product product : products) {
    payment.add(product.getName(), product.getPrice());
}

You could add things like coupons, tax, discounts, promotions and what what but well I will leave that you.

Sending payment to PayNow

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

Java
MobileInitResponse response = paynow.sendMobile(payment, "0771234567", MobileMoneyMethod.ECOCASH);

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

Java
String phone = "07777777";
String platform = "";

if (phone.startsWith("071")) {
    platform = MobileMoneyMethod.ONEMONEY;
} else if (phone.startsWith("073")) {
    platform = MobileMoneyMethod.TELECASH;
} else {
    platform = MobileMoneyMethod.ECOCASH;
}


// Then
MobileInitResponse response = paynow.sendMobile(payment, "0771234567", platform)

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