DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Efficient Asynchronous Processing Using CyclicBarrier and CompletableFuture in Java
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Java 23: What Developers Need to Know

Trending

  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  1. DZone
  2. Coding
  3. Java
  4. A Multi-Gateway Payment Processing Library for Java

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.

By 
Muhammad ilyas user avatar
Muhammad ilyas
·
May. 03, 18 · News
Likes (29)
Comment
Save
Tweet
Share
58.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Library Java (programming language) Processing

Published at DZone with permission of Muhammad ilyas. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Efficient Asynchronous Processing Using CyclicBarrier and CompletableFuture in Java
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Java 23: What Developers Need to Know

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!