RazorPay与React JS和Java应用集成

huangapple go评论77阅读模式
英文:

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.

    &lt;dependency&gt;
    &lt;groupId&gt;com.razorpay&lt;/groupId&gt;
    &lt;artifactId&gt;razorpay-java&lt;/artifactId&gt;
    &lt;version&gt;x.y.z&lt;/version&gt; //x.y.z = the version you want to install
    &lt;/dependency&gt;

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 &quot;com.razorpay:razorpay-java:x.y.z&quot; //x.y.z = the version you want to install

Sample Code

Initialization#

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

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&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;();
    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(&quot;amount&quot;, 5000);
    options.put(&quot;currency&quot;, &quot;INR&quot;);
    options.put(&quot;receipt&quot;, &quot;txn_123456&quot;);
    Order order = razorpayClient.Orders.create(options);

Fetch Order by ID#

    Order order = razorpayClient.Orders.fetch(&quot;order_id&quot;);

Fetch all Orders#

    List&lt;Order&gt; orders = razorpayClient.Orders.fetchAll();

Fetch Order for a Payment#

    List&lt;Payment&gt; payments = razorpayClient.Orders.fetchPayments(&quot;order_id&quot;);

Capture a Payment#

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


Fetch Payment by ID#

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

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

Fetch all Payments#

    List&lt;Payment&gt; payments = razorpayClient.Payments.fetchAll();

Create a Full Refund#

    JSONObject refundRequest = new JSONObject();
    refundRequest.put(&quot;payment_id&quot;, &lt;payment_id&gt;);
    Refund refund = razorpayClient.Payments.refund(refundRequest);

Create a Partial Refund#

    JSONObject refundRequest = new JSONObject();
    refundRequest.put(&quot;amount&quot;, &lt;amount&gt;);
    refundRequest.put(&quot;payment_id&quot;, &lt;payment_id&gt;);
    Refund refund = razorpay.Payments.refund(refundRequest);


Fetch Refund by ID#

    Refund refund = razorpayClient.Refunds.fetch(&quot;refund_id&quot;);

Fetch all Refunds#

    List&lt;Refund&gt; refunds = razorpayClient.Refunds.fetchAll();

Fetch Refund against Payment#

    Refund refund = razorpayClient.Payments.fetchRefund(&quot;refund_id&quot;);

Fetch all Refunds against a Payment#

    JSONObject refundRequest = new JSONObject();
    refundRequest.put(&quot;payment_id&quot;, &lt;payment_id&gt;);
    List&lt;Refund&gt; refund = razorpayClient.Payments.fetchAllRefunds(refundRequest);


Fetch Card Details#

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



Create a Customer#

    JSONObject request = new JSONObject();
    request.put(&quot;name&quot;, &lt;name&gt;);
    request.put(&quot;email&quot;, &lt;email&gt;);
    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(&quot;name&quot;, &lt;name&gt;);
    request.put(&quot;email&quot;, &lt;email&gt;);
    Customer customer = razorpayClient.Customers.edit(customerId, request);



Fetch a Token#

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

Fetch Token for a Customer#

    List&lt;Token&gt; tokens = razorpayClient.Customers.fetchTokens(customerId);

Delete a Token#

    razorpayClient.Customers.deleteToken(customerId, tokenId);

Subscriptions#



Create a Plan#

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

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

Fetch Plan by ID# 

    Plan plan = razorpayClient.Plans.fetch(&quot;&lt;plan_id&gt;&quot;);

Fetch all Plans#

    List&lt;Plan&gt; listPlans = razorpayClient.Plans.fetchAll();

Create a Subscription#

    JSONObject request = new JSONObject();
    request.put(&quot;plan_id&quot;, &quot;&lt;plan_id&gt;&quot;);
    request.put(&quot;customer_notify&quot;, 1);
    request.put(&quot;total_count&quot;, 6);
    request.put(&quot;start_at&quot;, 1495995837);
   
    JSONArray addons = new JSONArray();
    JSONObject addon = new JSONObject();
    JSONObject item = new JSONObject();
    item.put(&quot;name&quot;, &quot;Delivery charges&quot;);
    item.put(&quot;amount&quot;, 30000);
    item.put(&quot;currency&quot;, &quot;INR&quot;);
    addon.put(&quot;item&quot;, item);
    addons.put(addon);
    request.put(&quot;addons&quot;, addons);

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

Fetch Subscription by ID#

    Subscription subscription = razorpayClient.Subscriptions.fetch(&quot;&lt;subscription_id&gt;&quot;);

Fetch all Subscriptions#

     List&lt;Subscription&gt; listSubscriptions = razorpayClient.Subscriptions.fetchAll();

Cancel a Subscription#

     Subscription subscription = razorpayClient.Subscriptions.cancel(&quot;&lt;subscription_id&gt;&quot;);

Create an Add-on#

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

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

    Addon addon = razorpayClient.Subscriptions.createAddon(&lt;subscription_id&gt;, request);

 Fetch Add-on by ID#

    Addon addon = razorpayClient.Addons.fetch(&lt;addon_id&gt;);

Delete an Add-on#

    razorpayClient.Addons.delete(&lt;addon_id&gt;);

Payment Links#



Create an Subscription Link#

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

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

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

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

Fetch all Subscription Links#

    List&lt;Invoice&gt; invoices = razorpayClient.Invoices.fetchAll();

Cancel a Subscription Link#

    Invoice invoice = razorpayClient.Invoices.cancel(&quot;invoice_id&quot;);



Create an Invoice#

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

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

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

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

Fetch Invoice by ID#
 

    Invoice invoice = razorpayClient.Invoices.fetch(&quot;invoice_id&quot;);
    Fetch all Invoices#
    List&lt;Invoice&gt; invoices = razorpayClient.Invoices.fetchAll();
    Cancel an Invoice#
    Invoice invoice = razorpayClient.Invoices.cancel(&quot;invoice_id&quot;);


Create a Virtual Account#

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

Fetch Virtual Account by ID#

    VirtualAccount virtualAccount = razorpayClient.VirtualAccounts.fetch(&quot; 
    &lt;virtual_account_id&gt;&quot;);

Fetch all Virtual Accounts#

    List&lt;VirtualAccount&gt; virtualAccountList = razorpayClient.VirtualAccounts.fetchAll();
    Close a Virtual Account#
    VirtualAccount virtualAccount = razorpayClient.VirtualAccounts.close(&quot; 
    &lt;virtual_account_id&gt;&quot;);
    List Payments for a Virtual Account#
    List&lt;Payment&gt; paymentList = 
    razorpayClient.VirtualAccounts.fetchPayments(&quot;virtual_account_id&quot;);


Create a Transfer#

    JSONObject request = new JSONObject();
    
    JSONArray transfers = new JSONArray();
    
    JSONObject transfer = new JSONObject();
    transfer.put(&quot;amount&quot;, &lt;amount&gt;);
    transfer.put(&quot;currency&quot;, &quot;INR&quot;);
    transfer.put(&quot;account&quot;, &lt;account_id&gt;);
    
    transfers.put(transfer);
    request.put(&quot;transfers&quot;, transfers);
    List&lt;Transfer&gt; transfers = razorpayClient.Payments.transfer(&quot;payment_id&quot;, request);

Create a Direct Transfer#

    JSONObject request = new JSONObject();
    request.put(&quot;amount&quot;, &lt;amount&gt;);
    request.put(&quot;currency&quot;, &quot;INR&quot;);
    request.put(&quot;account&quot;, &lt;account_id&gt;);
    Transfer transfer = razorpayClient.Transfers.create(request);

Edit a Transfer#

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

Fetch Bank Transfer Payments#

    BankTransfer bankTransfer = razorpayClient.Payments.fetchBankTransfers(&quot;payment_id&quot;);

Fetch all Transfers for a Payment#

    List&lt;Transfers&gt; transfers = razorpayClient.Payments.fetchAllTransfers(&quot;payment_id&quot;);
    Fetch a Transfer by ID#
    Transfer transfer = razorpayClient.Transfers.fetch(&quot;transfer_id&quot;);

Fetch all Transfers#

    List&lt;Transfer&gt; transfers = razorpayClient.Transfers.fetchAll();

Create a Reversal of a Transfer#

    JSONObject request = new JSONObject();
    request.put(&quot;amount&quot;, &lt;amount&gt;);
    
    Reversal reversal = razorpayClient.Transfers.reversal(&quot;transfer_id&quot;, request);

Webhooks#


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

    Utils.verifyWebhookSignature(&quot;&lt;webhook_payload&gt;&quot;, &quot;&lt;webhook_signature&gt;&quot;, &quot; 
    &lt;webhook_secret&gt;&quot;);

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(&quot;razorpay_order_id&quot;, &quot;&lt;order_id&gt;&quot;);
    options.put(&quot;razorpay_payment_id&quot;, &quot;&lt;payment_id&gt;&quot;);
    options.put(&quot;razorpay_signature&quot;, &quot;&lt;signature&gt;&quot;);
    Utils.verifyPaymentSignature(paymentResponse, &quot;&lt;key_secret&gt;&quot;);

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(&quot;path&quot;, 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 **&lt;key_id&gt;** and **&lt;key_secret&gt;** 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 **&lt;key_id&gt;** and the **&lt;key_secret&gt;** inside server.yml file. Use the live keys when 
using the application to accept live payments.

</details>



huangapple
  • 本文由 发表于 2020年10月17日 18:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定