Git Product home page Git Product logo

paynow-java-sdk's Introduction

Java SDK for Paynow 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. Details about how you can retrieve the ID and key are explained in detail on this page

Prerequisites

In order to make use of this project, the following prerequisites must be met for it to work.

  1. Java JDK 7 or higher

Installation

To use the Java Paynow SDK, you need to add the latest release as a dependency to your project. The latest release will be in the Maven Central Repository.

Gradle

repositories {
	mavenCentral()
}
dependencies {
	implementation 'zw.co.paynow:java-sdk:1.1.1'
}

Maven

<dependency>
    <groupId>zw.co.paynow</groupId>
    <artifactId>java-sdk</artifactId>
    <version>1.1.1</version>
</dependency>

Getting started

Create an instance of Paynow associated with your integration ID and integration key as supplied by Paynow.

Paynow paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");

Initiating a web based transaction

A web based transaction is made over the web, via the Paynow website.

You can optionally set the result and return URLs.

  • Result URL is the URL on the merchant website where Paynow will post transaction results to.
  • Return URL is the URL where the customer will be redirected to after the transaction has been processed. If you do not specify a return URL, you will have to rely solely on polling status updates to determine if the transaction has been paid.
paynow.setResultUrl("http://example.com/gateways/paynow/update");
paynow.setReturnUrl("http://example.com/return?gateway=paynow&merchantReference=1234");

Create a new payment using any of the createPayment(...) methods, ensuring you pass your own unique reference for that payment (e.g invoice id). If you also pass in the email address, Paynow will attempt to auto login the customer at the Paynow website using this email address if it is associated with a registered account.

Payment payment = paynow.createPayment("Invoice 35");

You can then start adding items to the payment cart.

// Passing in the name of the item and the price of the item
payment.add("Bananas", 2.5);
payment.add("Apples", 1.0);

When you are ready to submit the payment request, initiate the transaction by calling the send(...) method. You can optionally set the description of the cart which will be shown to the user at the Paynow website, otherwise a default description will be generated.

payment.setCartDescription("Some custom description");//this is optional

// Save the response from paynow in a variable
WebInitResponse response = paynow.send(payment);

The WebInitResponse response from Paynow will contain various information including:

  • redirect URL where you should redirect the customer to make the payment
  • poll URL to check if the transaction has been paid

If the request was successful, you should consider saving the poll URL sent from Paynow in your database so that you can use it later to check if the transaction has been paid.

if (response.success()) {   
    // Get the url to redirect the user to so they can make payment
    String redirectUrl = response.redirectURL();
    
    // Get the poll URL of the transaction
    String pollUrl = response.pollUrl();
} else {
    // Ahhhhhhhhhhhhhhh
    // *freak out*
}

Initiating a mobile based transaction

A mobile transaction is a transaction made using mobile money e.g. using Ecocash or OneMoney

Note: Mobile based transactions currently only work for Ecocash and OneMoney

Create a new payment using the createPayment(...) method that requires a unique merchant reference and the email address of the user making the payment.

Payment payment = paynow.createPayment("Invoice 32", "[email protected]");

Adding items to the cart is the same as in web based transactions.

// Passing in the name of the item and the price of the item
payment.add("Bananas", 2.5);
payment.add("Apples", 1.0);

When you are ready to submit the payment request, initiate the transaction by calling the sendMobile(...) method.

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

The MobileInitResponse response from Paynow will contain various information including:

  • instructions for your customer on how to make the payment on their mobile phone
  • poll URL to check if the transaction has been paid

If the request was successful, you should consider saving the poll URL sent from Paynow in your database so that you can use it later to check if the transaction has been paid.

if (response.success()) {   
    // Get the instructions to show to the user
    String instructions  = response.instructions();

    // Get the poll URL of the transaction
    String pollUrl = response.pollUrl();
} else {
    // Ahhhhhhhhhhhhhhh
    // *freak out*
}

Poll the transaction to check for the payment status

It is possible to check the status of a transaction i.e. if the payment has been paid. To do this, make sure after initiating the transaction, you take note of the poll URL in the response. With this URL, call the pollTransaction(...) method of the Paynow object you created as follows. Note that checking transaction status is the same for web and mobile based transasctions.

// Check the status of the transaction with the specified pollUrl
StatusResponse status = paynow.pollTransaction(pollUrl);

if (status.isPaid()) {
  // Yay! Transaction was paid for
} else {
  System.out.println("Why you no pay?");
}

Full usage example

The following is a full usage example for web based transactions.

// MakingFirstPayment.java
import zw.co.paynow.core.*;
import zw.co.paynow.core.Paynow;
import zw.co.paynow.core.Payment;

public class MakingFirstPayment {

    public static void main(String[] args) {
        Paynow paynow = new Paynow("INTEGRATION_ID", "INTEGRATION_KEY");

        Payment payment = paynow.createPayment("Invoice 35");

        // Passing in the name of the item and the price of the item
        payment.add("Bananas", 2.5);
        payment.add("Apples", 3.4);

        //Initiating the transaction
        WebInitResponse response = paynow.send(payment);
        
        //If a mobile transaction,
        //MobileInitResponse response = paynow.sendMobile(payment, "0771234567", MobileMoneyMethod.ECOCASH);

        if (response.isRequestSuccess()) {
            // Get the url to redirect the user to so they can make payment
            String redirectURL = response.redirectURL();

            // Get the poll url of the transaction
            String pollUrl = response.pollUrl();

            //checking if the payment has been paid
            StatusResponse status = paynow.pollTransaction(pollUrl);

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

        } else {
            // Something went wrong
            System.out.println(response.errors());
        }
    }

}

paynow-java-sdk's People

Contributors

iammerus avatar lucy1603 avatar spaghettiwews avatar tawtsvenz avatar tzifudzi avatar webdevprojects-zw avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

paynow-java-sdk's Issues

Some Econet numbers regarded as invalid

Econet phone numbers beginning with 0771, 0776, 0777 and 0778 are all matched as invalid numbers with the regex specified in the Paynow.java file. Note that numbers beginning with these digits exist.

Is this an intended requirement? Or can these numbers also perform mobile transactions.

Inquiry about peer-to-peer transactions.

I would like to ask if there's some way of allowing peer-to-peer transactions, specifically allowing a user of an application to receive money from another user, e.g. through Ecocash. I read in your documentation that one would have to register to receive money from customers. I would like to sign users up for this service on your platform when they sign themselves up onto my app then under the hood they're signed up to your service, obviously your verification process would be used. All acknowledgment would be given to users about the integration with your service. Is this feasible?

Allow use of custom HttpClient

As raised by @bmukokera in the Paynow developer forum, an SDK should follow SOLID principles and hence be easy to extend. Currently, the SDK uses an open source HttpClient from the Apache library and this cannot be changed without recompiling the original source code.

The SDK needs to be refactored to allow for use with a custom HttpClient. @bmukokera If you are still willing to take it up may I proceed to assign you the task? We currently do not have a set of contributing guidelines so I would suggest you just create a develop branch and make a pull request into master.

Payment not done for 10.0 amount

when we use payment.add("Banana",10.0)

it will return status error, Invalid amount

the error is at

Payment.java

public final HashMap<String, String> toDictionary()

method

map.put("amount", getTotal().setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toString());

here this line changed amount to exponential

Paynow.java Init payment response condition error

replace

if (!response.get("status").toLowerCase().equals("error") || !response.containsKey("hash") || !Hash.verify(response, getIntegrationKey())) {
throw new HashMismatchException(response.get("Error"));
}

with

if (response.get("status").toLowerCase().equals("error") || !response.containsKey("hash") || !Hash.verify(response, getIntegrationKey())) {
throw new HashMismatchException(response.get("Error"));
}

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.