Instamojo java wrapper for the Instamojo API assists you to programmatically create, list, filter payment orders and refunds on Instamojo.
Java Version : 1.7+
Add following dependency to the build.gradle
compile group: 'com.instamojo', name: 'instamojo-java', version: '2.0.2'
Generate CLIENT_ID and CLIENT_SECRET for specific environments from the following links.
Related support article: How Do I Get My Client ID And Client Secret?
As of now, MULTITENANCY IS NOT SUPPORTED by this wrapper which means you will not be able to use this wrapper in a single application with multiple Instamojo accounts. The call to ApiContext.create()
returns a singleton object of class ApiContext
with the given CLIENT_ID and CLIENT_SECRET, and will always return the same object even when called multiple times (even with a different CLIENT_ID and CLIENT_SECRET).
/*
* Get a reference to the instamojo api
*/
ApiContext context = ApiContext.create("[CLIENT_ID]", "[CLIENT_SECRET]", ApiContext.Mode.TEST);
Instamojo api = new InstamojoImpl(context);
/*
* Create a new payment order
*/
PaymentOrder order = new PaymentOrder();
order.setName("John Smith");
order.setEmail("john.smith@gmail.com");
order.setPhone("12345678790");
order.setCurrency("INR");
order.setAmount(9D);
order.setDescription("This is a test transaction.");
order.setRedirectUrl("http://www.someexample.com");
order.setWebhookUrl("http://www.someurl.com/");
order.setTransactionId("dxg234");
try {
PaymentOrderResponse paymentOrderResponse = api.createPaymentOrder(order);
System.out.println(paymentOrderResponse.getPaymentOrder().getStatus());
} catch (HTTPException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getMessage());
System.out.println(e.getJsonPayload());
} catch (ConnectionException e) {
System.out.println(e.getMessage());
}
/*
* Get details of payment order when order id is given
*/
try {
PaymentOrder paymentOrder = api.getPaymentOrder("[PAYMENT_ORDER_ID]");
System.out.println(paymentOrder.getId());
System.out.println(paymentOrder.getStatus());
} catch (HTTPException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getMessage());
System.out.println(e.getJsonPayload());
} catch (ConnectionException e) {
System.out.println(e.getMessage());
}
/*
* Get details of payment order when transaction id is given
*/
try {
PaymentOrder paymentOrder = api.getPaymentOrderByTransactionId("[TRANSACTION_ID]");
System.out.println(paymentOrder.getId());
System.out.println(paymentOrder.getStatus());
} catch (HTTPException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getMessage());
System.out.println(e.getJsonPayload());
} catch (ConnectionException e) {
System.out.println(e.getMessage());
}
/*
* Get list of all payment orders
*/
try {
PaymentOrderFilter paymentOrderFilter = new PaymentOrderFilter();
ApiListResponse<PaymentOrder> paymentOrders = api.getPaymentOrders(paymentOrderFilter);
// Loop over all of the payment orders and print status of each
// payment order.
for (PaymentOrder paymentOrder : paymentOrders.getResults()) {
System.out.println("Result = " + paymentOrder.getStatus());
}
System.out.println(paymentOrders);
} catch (HTTPException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getMessage());
System.out.println(e.getJsonPayload());
} catch (ConnectionException e) {
System.out.println(e.getMessage());
}
/*
* Create a new refund
*/
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);
try {
Refund createdRefund = api.createRefund(refund);
System.out.println(createdRefund.getId());
System.out.println(createdRefund.getStatus());
} catch (HTTPException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getMessage());
System.out.println(e.getJsonPayload());
} catch (ConnectionException e) {
System.out.println(e.getMessage());
}