希望能得到回答,这个问题已经困扰我一宿了,我不想明天还被这个问题困扰
小程序支付下单签名一直错误,怎么办?//生成的数组 整体用PHP写的 $data = []; $data['amount'] = ['total'=>$info['money']*100,'currency'=>'CNY']; //金额 $data['appid'] = $this->WXappid; //appid $data['attach'] = $info['number'];//订单号 $data['description'] = $house['name'].'('.$room['name'].') - '.$info['hours'].'小时'; //描述 $data['mchid'] = $this->mchid;//商户id $data['notify_url'] = $this->LINK.'/api/orderReturn.html';//回传URL $data['payer'] = ['openid'=>$info['openid']];//openid $data['out_trade_no'] = $info['number'];//订单号 $json = json_encode($data);//转为json $res = $this->sign_encode($json);//生成签名 并返回给小程序 传输的json为请求主体 //签名生成 public function sign_encode($body = ''){ // Authorization: $nonce_str = $this->GetRandStr(32); //随机字符串 $timestamp = time(); //时间戳 $mchid = $this->mchid; //商户ID $serial_no = $this->serial_no; //序列号 $url = 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi'; //请求下单的地址 $http_method = 'POST'; //提交方式 $url_parts = parse_url($url); $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : "")); //这个是官方样例里面的 $mch_private_key = $this->getPrivateKey(); //取出商户的KEY $message = $http_method."\n".$canonical_url."\n".$timestamp."\n".$nonce_str."\n".$body."\n"; //拼接加密字符串 openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption'); //进行加密 $sign = base64_encode($raw_sign); //加密 $schema = 'WECHATPAY2-SHA256-RSA2048'; $token = sprintf('mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',$mchid, $nonce_str, $sign, $timestamp, $serial_no); //得出结果 然后传递给Authorization这个头文件 return $schema.' '.$token; //连接字符串后传递回去 } public function getPrivateKey() { return openssl_get_privatekey(file_get_contents('./static/wechatkey/apiclient_key.pem')); } //这里是小程序代码 wx.request({ method:'POST', url: 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi', data:{ "amount":{"total":that.data.info.money*100,"currency":"CNY"}, "appid":"*****************", "attach":that.data.info.number, "description":that.data.house.name+"("+that.data.room.name+") - "+that.data.info.hours+"小时", "mchid":"***********", "notify_url":app.globalData.httpUrl+"/api/orderReturn.html", "out_trade_no":that.data.info.number, "payer":{"openid":that.data.userInfo.openid} }, header: { 'content-type': 'application/json', 'Accept': 'application/json', 'Authorization': res.data.data //这个是请求返回的签名 }, success (re) { console.log(re); }, error(er) { console.log(er); } }); /** * 我是按照官方给的方式,请求下单,生成Authorization 使用的签名, * 但是到目前为止 都是一直显示签名错误,我实在找不到原因了, * 请求大神帮助看看我代码哪里写的有问题 */
2020-12-02Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》返回的永远是这个
小程序下单中的Authorization如何进行签名?//签名生成 public function sign_encode($body = ''){ // Authorization: <schema> <token> $nostr = $this->GetRandStr(32); //随机字符串 $timestamp = time(); //时间戳 $mchid = $this->mchid; //商户ID $serial_no = $this->serial_no; //商户的APIv3密钥 $url = 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi'; //请求下单的地址 $http_method = 'POST'; //提交方式 $url_parts = parse_url($url); $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : "")); //这个是官方样例里面的 $mch_private_key = file_get_contents('./static/wechatkey/apiclient_key.pem'); //取出商户的KEY $message = $http_method."\n".$canonical_url."\n".$timestamp."\n".$nostr."\n".$body."\n"; //拼接加密字符串 openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption'); //进行加密 $sign = base64_encode($raw_sign); //加密 //$schema = 'WECHATPAY2-SHA256-RSA2048'; //官方写的 完全不知道干啥用就注释了 $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',$mchid, $nostr, $timestamp, $serial_no, $sign); //得出结果 然后传递给Authorization这个头文件 return $token; }
2020-12-02