要实现微信小程序的支付功能,首先必须要有订单支撑。
一 、小程序通过请求接口创建平台订单,订单创建成功后返回小程序拉起支付需要的数据。
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]);
}
代码中用到的签名方法如下文章来源:https://www.toymoban.com/news/detail-714872.html
//微信支付签名
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调起支付界面。文章来源地址https://www.toymoban.com/news/detail-714872.html
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
})
}
}
});
到了这里,关于Thinkphp6接入打通微信小程序支付功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!