您好,欢迎访问一九零五行业门户网

关于微信支付小程序v3【附PHP完整后端代码】

微信支付 小程序 (v3)- php 完整后端代码
踩坑太多,不多说,直接上完整后端代码<?phpheader('content-type:text/html; charset=utf-8');ini_set('date.timezone','asia/shanghai');$data_s = file_get_contents('php://input');$data_s = json_decode($data_s,true);//统一下单function wechartaddorder($name,$ordernumber,$money,$openid,$timestamp,$noncestr){ $url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; $urlarr = parse_url($url); $appid = '小程序appid';//appid $mchid = '微信支付商户id';//商户id $xlid = '微信支付公钥序列号';//秘钥序列号 可在这个网址中查询 https://myssl.com/cert_decode.html $data = array(); $time = $timestamp; $data['appid'] = $appid; $data['mchid'] = $mchid; $data['description'] = $name;//商品描述 $data['out_trade_no'] = $ordernumber;//订单编号 $data['notify_url'] = "你的域名/你的支付目录路径/notify.php";//回调接口 需根据自己的情况修改 $data['amount']['total'] = intval($money * 1);//金额 单位 分 $data['payer']['openid'] = $openid;//用户openid $data = json_encode($data); $key = getsign($data,$urlarr['path'],$noncestr,$time);//签名 $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"',$mchid,$xlid,$noncestr,$time,$key);//头部信息 $header = array( 'content-type:'.'application/json; charset=utf-8', 'accept:application/json', 'user-agent:*/*', 'authorization: wechatpay2-sha256-rsa2048 '.$token ); $ret = curl_post_https($url,$data,$header); $ret = ltrim($ret,'{"prepay_id":"'); $ret = rtrim($ret,'}"'); //微信支付(小程序)签名 $str = getwechartsign($appid,$timestamp,$noncestr,'prepay_id='.$ret); $arr = array('appid'=>$appid,'timestamp'=>$timestamp,'package'=>'prepay_id='.$ret,'paysign'=>$str); exit(json_encode($arr));}$set_body = '支付测试';//支付显示内容$price = '1';//支付金额$out_trade_no = $data_s['out_trade_no'];//订单号$timestamp = $data_s['timestamp'];//时间戳$openid = $data_s['openid'];$noncestr = $data_s['noncestr'];wechartaddorder($set_body,$out_trade_no,$price,$openid,$timestamp,$noncestr);//微信支付签名function getsign($data=array(),$url,$randstr,$time){ $str = "post"."\n".$url."\n".$time."\n".$randstr."\n".$data."\n"; $key = file_get_contents('apiclient_key.pem');//在商户平台下载的秘钥 $str = getsha256withrsa($str,$key); return $str;}//调起支付的签名function getwechartsign($appid,$timestamp,$noncestr,$prepay_id){ $str = $appid."\n".$timestamp."\n".$noncestr."\n".$prepay_id."\n"; $key = file_get_contents('apiclient_key.pem'); $str = 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;}/* php curl https post */function curl_post_https($url,$data,$header){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个curl会话 curl_setopt($curl, curlopt_url, $url); // 要访问的地址 curl_setopt($curl, curlopt_ssl_verifypeer, 0); // 对认证证书来源的检查 curl_setopt($curl, curlopt_ssl_verifyhost, 1); // 从证书中检查ssl加密算法是否存在 curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']); // 模拟用户使用的浏览器 curl_setopt($curl, curlopt_followlocation, 1); // 使用自动跳转 curl_setopt($curl, curlopt_autoreferer, 1); // 自动设置referer curl_setopt($curl, curlopt_post, 1); // 发送一个常规的post请求 curl_setopt($curl, curlopt_postfields, $data); // post提交的数据包 curl_setopt($curl, curlopt_timeout, 30); // 设置超时限制防止死循环 curl_setopt($curl, curlopt_header, 0); // 显示返回的header区域内容 curl_setopt($curl, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, curlopt_httpheader, $header); $tmpinfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭curl会话 return $tmpinfo; // 返回数据,json格式}
查询订单是否支付完成header('content-type:text/html; charset=utf-8');ini_set('date.timezone','asia/shanghai');$data_s = file_get_contents('php://input');$data_s = json_decode($data_s,true);if(empty($data_s['out_trade_no'])){ exit;}$out_trade_no = $data_s['out_trade_no'];//订单号$merchant_id = '商户id';//商户id$mch_private_key = file_get_contents('apiclient_key.pem');//在商户平台下载的秘钥$xlid = '微信支付公钥序列号';//秘钥序列号 可在这个网址中查询 https://myssl.com/cert_decode.html$url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/'.$out_trade_no.'?mchid='.$merchant_id;$url_parts = parse_url($url);$http_method = 'get';$timestamp = time();$nonce = md5(time().$out_trade_no);$body = '';$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));$message = $http_method."\n". $canonical_url."\n". $timestamp."\n". $nonce."\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"',$merchant_id, $nonce, $timestamp, $xlid, $sign);$header = array( 'content-type:'.'application/json; charset=utf-8', 'accept:application/json', 'user-agent:*/*', 'authorization: wechatpay2-sha256-rsa2048 '.$token); $ret = curl_get_https($url,$data,$header);$return_out_trade_no = get_between($ret,'"out_trade_no":"','","payer"');$return_trade_state = get_between($ret,'trade_state":"','","trade_state_desc"');$arr = array('type'=>'success','trade_state'=>$return_trade_state,'out_trade_no'=>$return_out_trade_no);exit(json_encode($arr));/* * php截取指定两个字符之间字符串 * */function get_between($input, $start, $end) { $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1)); return $substr;}/* php curl https get */function curl_get_https($url,$data,$header){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个curl会话 curl_setopt($curl, curlopt_url, $url); // 要访问的地址 curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']); // 模拟用户使用的浏览器 curl_setopt($curl, curlopt_followlocation, 1); // 使用自动跳转 curl_setopt($curl, curlopt_autoreferer, 1); // 自动设置referer curl_setopt($curl, curlopt_timeout, 30); // 设置超时限制防止死循环 curl_setopt($curl, curlopt_header, 0); // 显示返回的header区域内容 curl_setopt($curl, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, curlopt_httpheader, $header);// 头部信息 $tmpinfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭curl会话 return $tmpinfo; // 返回数据,json格式}
以上就是关于微信支付小程序v3【附php完整后端代码】的详细内容。
其它类似信息

推荐信息