环境:php7.3.4nts文章来源地址https://www.toymoban.com/news/detail-502842.html
<?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;
}
?>
文章来源:https://www.toymoban.com/news/detail-502842.html
到了这里,关于解决问题备忘:Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!