//CURL 请求
public function curl($url = '',$request = [],$header = [],$method = 'POST'){
$header[] = 'Accept-Encoding: gzip, deflate';//gzip解压内容
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if ($method == "POST") {//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.执行
if (curl_errno($ch)) {//7.如果出错
return curl_error($ch);
}
curl_close($ch);//8.关闭
return $tmpInfo;
}
//处理回调
public function wx_notify(){
//接收微信返回的数据数据,返回的xml格式
$xmlData = file_get_contents('php://input');
//将xml格式转换为数组
$data = $this->FromXml($xmlData);
//用日志记录检查数据是否接受成功,验证成功一次之后,可删除。
$file = fopen('./log.txt', 'a+');
fwrite($file,var_export($data,true));
//为了防止假数据,验证签名是否和返回的一样。
//记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
$sign = $data['sign'];
unset($data['sign']);
if($sign == $this->getSign($data)){
//签名验证成功后,判断返回微信返回的
if ($data['result_code'] == 'SUCCESS') {
//根据返回的订单号做业务逻辑
$arr = array(
'ispay' => 1,
);
$re = db('uniform_order')->where(['ordersn'=>$data['out_trade_no']])->update($arr);
//处理完成之后,告诉微信成功结果!
if($re){
echo '<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>';exit();
}
}
//支付失败,输出错误信息
else{
$file = fopen('./log.txt', 'a+');
fwrite($file,"错误信息:".$data['return_msg'].date("Y-m-d H:i:s"),time()."\r\n");
}
}
else{
$file = fopen('./log.txt', 'a+');
fwrite($file,"错误信息:签名验证失败".date("Y-m-d H:i:s"),time()."\r\n");
}
}
//生成随机字符串
public function rand($len)
{
$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$key = "";
for($i=0;$i<$len;$i++)
{
$key .= $str{mt_rand(0,32)}; //生成php随机数
}
return $key;
}
前端注意事项:
timeStamp.toString();
wx.requestPayment({ timeStamp: d.data.timeStamp.toString(), nonceStr: d.data.nonceStr, package: d.data.package, signType: d.data.signType, paySign: d.data.sign, success (res) { console.log(res); wx.showToast({ title: '支付完成', icon: 'none', duration: 2000, success(){ wx.navigateTo({ url: '../detail/index?id='+id+"&&s=1" }) } }) }, fail (res) { } })