other|2019最新PayPal二代支付接口Smart Payment Buttons开发

在2018年的时候PayPal推出了新一代的支付接口,去年开始测试然后今年正式上线,接着就是市场推广,帮助老客户升级及推广新客户。新的接口版本是v2 checkout,名字叫smart payment button,简称spb。对比v1接口最大的区别主要集中在两点,一是简化了支付流程,更便捷。二是优化了信用卡通道,改变了过去信用卡复杂的方式。

这边接到任务需要开发机遇mangeto1.9和magento2的插件,以便于帮助老客户升级。于是就花了几天时间开始研究这个问题,具体过程如下:

1、官方文档演示及说明:https://developer.paypal.com/docs/checkout/,可以看到大体流程就是通过前台点击请求create api生成订单然后跳转登陆客户授权,授权成功后返回到api,通过capture获取交易详细信息并入库;

2、通过composer下载sdk,composer require paypal/paypal-checkout-sdk 1.0.0

github地址:https://github.com/paypal/Checkout-PHP-SDK,建议通过composer下载,下载后的文件夹是一个vendor,自带自动加载方法,而git里面clone下来的之后PayPal的文件夹。

3、v2 checkou api接口文档:https://developer.paypal.com/docs/api/orders/v2/

目前通过研究发现,大致流程基本上是三步,一创建订单,create order ,二、用户授权后capture订单,三、验证订单execute并获取详细信息写入数据库。

代码演示如下:

a、html页面,通过js sdk 创建订单并capture订单信息,传递orderId到后台程序:

<!DOCTYPE html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <script
            src="https://www.paypal.com/sdk/js?client-id=*****">
    </script>

</head>
<body>

<div id="paypal-button-container"></div>

<script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);
        // Call your server to save the transaction
        return fetch('execute.php', {
          method: 'post',
          headers: {
            'content-type': 'application/json'
          },
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    }
  }).render('#paypal-button-container');
</script>
 
</body>

b、后台接收orderId,并验证订单信息获取订单信息写入数据库;

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/28
 * Time: 14:31
 */
namespace Sample;

require __DIR__ . '/vendor/autoload.php';
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
// Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
// $response->result->id gives the orderId of the order created above
$clientId = "*****";
$clientSecret = "***********";

$environment = new SandBoxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
$content=file_get_contents("php://input");
$json=json_decode($content,true);
$orderID=$json['orderID'];
file_put_contents("1.txt",$orderID,FILE_APPEND);
$request = new OrdersCaptureRequest($orderID);
$request->prefer('return=representation');
try {
    // Call API with your client and get a response for your call
    $response = $client->execute($request);

    // If call returns body in response, you can get the deserialized version from the result attribute of the response
    print_r($response);
    //验证通过获取到订单的详细信息可以写入到数据库
}catch (HttpException $ex) {
    echo $ex->statusCode;
    print_r($ex->getMessage());
}

通过以上步骤基本就走完了一个订单的流程。这里特别说明,前端js和后台程序打通的时候本地测试可能会报错,出现如下提示:

SSL certificate: unable to get local issuer certificate

解决办法:

到http://curl.haxx.se/ca/cacert.pem下载pem文件,并将文件拷贝到D:\phpStudy\PHPTutorial\cacert.pem

在php.ini
增加
curl.cainfo = "D:\phpStudy\PHPTutorial\cacert.pem"

通过测试发现,spb的主要功能是把create和capture方法通过js sdk的方式集成到了一起,这样前端可以直接生成订单,后续只需要进行验证即可。以上代码就是采用这种方式,其实还有另外两种方式,结合起来汇总如下:

A、create和capture全都通过js sdk实现,然后后端只需要execute并入库即可。

B、cteate通过ajax请求php实现并返回临时信息给js,前端接收信息后跳转登陆并授权,然后通过js检测授权后capture订单Id并传递给后端,后端根据Id execute入库。

C、不走前端,全部后端执行,先create,然后跳转网址授权,返回后capture,接着execute。

前端或者后端传递的订单组合参数列表:https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/#on-the-server,但其中没有订单号。

现在依旧存在的几个问题如下:

1、订单号的参数怎么传递;
2、消息通知如何获取,状态变更;
3、特殊状态如预授权订单等设置及处理;

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值