英文:
RazorPay Integration with React JS and Java application
问题
可以在使用React.js(前端)和Java 8(后端)的应用程序中集成Razorpay吗?
在Razorpay的网站上,他们没有关于如何与React.js应用程序集成的文档。
我对React.js和支付网关集成都很新,请问如果有人在React.js中使用Razorpay进行集成,是否可以建议一些好的方法/可用文档?
或者,如果有人可以推荐任何一个易于与React.js和Java应用程序集成的好的支付网关,我会非常感谢!
英文:
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
得分: 3
为了处理后端的 API JAVA,仅使用 Java 作为后端就足够了。对于前端,我们使用 React,使用 JavaScript 编写 HTML。我们依赖 JavaScript 的强大功能来生成依赖于某些数据的 HTML,而不是通过增强 HTML 来使其与该数据配合工作。增强 HTML 是其他 JavaScript 框架通常所做的。
因此,要使用该 API,您需要:
先决条件:
Java 1.7 或更高版本
安装:
您可以使用 Maven 或 Gradle 安装 Razorpay。
Maven:
在系统上下载并安装 Maven。
从 GitHub 的 Releases 部分下载最新的源代码 zip 文件。
解压文件,并将此依赖项添加到您的项目的项目对象模型 (POM) 中。
<dependency>
<groupId>com.razorpay</groupId>
<artifactId>razorpay-java</artifactId>
<version>x.y.z</version> <!-- x.y.z = 您想要安装的版本 -->
</dependency>
Gradle:
在系统上下载并安装 Gradle。
从 GitHub 的 Releases 部分下载最新的源代码 zip 文件。
解压文件,并将此依赖项添加到项目的构建文件中:
compile "com.razorpay:razorpay-java:x.y.z" // x.y.z = 您想要安装的版本
示例代码:
初始化:
使用 key_id 和 key_secret 创建 RazorpayClient。
RazorpayClient razorpayClient = new RazorpayClient("key_id", "key_secret");
生成 API 密钥:
使用适当的凭据登录您的仪表板。
选择要为其生成 API 密钥的模式(测试或生产)。
注意:
您必须为测试模式和生产模式分别生成不同的 API 密钥。测试模式中不使用真实货币。
转到 设置 → API 密钥 → 生成密钥,以为所选模式生成密钥。
添加自定义标头:
注意:
这是一个可选步骤。
要将自定义标头添加到请求中:
Map<String, String> headers = new HashMap<String, String>();
razorpayClient.addHeaders(headers);
订单:
注意:
点击此处以获取请求参数以及示例请求和响应。
创建订单:
JSONObject options = new JSONObject();
options.put("amount", 5000);
options.put("currency", "INR");
options.put("receipt", "txn_123456");
Order order = razorpayClient.Orders.create(options);
根据 ID 获取订单:
Order order = razorpayClient.Orders.fetch("order_id");
获取所有订单:
List<Order> orders = razorpayClient.Orders.fetchAll();
为付款获取订单:
List<Payment> payments = razorpayClient.Orders.fetchPayments("order_id");
捕获付款:
JSONObject options = new JSONObject();
options.put("amount", 1000);
options.put("currency", "INR");
razorpayClient.Payments.capture("payment_id", options);
根据 ID 获取付款:
Payment payment = razorpayClient.Payments.fetch("payment_id");
int amount = payment.get("amount");
String id = payment.get("id");
Date createdAt = payment.get("created_at");
根据 ID 获取所有付款:
List<Payment> payments = razorpayClient.Payments.fetchAll();
创建完全退款:
JSONObject refundRequest = new JSONObject();
refundRequest.put("payment_id", <payment_id>);
Refund refund = razorpayClient.Payments.refund(refundRequest);
创建部分退款:
JSONObject refundRequest = new JSONObject();
refundRequest.put("amount", <amount>);
refundRequest.put("payment_id", <payment_id>);
Refund refund = razorpay.Payments.refund(refundRequest);
根据 ID 获取退款:
Refund refund = razorpayClient.Refunds.fetch("refund_id");
获取所有退款:
List<Refund> refunds = razorpayClient.Refunds.fetchAll();
针对付款获取退款:
Refund refund = razorpayClient.Payments.fetchRefund("refund_id");
针对特定付款获取所有退款:
JSONObject refundRequest = new JSONObject();
refundRequest.put("payment_id", <payment_id>);
List<Refund> refund = razorpayClient.Payments.fetchAllRefunds(refundRequest);
获取卡片详情:
Card card = razorpayClient.Cards.fetch(id);
创建客户:
JSONObject request = new JSONObject();
request.put("name", <name>);
request.put("email", <email>);
Customer customer = razorpayClient.Customers.create(request);
根据 ID 获取客户:
Customer customer = razorpayClient.Customers.fetch(customerId);
编辑客户:
JSONObject request = new JSONObject();
request.put("name", <name>);
request.put("email", <email>);
Customer customer = razorpayClient.Customers.edit(customerId, request);
获取令牌:
Token token = razorpayClient.Customers.fetchToken(customerId, tokenId);
获取客户的令牌:
List<Token> tokens = razorpayClient.Customers.fetchTokens(customerId);
删除令牌:
razorpayClient.Customers.deleteToken(customerId, tokenId);
订阅:
创建计划:
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);
根据 ID 获取计划:
Plan plan = razorpayClient.Plans.fetch("<plan_id>");
获取所有计划:
List<Plan> listPlans = razorpayClient.Plans.fetchAll();
创建订阅:
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
<details>
<summary>英文:</summary>
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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论