Home » Integration » Integrate Instamojo with Java or Spring Boot

Integrate Instamojo with Java or Spring Boot

Instamojo is one of the best payment gateways in India. They offer the best pricing for online transactions. It supports many technologies like .net, java, python, ruby, PHP, android, ios, C# and etc. We can integrate this easily into our eCommerce website.

The payment gateway is an online payment service to make and receive payments online. E-commerce websites using this technology to receive payments directly from the customers while ordering the items.

There are many payment gateways available in the market for doing E-Commerce business. Like PayPal, PayUMoney, Paytm, Razorpay, and so on. Here let us see about the Instamojo payment gateway and how to use that in the Spring Boot application.

Actually, payment through Instamojo is the easiest process. They are generating the URL for every order's payment. Customers can pay through the URL with any browser I mean just click the link and pay.

Instamojo

So this payment URL can be used like customers can make the payment directly in eCommerce website or Website admin can make the order for a customer and just share the payment URL to customers to pay.

There are several payment methods available in Instamojo. Here I listed it. The payment methods are BHIM, GPAY, PayTM, Credit and Debit Card, Net Banking, and Wallets.

In this article, let me explain how to integrate Instamojo with Java or Spring Boot.

If you want to know more about instamojo please click here. Now we start to integrate with Spring Boot.

Setup Instamojo with Spring Boot

Please include this maven dependency in your pom.xml file

<dependency>
	<groupId>com.instamojo</groupId>
	<artifactId>instamojo-java</artifactId>
	<version>1.0.1</version>
	 <type>pom</type>
</dependency>

you can find the latest version of Instamojo from their website. Now we focus on the coding part. Before that, we need to signup with Instamojo to get the client credentials.

We have to use clientId and clientSecret for establishing a connection on Instamojo before making any API request.

The below snippet is used to make the connection establishment with Instamojo.

private void connectionEstablishementInstamojo() {
    String clientId = "ad1#####";
    String clientSecret = "mqRcIZ######";
    String apiEndPoint = "https://api.instamojo.com/v2/";
    String authEndPoint = "https://www.instamojo.com/oauth2/token/";
    try {
        // gets the reference to the instamojo api
        Instamojo api = InstamojoImpl.getApi(clientId, clientSecret, apiEndPoint, authEndPoint);
    } catch (ConnectionException e) {
        LOGGER.log(Level.SEVERE, e.toString(), e);
    }
}

The above snippet clientId and clientSecret you will be getting from Instamojo after signup. Here the apiEndPoint and authEndPoint belong to the production URL. I have used the production URL directly here.

Production Endpoints

apiendpoint=https://api.instamojo.com/v2/
authendpoint=https://www.instamojo.com/oauth2/token/

Testing Endpoints

If you want to test it with the testing environment then the endpoints should be different and the clientId and clientSecret you need to get it by register Instamojo with the testing environment.

The apiEndPoint and authEndPoint of Instamojo testing environment is below

apiendpoint=https://test.instamojo.com/v2/
authendpoint=https://test.instamojo.com/oauth2/token/

Create New Order with Payment Order API

The below snippet is used to create the new order

public CreatePaymentOrderResponse createNewOrder() {
    PaymentOrder order = new PaymentOrder();
    CreatePaymentOrderResponse createPaymentOrderResponse = new CreatePaymentOrderResponse();
    order.setName("ABC");
    order.setEmail("[email protected]");
    order.setPhone("1234567890");
    order.setCurrency("INR");
    order.setAmount(15 D);
    order.setDescription("Create New Order");
    order.setRedirectUrl("http://www.someexample.com");
    order.setWebhookUrl("http://www.someurl.com/");
    //Unique Transaction Id Here i used UUID
    order.setTransactionId(UUID.randomUUID().toString());

    //connect to instamojo
    connectionEstablishementInstamojo();

    boolean isOrderValid = order.validate();
    if (isOrderValid) {
        try {
            createPaymentOrderResponse = api.createNewPaymentOrder(order);
            // print the status of the payment order.            System.out.println(createPaymentOrderResponse.getPaymentOrder().getStatus());
        } catch (InvalidPaymentOrderException e) {
            //logger.log(Level.SEVERE, e.toString(), e);
            if (order.isTransactionIdInvalid()) {
                System.out.println("Transaction id is invalid. This is mostly due to duplicate  transaction id.");
            }
            if (order.isCurrencyInvalid()) {
                System.out.println("Currency is invalid.");
            }
        } catch (ConnectionException e) {
            //Logger.log(Level.SEVERE, e.toString(), e);
        }
    } else {
        // inform validation errors to the user.
        if (order.isTransactionIdInvalid()) {
            System.out.println("Transaction id is invalid.");
        }
        if (order.isAmountInvalid()) {
            System.out.println("Amount can not be less than 9.00.");
        }
        if (order.isCurrencyInvalid()) {
            System.out.println("Please provide the currency.");
        }
        if (order.isDescriptionInvalid()) {
            System.out.println("Description can not be greater than 255 characters.");
        }
        if (order.isEmailInvalid()) {
            System.out.println("Please provide valid Email Address.");
        }
        if (order.isNameInvalid()) {
            System.out.println("Name can not be greater than 100 characters.");
        }
        if (order.isPhoneInvalid()) {
            System.out.println("Phone is invalid.");
        }
        if (order.isRedirectUrlInvalid()) {
            System.out.println("Please provide valid Redirect url.");
        }
        if (order.isWebhookInvalid()) {
            System.out.println("Provide a valid webhook url");
        }
    }
    return createPaymentOrderResponse;
}

The above API call returns the payment order details and payment option details. In payment option details response contains the payment URL which we can use for payment.

By clicking the payment URL will redirect to the Instamojo payment gateway page with payment options. We can choose any payment mode and do the payment then it will be redirected to the redirect URL which we set in to create an order API. This redirection happens whether the payment is successful or failed.

Hope this article is helpful for Instamojo integration. The API of Instamojo changes based on the maven version of Instamojo. So if you want to use the code in this article please use the same maven version I used. If you want to know all other APIs of Instamojo please click here.

Leave a Reply

Your email address will not be published. Required fields are marked *