收藏
回答

小程序支付下单签名一直错误,怎么办?

//生成的数组 整体用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-02
回答关注问题邀请回答
收藏

3 个回答

  • 支付社区运营
    支付社区运营
    2020-12-03

    您好,您的问题建议您前往V3文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml  点击右下角“技术咨询”实时寻求技术帮助。

    2020-12-03
    有用
    回复
  • 老张
    老张
    2020-12-02

    微信支付是后台的东西,永远不会与wx.request发生任何毛钱的关系。

    2020-12-02
    有用
    回复
  • 佳
    2020-12-02

    希望能得到回答,这个问题已经困扰我一宿了,我不想明天还被这个问题困扰

    2020-12-02
    有用
    回复
登录 后发表内容
问题标签