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

时间:2017-01-12 18:29:02

标签: java paypal

我今天一直在寻找示例/教程/文档 这解释了如何使用paypal api和java进行大规模付款。我已经查看了paypal网站,尽管我看到的只是大量支付的概述以及它们如何工作以及它们存在的原因的理论解释。是否有任何资源/教程显示如何使用java和paypal api进行批量支付,实际代码和/或java文档清楚地解释了在沙盒模式下进行批量支付所需的内容?我非常感谢你对此有任何帮助。

1 个答案:

答案 0 :(得分:0)

我将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();

在此回复中,如果成功与否,您可以找到需要访问的link以获取有关此付款的详细信息。

值得注意的是,现在Paypal会立即为多个收件人提供同步处理

相关问题