解决问题备忘:Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》

环境:php7.3.4nts

<?php

//新手,不会较为规范的代码,如下:

header("Access-Control-Allow-Origin: *");
$openid = !empty($_GET['openid']) ? $_GET['openid'] : die('openid无效');//用户的openid

$appid = '';//公众号appid
$mchid = '';//商户号
$description = '';//商品描述
$out_trade_no = date('Ymd') . str_pad(mt_rand(1, 99999), 10, '0', STR_PAD_LEFT);//商户订单号
$notify_url = 'https://www.weixin.qq.com/wxpay/pay.php';//【异步通知地址】
$money = '1';//金额,单位:分

$url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi';
$data = '{"mchid":"'.$mchid.'","out_trade_no":"'.$out_trade_no.'","appid":"'.$appid.'","description":"'.$description.'","notify_url":"'.$notify_url.'","amount":{"total":'.$money.',"currency":"CNY"},"payer": {"openid":"'.$oid.'"}}';

//证书序列号,在商户证书那里可以看到
$serial_no = '';


//private_key是直接打开apiclient_key.pem文件复制出来的

$private_key = '-----BEGIN PRIVATE KEY-----
此处省略若干行
-----END PRIVATE KEY-----';


$url_parts = parse_url($url);
$timestamp = time();
$nonce = $out_trade_no;
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$message = "POST".chr(10).$canonical_url.chr(10).$timestamp.chr(10).$nonce.chr(10).$data.chr(10);

openssl_sign($message, $raw_sign, $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, $nonce, $timestamp, $serial_no, $sign);

$ret = posturl($url,$data,$schema.' '.$token);

//在同目录下创建一个log文件记录返回值,我是直接在服务器真实测试的,调试输出麻烦所以用了这个。
$handle=fopen("./log.txt","a+");
$str=fwrite($handle,$ret."\r\n");
fclose($handle);

die;
$json = json_decode($ret,1);


function posturl($url, $data, $Authorization){
    $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json","Authorization:".$Authorization,"User-Agent:".$_SERVER['HTTP_USER_AGENT']);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,你遇到的问题是关于微信支付查询接口的签名错误。根据错误提示信息,你需要参考微信支付商户REST API签名规则》来修复这个问题。 以下是一个示例代码,展示了如何使用PHP来生成正确的签名: ```php <?php // 生成签名 function generateSignature($method, $url, $timestamp, $nonce, $body) { $merchantId = 'your_merchant_id'; // 商户号 $serialNumber = 'your_serial_number'; // 商户API证书序列号 $privateKey = 'your_private_key'; // 商户API私钥 $message = $method . "\n" . $url . "\n" . $timestamp . "\n" . $nonce . "\n" . $body . "\n"; $signature = ''; openssl_sign($message, $signature, $privateKey, OPENSSL_ALGO_SHA256); $signature = base64_encode($signature); return 'WECHATPAY2-SHA256-RSA2048 mchid="' . $merchantId . '",serial_no="' . $serialNumber . '",nonce_str="' . $nonce . '",timestamp="' . $timestamp . '",signature="' . $signature . '"'; } // 使用示例 $method = 'GET'; // 求方法 $url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/id/1217752501201407033233368018?mchid=1230000109'; // 求URL $timestamp = time(); // 当前时间戳 $nonce = uniqid(); // 随机字符串 $body = ''; // 求体,如果有的话 $authorization = generateSignature($method, $url, $timestamp, $nonce, $body); echo $authorization; ?> ``` 注意,上述示例中的`your_merchant_id`、`your_serial_number`和`your_private_key`需要替换为你自己的商户号、商户API证书序列号和商户API私钥。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值