Home » Integration » All the java API of Instamojo Payment Gateway

All the java API of Instamojo Payment Gateway

In the previous article, we have seen how to set up Instamojo with Spring Boot and create the first order on Instamojo. In this article, I explain all the other java APIs of Instamojo.

Instamojo

Here all the APIs belong to the Instamojo maven version of 1.0.1. So if you want to use the below snippets please use the same maven version.

We can get order details from Instamojo in different ways. Here I explain two major ways to get it.

  1. Through order id, we can get the order details.
  2. Through transaction id, we can get the order details.

Every order of Instamojo must have the order id and transaction id.

The below snippet is used to get the order details from order id

try {

connectionEstablishementInstamojo();
//put your order id here
PaymentOrderDetailsResponse paymentOrderDetailsResponse = api.getPaymentOrderDetails("[PAYMENT_ORDER_ID]");

if (paymentOrderDetailsResponse.getId() != null) {
// print the status of the payment order.
System.out.println(paymentOrderDetailsResponse.getStatus());
} else {
System.out.println("Please enter valid order id.");
}
} catch (ConnectionException e) {
	LOGGER.log(Level.SEVERE, e.toString(), e);
}

the above snippet i used connectionEstablishementInstamojo() function. Please refer that here setup of Instamojo with java.

Get details of payment order when transaction id is given.

try {
connectionEstablishementInstamojo();
//put your transaction id here
PaymentOrderDetailsResponse paymentOrderDetailsResponse = api.getPaymentOrderDetailsByTransactionId("[TRANSACTION_ID]");

if (paymentOrderDetailsResponse.getId() != null) {
// print the status of the payment order.
System.out.println(paymentOrderDetailsResponse.getStatus());
} else {
System.out.println("Please enter valid transaction id.");
}
} catch (ConnectionException e) {
	LOGGER.log(Level.SEVERE, e.toString(), e);
}

The below API is for getting all the orders from Instamojo.

try {

connectionEstablishementInstamojo();
PaymentOrderFilter paymentOrderFilter = new PaymentOrderFilter();

PaymentOrderListResponse paymentOrderListResponse = api.getPaymentOrderList(paymentOrderFilter);

// Loop over all of the payment orders and print status of each
// payment order.
for (PaymentOrder paymentOrder : paymentOrderListResponse.getPaymentOrders()) {
System.out.println("Result = " + paymentOrder.getStatus());
}
System.out.println(paymentOrderListResponse.getPaymentOrders());
} catch (ConnectionException e) {
	LOGGER.log(Level.SEVERE, e.toString(), e);
}

We can do the refund also to the customers. The API for creating a refund below.

Refund refund = new Refund();
refund.setPaymentId("[PaymentId]");
refund.setStatus("refunded");
refund.setType("RFD");
refund.setBody("This is a test refund.");
refund.setRefundAmount(9D);
refund.setTotalAmount(10D);

boolean isRefundValid = refund.validate();

if (isRefundValid) {
try {
connectionEstablishementInstamojo();
CreateRefundResponse createRefundResponse = api.createNewRefund(refund);
// print the status of the refund.
		System.out.println(createRefundResponse.getRefund().getStatus());
} catch (InvalidRefundException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);

if (refund.isTypeInvalid()) {
System.out.println("type is invalid.");
}
} catch (ConnectionException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
} else {
// inform validation errors to the user.
if (refund.isPaymentIdInvalid()) {
System.out.println("Payment id is invalid.");
}
if (refund.isTypeInvalid()) {
System.out.println("Type is invalid.");
}
if (refund.isBodyInvalid()) {
System.out.println("Body is invalid.");
}
if (refund.isRefundAmountInvalid()) {
System.out.println("Refund amount is invalid.");
}
}

Hope you understood all the APIs. if you have any queries please mention your comment below. If you want to use the latest API please refer to the Instamojo website.

Leave a Reply

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