RazorPay Integration with React JS and Java application

3.9k views Asked by At

Is it possible to integrate Razorpay in an application using reactjs(front end) and Java 8(backend).

On razorpay website they do not have documents to integrate with react js applications.

I'm new to reactjs and Payment gateway integration so If anyone has worked on razorpay integration with reactjs Please suggest any good approach/any documentation available.

Or if anyone can suggest any good payment gateway which is easy to integrate with REACTJS+JAVA application.

Thanks !

1

There are 1 answers

5
Ravikiran On BEST ANSWER

In order to deal with the API JAVA as a backend is enough and For the Frontend With React, we write HTML using JavaScript. We rely on the power of JavaScript to generate HTML that depends on some data, rather than enhancing HTML to make it work with that data. Enhancing HTML is what other JavaScript frameworks usually do.

So to work with the API. Prerequisites# Java 1.7 or higher

Installation# You can install Razorpay using Maven or Gradle.

Maven# Download and install Maven on your system.

Download the latest Source code zip file from the Releases section of GitHub.

Unzip the file and add this dependency to the project object model (POM) of your project.

<dependency>
<groupId>com.razorpay</groupId>
<artifactId>razorpay-java</artifactId>
<version>x.y.z</version> //x.y.z = the version you want to install
</dependency>

Gradle# Download and install Gradle on your system.

Download the latest Source code zip file from the Releases section of GitHub.

Unzip the file and add this dependency to the build file of the project:

compile "com.razorpay:razorpay-java:x.y.z" //x.y.z = the version you want to install

Sample Code

Initialization#

Create RazorpayClient with key_id & key_secret.
JAVA RazorpayClient razorpayClient = new RazorpayClient("key_id", "key_secret");

Generate API Key#

Log into your Dashboard with appropriate credentials. Select the mode (Test or Live) for which you want to generate the API key. Note: You have to generate separate API Keys for the test and live modes. No real money is used in test mode. Navigate to Settings → API Keys → Generate Key to generate key for the selected mode.

Add Custom Headers# Note: This is an optional step.

To add custom headers to request:

Map<String, String> headers = new HashMap<String, String>();
razorpayClient.addHeaders(headers);

Orders# Note: Click here for request parameters and an example request and response.

Create an Order#

JSONObject options = new JSONObject();
options.put("amount", 5000);
options.put("currency", "INR");
options.put("receipt", "txn_123456");
Order order = razorpayClient.Orders.create(options);

Fetch Order by ID#

Order order = razorpayClient.Orders.fetch("order_id");

Fetch all Orders#

List<Order> orders = razorpayClient.Orders.fetchAll();

Fetch Order for a Payment#

List<Payment> payments = razorpayClient.Orders.fetchPayments("order_id");

Capture a Payment#

JSONObject options = new JSONObject();
options.put("amount", 1000);
options.put("currency", "INR");
razorpayClient.Payments.capture("payment_id", options);

Fetch Payment by ID#

Payment payment = razorpayClient.Payments.fetch("payment_id");
int amount = payment.get("amount");
String id = payment.get("id");
Date createdAt = payment.get("created_at");

The entity .get("attribute_key") method has flexible return types depending on the attribute.

Fetch all Payments#

List<Payment> payments = razorpayClient.Payments.fetchAll();

Create a Full Refund#

JSONObject refundRequest = new JSONObject();
refundRequest.put("payment_id", <payment_id>);
Refund refund = razorpayClient.Payments.refund(refundRequest);

Create a Partial Refund#

JSONObject refundRequest = new JSONObject();
refundRequest.put("amount", <amount>);
refundRequest.put("payment_id", <payment_id>);
Refund refund = razorpay.Payments.refund(refundRequest);

Fetch Refund by ID#

Refund refund = razorpayClient.Refunds.fetch("refund_id");

Fetch all Refunds#

List<Refund> refunds = razorpayClient.Refunds.fetchAll();

Fetch Refund against Payment#

Refund refund = razorpayClient.Payments.fetchRefund("refund_id");

Fetch all Refunds against a Payment#

JSONObject refundRequest = new JSONObject();
refundRequest.put("payment_id", <payment_id>);
List<Refund> refund = razorpayClient.Payments.fetchAllRefunds(refundRequest);

Fetch Card Details#

Card card = razorpayClient.Cards.fetch(id);

Create a Customer#

JSONObject request = new JSONObject();
request.put("name", <name>);
request.put("email", <email>);
Customer customer = razorpayClient.Customers.create(request);

Fetch Customer by ID#

Customer customer = razorpayClient.Customers.fetch(customerId);

Edit a Customer#

JSONObject request = new JSONObject();
request.put("name", <name>);
request.put("email", <email>);
Customer customer = razorpayClient.Customers.edit(customerId, request);

Fetch a Token#

Token token = razorpayClient.Customers.fetchToken(customerId, tokenId);

Fetch Token for a Customer#

List<Token> tokens = razorpayClient.Customers.fetchTokens(customerId);

Delete a Token#

razorpayClient.Customers.deleteToken(customerId, tokenId);

Subscriptions#

Create a Plan#

JSONObject request = new JSONObject();
request.put("period", "weekly");
request.put("interval", 1);

JSONObject item = new JSONObject();
item.put("name", "Test Weekly 1 plan");
item.put("description", "Description for the weekly 1 plan");
item.put("amount", 600);
item.put("currency", "INR");
request.put("item", item);
Plan plan = razorpayClient.Plans.create(request);

Fetch Plan by ID#

Plan plan = razorpayClient.Plans.fetch("<plan_id>");

Fetch all Plans#

List<Plan> listPlans = razorpayClient.Plans.fetchAll();

Create a Subscription#

JSONObject request = new JSONObject();
request.put("plan_id", "<plan_id>");
request.put("customer_notify", 1);
request.put("total_count", 6);
request.put("start_at", 1495995837);

JSONArray addons = new JSONArray();
JSONObject addon = new JSONObject();
JSONObject item = new JSONObject();
item.put("name", "Delivery charges");
item.put("amount", 30000);
item.put("currency", "INR");
addon.put("item", item);
addons.put(addon);
request.put("addons", addons);

Subscription subscription = razorpayClient.Subscriptions.create(request);

Fetch Subscription by ID#

Subscription subscription = razorpayClient.Subscriptions.fetch("<subscription_id>");

Fetch all Subscriptions#

 List<Subscription> listSubscriptions = razorpayClient.Subscriptions.fetchAll();

Cancel a Subscription#

 Subscription subscription = razorpayClient.Subscriptions.cancel("<subscription_id>");

Create an Add-on#

JSONObject request = new JSONObject();
request.put("quantity", 2);

JSONObject addonItem = new JSONObject();
addonItem.put("name", "Extra Chair");
addonItem.put("amount", 30000);
addonItem.put("currency", "INR");
request.put("item", addonItem);

Addon addon = razorpayClient.Subscriptions.createAddon(<subscription_id>, request);

Fetch Add-on by ID#

Addon addon = razorpayClient.Addons.fetch(<addon_id>);

Delete an Add-on#

razorpayClient.Addons.delete(<addon_id>);

Payment Links#

Create an Subscription Link#

JSONObject lineItem = new JSONObject();
lineItem.put("amount", 100); // Note: The amount should be in paise.
lineItem.put("name", "name_invoice");

JSONArray lineItems = new JSONArray();
lineItems.put(lineItem);

JSONObject request = new JSONObject();
request.put("line_items", lineItems);
request.put("date", 1480768625); // Timestamp in seconds
request.put("currency", "INR");
request.put("sms_notify", "0");

Invoice invoice = razorpayClient.Invoices.create(request);
Fetch Subscription Link by ID#
Invoice invoice = razorpayClient.Invoices.fetch("invoice_id");

Fetch all Subscription Links#

List<Invoice> invoices = razorpayClient.Invoices.fetchAll();

Cancel a Subscription Link#

Invoice invoice = razorpayClient.Invoices.cancel("invoice_id");

Create an Invoice#

JSONObject lineItem = new JSONObject();
lineItem.put("amount", 100);
lineItem.put("name", "name_invoice");

JSONArray lineItems = new JSONArray();
lineItems.put(lineItem);

JSONObject request = new JSONObject();
request.put("line_items", lineItems);
request.put("date", 1480768625);
request.put("currency", "INR");
request.put("sms_notify", "0");

Invoice invoice = razorpayClient.Invoices.create(request);

Fetch Invoice by ID#

Invoice invoice = razorpayClient.Invoices.fetch("invoice_id");
Fetch all Invoices#
List<Invoice> invoices = razorpayClient.Invoices.fetchAll();
Cancel an Invoice#
Invoice invoice = razorpayClient.Invoices.cancel("invoice_id");

Create a Virtual Account#

JSONObject request = new JSONObject();
JSONArray receiverTypeArray = new JSONArray();
receiverTypeArray.put("bank_account");
request.put("receiver_types", receiverTypeArray);
JSONObject notes = new JSONObject();
notes.put("receiver_key", "receiver_value");
request.put("notes", notes);
request.put("description", "First Virtual Account");
VirtualAccount virtualAccount = razorpayClient.VirtualAccounts.create(request);

Fetch Virtual Account by ID#

VirtualAccount virtualAccount = razorpayClient.VirtualAccounts.fetch(" 
<virtual_account_id>");

Fetch all Virtual Accounts#

List<VirtualAccount> virtualAccountList = razorpayClient.VirtualAccounts.fetchAll();
Close a Virtual Account#
VirtualAccount virtualAccount = razorpayClient.VirtualAccounts.close(" 
<virtual_account_id>");
List Payments for a Virtual Account#
List<Payment> paymentList = 
razorpayClient.VirtualAccounts.fetchPayments("virtual_account_id");

Create a Transfer#

JSONObject request = new JSONObject();

JSONArray transfers = new JSONArray();

JSONObject transfer = new JSONObject();
transfer.put("amount", <amount>);
transfer.put("currency", "INR");
transfer.put("account", <account_id>);

transfers.put(transfer);
request.put("transfers", transfers);
List<Transfer> transfers = razorpayClient.Payments.transfer("payment_id", request);

Create a Direct Transfer#

JSONObject request = new JSONObject();
request.put("amount", <amount>);
request.put("currency", "INR");
request.put("account", <account_id>);
Transfer transfer = razorpayClient.Transfers.create(request);

Edit a Transfer#

JSONObject request = new JSONObject();
request.put("on_hold", true);
Transfer transfer = razorpayClient.Transfers.edit(request);

Fetch Bank Transfer Payments#

BankTransfer bankTransfer = razorpayClient.Payments.fetchBankTransfers("payment_id");

Fetch all Transfers for a Payment#

List<Transfers> transfers = razorpayClient.Payments.fetchAllTransfers("payment_id");
Fetch a Transfer by ID#
Transfer transfer = razorpayClient.Transfers.fetch("transfer_id");

Fetch all Transfers#

List<Transfer> transfers = razorpayClient.Transfers.fetchAll();

Create a Reversal of a Transfer#

JSONObject request = new JSONObject();
request.put("amount", <amount>);

Reversal reversal = razorpayClient.Transfers.reversal("transfer_id", request);

Webhooks#

Validate Webhook Signature# You can verify the signature of the received webhook:

Utils.verifyWebhookSignature("<webhook_payload>", "<webhook_signature>", " 
<webhook_secret>");

Utility#

Verify Signature for a Payment# You can use the Utils class to verify the signature received in response to a payment made using Orders API.

JSONObject options = new JSONObject();
options.put("razorpay_order_id", "<order_id>");
options.put("razorpay_payment_id", "<payment_id>");
options.put("razorpay_signature", "<signature>");
Utils.verifyPaymentSignature(paymentResponse, "<key_secret>");

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);

SAMPLE APPLICATION

Integrate and Run the Sample Application#

Create a Checkout form using Razorpay Checkout Integration.

Accept razorpay_payment_id parameter in the form submission.

Run the capture code to capture the payment.

Edit the key inside index.ftl.

Add your <key_id> and <key_secret> in the server.yml file. Refer to the Generate API Key to learn how to generate keys.

Build the test application using the following command:

mvn clean install

Run the test application using the following command:

java -jar target/razorpay-java-testapp-1.0-SNAPSHOT.jar server server.yml

Note:

If you want to re-use this as your final code, follow the below steps:

Edit the key inside index.ftl file. Edit the <key_id> and the <key_secret> inside server.yml file. Use the live keys when using the application to accept live payments.