Multi Gateway Payment Processing for Java – DEVELOPPARADISE
17/04/2018

Multi Gateway Payment Processing for Java


Introduction

While creating a fully functional web site like e-commerce where payment is necessary, developers usually face a tough time while implementing payment gateway. When your idea is a little big, you want multigateway support for your clients – that’s a real pain. Being a developer myself, I can understand what a developer should go through.

  1. Read each gateway documentation.
  2. Write the code.
  3. Test it.

And finally bring them all to a single standard. For example, some gateway APIs accept XML while others accept Json or a query string. I have also faced this kind of issue and thought if I am facing this kind of issue, why not other developers like me who will face the errors and they will go through the same process (reinventing the wheel).

I finally come up with an idea to make an opensource library and upload it on github. So I will implemented the gateway that I have worked on and any other developer who has worked on any gateway will implement it in our library and one day, this library will support all gateways.

J2pay

Yes, that’s what I named J2pay (Java 2 payment).

J2Pay is an open source multi-gateway payment processing library for Java which provides a simple and generic API for many gateways. It reduces developers’ efforts of writing individual code for each gateway. It provides flexibility to write code once for all gateways. It also excludes the effort of reading docs for individual gateways.

Below are the links that you should see:

Yes, I have already created the documentation and the project is live.

Key Features

I think that’s enough: I have explained the reason behind developing the opensource library, Now let me introduce to you how this library makes developer`s life easy.

  • While working on J2pay, you will always be passing and retrieving json. Yes, no matter what is the native way of gateway API, you will always be using json and for that, I used org.json library.
  • You do not have to worry about gateway specific variables like some gateways return transaction id as transId or transnum but j2pay will always return transactionId and believe me, it will also give you the same formatted response no matter what gateway you are using.

My first and favourite point is that you should not read the gateway documentation because a developer has already done that for you (maybe you are that developer who integrated the gateway).

J2pay magical methods, yes I call them magical – you will know why soon.

I created four methods:

  1. getApiSampleParameters
  2. getRefundSampleParameters
  3. getVoidSampleParameters
  4. getRebillSampleParameters

So if you ever worked on payment gateway, there are some authentications that must be provided in each request like username and password so the gateway API can identify you.

You can call getApiSampleParameters. Let me show you some code.

First, I will get the gateway object which I will be working on. Suppose I am working on authorize gateway.

//getting the desired gateway. Gateway gateway = GatewayFactory.getGateway(AvailableGateways.AUTHORIZE);

So we have got the authorize gateway in gateway variable and until now, I don’t know what API parameters are required for authorize gateway. But don’t worry, I promise you never have to read the authorize documentation.

JSONObject apiSampleParameters = gateway.getApiSampleParameters(); System.out.println(apiSampleParameters)      //output {"name":"also called api user name / api login id","transactionKey":"the transaction key"}

See, the library itself is telling us what are the API parameters. You can populate this key by your secret values and can pass it to purchase method.

For a detailed example, please see the official documentation link provided in the above section.

This library is only focused on four major methods of gateways:

  1. Purchase
  2. Refund
  3. Void
  4. Rebill (Recurring)

Let me tell you the most important part that is unique response. Let me share a sample response of purchase method.

//output     {         "lr": {             "success": true,             "message": "SUCCESS",             "transactionId": "3902990127",             "amount": 45,             "cardExpiryYear": "2017",             "cardFirst6": "601160",             "cardExpiryMonth": "12",             "maskedCard": "601160******6611",             "rebillParams": {                 "customerVaultId": "174302554"             },                     "voidParams": {                 "transactionId": "3902990127"             },             "currencyCode": "USD",             "cardLast4": "6611",             "refundParams": {                 "transactionId": "3902990127"             }         },         "gr": { // long gateway response }     }

As you can see, response is divided into two keys:

  1. lr (library response)
  2. gr (gateway response)

Library response only contains the values that library thinks important for you and could be useful for further actions like refund/void/rebill. Keep in mind library response has already prepared the parameters required for further actions on this transaction, i.e., refund, rebill or void.

Let me show you how simply you can execute a recurring transaction.

Assume we saved purchase response in purchaseResponse variable.

JSONObject rebillParams = purchaseResponse.getJSONObject("lr").getJSONObject("rebillParams");     HTTPResponse rebillResponse = gateway.rebill(apiSampleParameters, rebillParams, 50);

See, just two lines and you have successfully executed the recurring, same for refund and void.

Final Words

Please share your thoughts about J2pay. If you have some free time and have worked on any gateway, you can implement it in this library. See the contributors docs here.

Also don’t forget to give a star to github repository or fork the project if you would like to contribute and watch the repo to get notified when new gateway support is added.

Thanks for reading!