Skip to content

Latest commit

 

History

History
153 lines (99 loc) · 3.78 KB

File metadata and controls

153 lines (99 loc) · 3.78 KB

Razorpay Java SDK

Official java bindings for the Razorpay API.

Documentation

Documentation of Razorpay's API and their usage is available at https://docs.razorpay.com

Requirements

Java 1.8 or later

Mock Tests Support till Java 1.8

Installation

Maven users

Add this dependency to your project's POM:

<dependency>
 <groupId>com.razorpay</groupId>
 <artifactId>razorpay-java</artifactId>
 <version>1.4.10</version>
</dependency>

Gradle users

Add this dependency to your project's build file:

implementation "com.razorpay:razorpay-java:1.4.7"

Usage

RazorpayClient can be instantiated via two ways:

Using Private Auth

Instantiate RazorpayClient with key_id & key_secret. You can obtain the keys from the dashboard app https://dashboard.razorpay.com/#/app/keys

// Initialize client
RazorpayClient instance = new RazorpayClient("key_id", "key_secret");
  • Add custom headers to request (optional)
Map<String, String> headers = new HashMap<String, String>();
razorpayClient.addHeaders(headers);

Using Access Token

Instantiate RazorpayClient with access_token. The access_token can be obtained only in case if you are a platform partner. For more information, refer page - https://razorpay.com/docs/partners/platform/.

// Initialize client
RazorpayClient instance = new RazorpayClient("access_token");
  • Add custom headers to request (optional)
Map<String, String> headers = new HashMap<String, String>();
razorpayClient.addHeaders(headers);

Error Handling

SDK calls throw RazorpayException on API errors. In addition to the message, you can access structured error data returned by the API:

try {
  Order order = razorpayClient.orders.create(orderRequest);
} catch (RazorpayException e) {
  int    httpStatus  = e.getStatusCode();   // HTTP status code (e.g. 400)
  String code        = e.getCode();         // e.g. "BAD_REQUEST_ERROR"
  String description = e.getDescription();  // human-readable message
  String field       = e.getField();        // offending field, if any
  String reason      = e.getReason();
  String source      = e.getSource();
  String step        = e.getStep();
  JSONObject metadata = e.getMetadata();
  JSONObject raw     = e.getErrorResponse(); // full error JSON
}

Supported Resources


  • Make custom requests

You can make custom API requests using clients. For example, here is how to make custom request to /payments/path endpoint.

Entity response = razorpayClient.Payments.post("path", JSONObject requestBody);