gpt4 book ai didi

java - 如何使用 java 和 paypal api 在沙盒模式下进行批量支付?

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:33 28 4
gpt4 key购买 nike

我今天一直在寻找示例/教程/文档其中解释了如何使用 paypal api 和 java 进行批量支付。我查看了 paypal 网站,尽管我所看到的只是对什么是批量支付的概述以及对它们如何工作以及为什么存在的理论解释。是否有任何资源/教程显示如何使用实际代码和/或 java 文档使用 java 和 paypal api 进行批量支付,这些文档清楚地解释了在沙盒模式下进行批量支付所需的条件?如果对此有任何帮助,我将不胜感激。

最佳答案

我将 Mass Payout API 与 Spring Boot 集成在一起。以下是主要摘录,但不依赖于框架。

首先,我们添加合适的 Maven 依赖项:

<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.13.1</version>
</dependency>

现在,我们可以创建一个 Payout 对象并将多个收件人添加为 PayoutItem,例如:

Payout payout = new Payout();

PayoutSenderBatchHeader senderBatchHeader = new PayoutSenderBatchHeader();
senderBatchHeader.setEmailSubject("PayPal Email Header");

Currency amount = new Currency();
//Transaction of 1 unit with US Dollars as unit.
amount.setValue("1").setCurrency("USD");

一旦完成,您就可以开始添加收件人了:

PayoutItem sendTo = new PayoutItem();

//This can be "Phone" and specify PayPal mobile number on setReceiver
sendTo.setRecipientType("Email")
.setReceiver("user@email.com")
.setNote("Thanks.").setAmount(amount);

List<PayoutItem> items = new ArrayList<>();
items.add(sendTo);
//Add more recipients to items list but with same currency as handling
//different currencies in single batch isn't possible

payout.setSenderBatchHeader(senderBatchHeader).setItems(items);

至此,大功告成,最后执行请求:

//paypalMode can be either "sandbox" or "live"
APIContext apiContext = new APIContext(
paypalClientId, paypalClientSecret, paypalMode);

PayoutBatch batch = payout.create(apiContext);
String batchId = batch.getBatchHeader().getPayoutBatchId();

支付请求现在已经执行,但是是异步的。检查 JSON 字符串响应为:

String jsonResponseStr = Payout.getLastResponse();

在此响应中,您可以找到一个链接,需要访问该链接以了解有关此付款的详细信息(无论是否成功)。

值得注意的是,Paypal 现在不支持同时对多个收件人进行同步处理。

关于java - 如何使用 java 和 paypal api 在沙盒模式下进行批量支付?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com