要實現(xiàn)微信小程序的支付功能,首先必須要有訂單支撐。
一 、小程序通過請求接口創(chuàng)建平臺訂單,訂單創(chuàng)建成功后返回小程序拉起支付需要的數(shù)據(jù)。
Db::startTrans();
//這個是自己平臺的訂單信息
$orderInfo=[];
//通過JSAPI下單
// 1、請求參數(shù)
$postJson = [
"appid" => '小程序appid',
"mchid" => '商戶號',
"description" => '商品信息',
"out_trade_no" => '自己平臺的訂單號',
"notify_url" => '用于接收微信支付結果的回調(diào)接口',
"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'=>'支付訂單創(chuàng)建失??!','data'=>$resp]);
}
代碼中用到的簽名方法如下文章來源:http://www.zghlxwxcb.cn/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;
}
//調(diào)起支付的簽名
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;
}
二、微信小程序獲取到上一步返回的調(diào)起微信支付的配置信息,然后通過wx.requestPayment調(diào)起支付界面。文章來源地址http://www.zghlxwxcb.cn/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接入打通微信小程序支付功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!