微信支付jsapi v3支付实例
小白 2021-09-16 【原创文章】
微信支付jsapi v3支付实例
参考https://blog.csdn.net/qq_15758827/article/details/110002894
核心代码:
$jsapi = [ 'appid' => $weixin_set['appid'], 'mchid' => $weixin_set['mchid'], 'description' => '本元商品购买', 'out_trade_no' => $pay['number'], 'notify_url' => $weixin_set['notify_url'], 'amount' => [ 'total' => 0.01 * 100, // 'total' => $pay['price'] * 100, 'currency' => 'CNY', ], 'payer' => [ 'openid' => $user_info['openid'] ], ]; //解析URL的函数 $urlarr = parse_url($url); $timeStamp = time(); //随机字符串 $nonceStr = getRandom(32); $jsapi = json_encode($jsapi); $key = getSign1($jsapi, $urlarr['path'], $nonceStr, $timeStamp);//签名 $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $weixin_set['mchid'], $weixin_set['number'], $nonceStr, $timeStamp, $key);//头部信息 $header = array( 'Content-Type:' . 'application/json; charset=UTF-8', 'Accept:application/json', 'User-Agent:application/json', 'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token ); //curl post 请求 $curl_info = curl_post_https($url, $jsapi, $header); $ret = json_decode($curl_info, true); if (empty($ret['prepay_id'])) { return json_return(500, '支付失败', $ret); } $Sign = $weixin_set['appid'] . "\n" . $timeStamp . "\n" . $nonceStr . "\n" . 'prepay_id=' . $ret['prepay_id'] . "\n"; $pay_info = [ 'timeStamp' => $timeStamp, 'nonceStr' => $nonceStr, 'package' => 'prepay_id=' . $ret['prepay_id'], 'signType' => 'RSA', 'paySign' => getSha256WithRSA2($Sign), 'id' => $pay['id'], 'price' => $pay['price'], ];
//随机字符串:
//随机字符串 function getRandom($param) { $str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $key = ""; for ($i = 0; $i < $param; $i++) { $key .= $str[mt_rand(0, 35)]; //生成php随机数 } return $key; }
//微信签名
//微信支付签名 function getSign1($data, $url, $randstr, $time) { $str = "POST" . "\n" . $url . "\n" . $time . "\n" . $randstr . "\n" . $data . "\n"; $key = file_get_contents('./pem/apiclient_key.pem');//在商户平台下载的秘钥 $str = $this->getSha256WithRSA($str, $key); return $str; } function getSha256WithRSA($content, $privateKey) { $binary_signature = ""; $algo = "SHA256"; openssl_sign($content, $binary_signature, $privateKey, $algo); $sign = base64_encode($binary_signature); return $sign; }
最新发布
- 守护进程-kill端口-输出日志-查看等!
- git 本地分支关联远程分支,合并!
- pytorch安装(含cuda、cudnn安装教程)!
- linux系统下ubuntu22.04安装Docker方法!
- 视频截取封面 =php-ffmpeg/php-ffmpeg!
- 常用实例2023-5-10!
- 富文本 塞入mysql 报错: General error: 1366 Incorrect string value: ‘\xF0\x9F\x98\x84‘ for column ‘content‘!
- 百度地图拖拽获取地址实例!
- php使用smtp鉴权方式发送邮件 插件PHPMailer!
- 有道翻译接口对接!