Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Multi-Gateway Payment Processing Library for Java

DZone's Guide to

A Multi-Gateway Payment Processing Library for Java

Check out J2Pay, an open source library designed to ease the burden of working with multiple payment gateways in your Java projects.

· Java Zone ·
Free Resource

Distribute microservices between the private and public clusters, yet maintain bi-directional connectivity. Content provided by IBM Developer.

While creating a fully functional website for something like eCommerce, where payment is necessary, developers usually face a tough time while implementing payment gateways. When your idea is a little big, you want multi-gateway support for your clients — but that's a real pain. Being a developer myself, I can understand.

  1. Read each gateway's documentation.

  2. Write the code.

  3. Test it.

  4. 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, others are, too." 

There's no sense in reinventing the wheel, so I finally came up with an idea to make an open source library and upload it to GitHub. So, I will implement the gateway that I have worked on, and any other developer who has worked on any gateway can implement it in this library. The goal, one day, is for this library to support all gateways.

J2Pay

J2Pay is an open source multi-gateway payment processing library for Java that provides a simple and generic API for many gateways. It reduces developers' efforts when 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 visit:

  1. GitHub: https://github.com/tranxactive/J2PAY

  2. Official documentation: http://www.j2pay.org

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

Key Features

While working on J2pay, you will always be passing and retrieving JSON. Yes, no matter which format is native to a gateway API, you will always be using JSON, and for that, I used the org.json library.

You do not have to worry about gateway-specific variables like some gateways returning transaction IDs as transId or transnum. Rather, J2Pay will always return transactionId, and it will also give you the same formatted response no matter what gateway you are using.

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

J2Pay uses magical methods. Yes, I call them magical — you will know why soon.

I have created four methods:

  1. getApiSampleParameters

  2. getRefundSampleParameters

  3. getVoidSampleParameters

  4. getRebillSampleParameters

So if you have ever worked on a 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 that I will be working on. Suppose I am working on an Authorize gateway.

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


So now we have got the Authorize gateway in the gateway variable and, up until now, I haven't known what API parameters are required for the 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 the API parameters. You can populate this key via your secret values and pass it to the 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 about the most important part — unique response — by sharing a sample response of the 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, the response is divided into two keys:

  1. lr (library response)

  2. gr (gateway response)

The library response only contains the values that the library thinks are important for you and could be useful for further actions, like refund/void/rebill. Keep in mind that the 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 the purchase response in the 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 transaction. It's the 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.

Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

Topics:
java ,payment gateway ,open source ,library ,j2pay ,ecommerce

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}