Thinkphp6接入打通微信小程序支付功能

要实现微信小程序的支付功能,首先必须要有订单支撑。

一 、小程序通过请求接口创建平台订单,订单创建成功后返回小程序拉起支付需要的数据。

Db::startTrans();
//这个是自己平台的订单信息
$orderInfo=[];
//通过JSAPI下单
// 1、请求参数
$postJson = [
    "appid" => '小程序appid',
    "mchid" => '商户号',
    "description" => '商品信息',
    "out_trade_no" => '自己平台的订单号',
    "notify_url" => '用于接收微信支付结果的回调接口',
    "amount" => [
        "total" => 100 //付款金额,单位为分
    ],                    
    "payer" => [
        "openid" => '付款用户的openid',
    ]
];
$time = time();
// 2、头部签名
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
$urlarr = parse_url($url);
$data = json_encode($postJson);
$noncestr = $time;
$key = $this->getSign($data, $urlarr['path'], $noncestr, $time);//签名
$token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', '商户号', '微信支付API证书序列号', $noncestr, $time, $key);//头部信息
$header = [
    'Content-Type:' . 'application/json; charset=UTF-8',
    'Accept:application/json',
    'User-Agent:*/*',
    'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token
];
$resp = http_post($url,$postJson,$header);
$resp = json_decode($resp,true);
if(isset($resp['prepay_id'])){
    $return = [
        "appId"=>'小程序appid',
        "timeStamp"=>$time,
        'package' => 'prepay_id=' . $resp['prepay_id'],
        'paySign' => $this->getWechartSign('小程序appid', $time, $noncestr, 'prepay_id=' . $resp['prepay_id']),//微信支付(小程序)签名
        "nonceStr"=>$time
    ];
    Db::commit();
    return self::jsonStr(['code'=>1,'msg'=>'下单成功','data'=>$return]);
}else{
    Db::rollback();
    return self::jsonStr(['code'=>0,'msg'=>'支付订单创建失败!','data'=>$resp]);
}

代码中用到的签名方法如下

//微信支付签名
public function getSign($data = [], $url, $randstr, $time) {
    $str = "POST" . "\n" . $url . "\n" . $time . "\n" . $randstr . "\n" . $data . "\n";
    $key = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/cert/apiclient_key.pem');//在商户平台下载的秘钥
    $str = $this->getSha256WithRSA($str, $key);
    return $str;
}
//调起支付的签名
public function getWechartSign($appid, $timeStamp, $noncestr, $prepay_id) {
    $str = $appid . "\n" . $timeStamp . "\n" . $noncestr . "\n" . $prepay_id . "\n";
    $key = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/cert/apiclient_key.pem');
    $str = $this->getSha256WithRSA($str, $key);
    return $str;
}
public function getSha256WithRSA($content, $privateKey) {
    $binary_signature = "";
    $algo = "SHA256";
    openssl_sign($content, $binary_signature, $privateKey, $algo);
    $sign = base64_encode($binary_signature);
    return $sign;
}

二、微信小程序获取到上一步返回的调起微信支付的配置信息,然后通过wx.requestPayment调起支付界面。

wx.requestPayment({
    "timeStamp": timeStamp+'',//必须字符串类型
    "nonceStr":  nonceStr+'',//必须字符串类型
    "package": package,
    "signType": "RSA",
    "paySign": paySign,
    "success":function(res){
        if (res.errMsg === 'requestPayment:ok') {
            wx.showModal({
            title: '支付成功',
            showCancel:false,
            complete: (res) => {
                //支付成功后的操作
            })
        }else{
            wx.showToast({
                icon:"none",
                title: '支付失败',
            })
        }
    },
    "fail":function(res){
        if(res.errMsg == 'requestPayment:fail cancel'){
            wx.showToast({
                icon:"none",
                title: '支付已取消',
            })
        }else{
            wx.showToast({
                icon:"none",
                title: res.errMsg
            })
        }
    }
});

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生²

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值