JavaEE PayPal 全球支付快速集成

1,776 阅读2分钟

JavaEE PayPal 全球支付快速集成

PayPal 原先的REST集成也需要Developer进行相关操作才能进行调用。但PayPal推出了Braintree SDK,那服务端集成起来,那就是一个字:

一. PayPal workflow

workflow

图解:

  1. App端或H5端 请求 服务端 获取 公开客户端令牌
  • 通过平台获取AccessToken(分为product环境和Sandbox环境)
  • 服务端通过AccessToken生成并 公开客户端令牌(用于App端或H5端调用)
  1. 服务端 响应 App端或H5端 公开客户端令牌
  2. App端或H5端 根据公开客户端令牌拉起支付界面
  • 客户填写的相关信息(可选择PayPal支付或其他全球支付类型)
  • Braintree 将在客户填写(即授权)后,响应一个paymentMethodNonce给 App端或H5端
  1. App端或H5端 将paymentMethodNonce回传服务端
  2. 服务端 paymentMethodNonceamount(金额) 创建交易

整个流程很简洁以及清晰。

二. 具体实现

2.1 导入Braintree SDK依赖

<!--paypal-->
<dependency>
    <groupId>com.braintreepayments.gateway</groupId>
    <artifactId>braintree-java</artifactId>
    <version>2.73.0</version>
</dependency>

2.2 获取AccessToken

PayPal Develper 平台

AccessToken位置:

Dashboard -> My Apps & Credentials -> 点击 Sandbox Accounts & Live Account 中的邮箱账号

图解:

one

two

2.3 生成公开客户端令牌

直接使用Braintree SDK集成的Gateway解析获取。

String accessToken = "access_token$sandbox$catalpaflat1234567890";
BraintreeGateway gateway = new BraintreeGateway(accessToken); 
String publicAccessToken = gateway.clientToken().generate();

2.4 创建交易(即下单)

App端或H5端 获取客户信息之后响应的paymentMethodNonce以及订单金额信息。

BigDecimal amount = new BigDecimal("1000.00");
String paymentMethodNonce = "";
String ordernum = "CF1234567890";
TransactionRequest request = new TransactionRequest().
            //付款金额
            amount(amount).
            //指定货币
            merchantAccountId("USD").
            //付款方式随机数
            paymentMethodNonce(paymentMethodNonce).
            //付款订单
            orderId(ordernum).
            descriptor().
            //订单描述
            name("Descriptor displayed in customer CC statements. 22 char max").
            done().
        //帐单邮寄地址
        shippingAddress()
            .firstName("Jen")
            .lastName("Smith")
            //国家
            .company("Braintree")
            //街道
            .streetAddress("1 E 1st St")
            //详细地址
            .extendedAddress("Suite 403")
            //未知
            .locality("Bartlett")
            //地区
            .region("IL")
            //邮政编码
            .postalCode("60103")
            //国家代码
            .countryCodeAlpha2("US")
            .done().
        //附加信息,当成功创建交易时,附加字段
        options().
            paypal().
            customField("PayPal custom field").
            description("Description for PayPal email receipt").
            done().
            storeInVaultOnSuccess(true).
            done();

Result<Transaction> result = gateway.transaction().sale(request);

**注⚠️:**如果不想在测试中添加帐单邮寄地址,或者您想要使用特定的帐单邮寄地址ID,请使用:

fake-valid-no-billing-address-nonce

具体代码实现:

TransactionRequest request = new TransactionRequest()
  .amount(new BigDecimal("10.00"))
  .paymentMethodNonce("fake-valid-nonce")
  .options()
    .submitForSettlement(true)
    .done();

Result<Transaction> result = gateway.transaction().sale(request);

2.5 校验下单结果

//解析下单是否成功
if (saleResult.isSuccess()) {
    //支付成功
    Transaction transaction = saleResult.getTarget();
    System.out.println("Success ID: " + transaction.getId());
} else {
    ValidationErrors errors = saleResult.getErrors();
    StringBuilder errorResult = new StringBuilder();
    //校验结果
    List<ValidationError> allDeepValidationErrors = errors.getAllDeepValidationErrors();
    int size = 0;
    int errorSize = allDeepValidationErrors.size();
    for (ValidationError error : allDeepValidationErrors) {
        if (size == errorSize) {
            errorResult.append(error.getMessage());
        } else {
            errorResult.append(error.getMessage()).append(",");
        }
        size++;
    }
    System.out.println("errors: " + errorResult.toString());
}