Ping++ 接入微信小程序支付的流程

更新于:2018年12月26日 09:41:22

微信于 2017 年 1 月 9 日 正式推出了「微信小程序」,Ping++ 也在第一时间宣布支持「微信小程序支付」。

写在前面

小程序本身类似于一个基于网页的轻型应用,所以他的支付接口在技术层面与微信公众号是几乎完全相同的,区别仅在于以下三点:

1. 支付渠道参数不同:同一家公司在申请微信公众号支付(wx_pub)和申请微信小程序支付(wx_lite),获得的渠道参数是不同的。

2. 获取 open_id 的方式不同:传统微信公众号支付场景下 open_id 的获取,是需要先代码实现获取 code 的步骤,再通过 code 得到 open_id ;而小程序支付场景下,小程序本身提供了获取 code 的 api,直接通过调用 api 即可获取 code ,再通过 code 得到 open_id ,比微信公众号支付少了一个步骤,具体请参考下文。

3. 微信小程序目前只支持微信支付。小程序如何申请微信支付?

接入流程

一、准备工作:

1、在微信申请到微信小程序支付(wx_lite )的参数,参考 小程序支付参数填写指南 配置渠道参数。

2、下载 Ping++ 包含「微信小程序支付」最新版 H5-SDK

3、下载 Ping++ 的 Server-SDK (以 PHP 为例)

4、因为「微信小程序」只支持 HTTPS 协议,请确保你的服务器支持 HTTPS 协议,并在小程序的 [开发平台 => 设置 => 开发设置 ] 上配置好服务器域名。

二、Server 端的接入

1、下载 Ping++ 的 Server-SDK

2、获取 open_id :(唯一与微信公众号支付接入不一样的地方) open_id 是根据微信小程序提供的获取 code 的 api 来获取的(code 的获取方法会在「三、Client 端接入」中讲到),Ping++ 提供了获取 open_id 的方法 :

//设置 Ping++ 的 API Key
\Pingpp\Pingpp::setApiKey('ApiKey');
//设置 Ping++ 私钥    
\Pingpp\Pingpp::setPrivateKeyPath('PrivateKey');    
//[小程序开发平台 => 设置 => 开发设置]    
$wx_app_id = 'AppID(小程序ID) ';    
//[小程序开发平台 => 设置 => 开发设置]     
$wx_app_secret = 'AppSecret(小程序密钥)';    
$code = $_GET['code'];    
//调用 Ping++ SDK  获取open_id    
$open_id = \Pingpp\WxLiteOAuth::getOpenid($wx_app_id, $wx_app_secret, $code);    
echo $open_id;     
exit;

3、调用 Ping++ 的 SDK 创建 Charge(与其他渠道的接入一样)

    $input_data = json_decode(file_get_contents('php://input'), true);
    $channel = strtolower($input_data['channel']);
    $amount = $input_data['amount'];
    $orderNo = substr(md5(time()), 0, 12);
    $open_id = $input_data['open_id'];
    $extra = array();
    switch ($channel) {
      case 'wx_lite':
        $extra = array(
            'open_id' => $open_id// 请求参数中的open_id
        );
        break;
    }
    try {
    $ch = \Pingpp\Charge::create(
        array(
            //请求参数字段规则,请参考 API 文档:https://www.pingxx.com/api#api-c-new
            'subject'   => 'Your Subject',
            'body'      => 'Your Body',
            'amount'    => $amount,//订单总金额, 人民币单位:分(如订单总金额为 1 元,此处请填 100)
            'order_no'  => $orderNo,// 推荐使用 8-20 位,要求数字或字母,不允许其他字符
            'currency'  => 'cny',
            'extra'     => $extra,
            'channel'   => $channel,// 支付使用的第三方支付渠道取值,请参考:https://www.pingxx.com/api#api-c-new
            'client_ip' => $_SERVER['REMOTE_ADDR'],// 发起支付请求客户端的 IP 地址,格式为 IPV4,如: 127.0.0.1
            'app'       => array('id' => APP_ID)
        )
    );
    echo $ch;// 输出 Ping++ 返回的支付凭据 Charge
} catch (\Pingpp\Error\Base $e) {
    // 捕获报错信息
    if ($e->getHttpStatus() != NULL) {
        header('Status: '. $e->getHttpStatus());
        echo $e->getHttpBody();
    } else {
        echo $e->getMessage();
    }
}

三、Client 端接入(微信小程序接入)

1、将我们之前下载好的 Ping++ 的 H5 端 SDK 导入到工程中(如下图)

0015863b8879af43f4200549851f210

     

2、在 index.js 中的点击事件里添加以下方法(用来获取 open_id

getOpenId:function () {
    wx.login({
        success: function (res) {
            if (res.code) {
                //发起网络请求 获取 open_id
                wx.request({
                    url: '填写你之前写好的 获取 open_id 的URL',//例如:https://api.weixin.qq.com/sns/jscode2session?appid=小程序AppID&secret=小程序secret&js_code='+res.code+'&grant_type=authorization_code
                    data: {
                        code: res.code
                    },
                     success: function (res) {
                        var open_id = res.data
                        console.log(open_id);
                    }
                });
            } else {
                console.log('获取用户登录态失败!' + res.errMsg)
            }
    }
});

     

3、调用 Ping++ 的 H5-SDK 实现支付功能(与微信公众号支付的调用一样)

var Pingpp = require('../../utils/pingpp.js')
    wx.request({
        url: '填写你之前写好的 获取 charge 的URL',
        data: {
            channel: 'wx_lite',//渠道名
            amount : 1//支付金额
            open_id: open_id //之前获取到的open_id
        }, success: function (res) {
            var charge = res.data;
            Pingpp.createPayment(charge, function (result, err) {
                console.log(result);
                console.log(err.msg);
                console.log(err.extra);
                if (result == "success") {
                    // 只有微信小程序 wx_lite 支付成功的结果会在这里返回
                } else if (result == "fail") {
                    // charge 不正确或者微信小程序支付失败时会在此处返回
                } else if (result == "cancel") {
                    // 微信小程序支付取消支付
                }
            });
        }
    })


这样,微信小程序支付就接入成功了!

    您需要登录后才可以回复