微信扫码支付
<?php //生成二维码图像类 参考 https://www.cnblogs.com/mengor/p/8192642.html require_once './phpqrcode/phpqrcode.php'; define('APPID','123123123'); define('MCHID','456456'); define('KEY','789789'); $data['appid']=APPID; //微信分配的公众账号ID $data['mch_id']=MCHID; //微信支付分配的商户号 $data['time_stamp']=time(); //系统当前时间,定义规则详见时间戳 $data['nonce_str']=uniqid(); //随机字符串,不长于32位。推荐随机数生成算法 $data['out_trade_no']=time().rand('1111','9999'); //商户定义的商品id 或者订单号 $data['body']='龙测试'; //商品信息 $data['trade_type']='NATIVE'; //交易类型 $data['total_fee']=1; //支付金额 单位分 $data['spbill_create_ip']='117.34.40.95'; $data['notify_url']='www.guomeng.group/index/way_login/pay'; //外网地址 $sign=createSign($data); $data['sign']=$sign; //签名 $data=dataToXml($data); //转xml $request=request($url='https://api.mch.weixin.qq.com/pay/unifiedorder',$https=true,$method='post',$data); //将数据发送到微信服务器 $obj=simplexml_load_string($request); $erweima_url=$obj->code_url; //将链接转成二维码 qrcode($erweima_url); //生成图片二维码
exit; //生成签名 function createSign($data){ $string=''; ksort($data); foreach($data as $key=>$value){ $string.="{$key}={$value}&"; } $string.='key='.KEY; return strToUpper(md5($string)); } //转xml function dataToXml($data){ $xml='<xml>'; foreach($data as $key=>$value){ $xml.="<{$key}>{$value}</{$key}>"; } $xml.='</xml>'; return $xml; } //curl发送请求 function request($url,$https,$method,$data){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($https==true){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if($method=='post'){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); } $result = curl_exec($ch); curl_close($ch); return $result; } //生成二维码 function qrcode($url=''){ $value = $url; //二维码内容 $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 $QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2); }
发送成功后微信服务器会返回xml数据
code_url就是链接地址生成二维码就好了
$xml = file_get_contents('php://input'); 用来接收回调参数
回调参考 https://www.cnblogs.com/itxiongwei/p/5531053.html
希望广大博友给予建议和指导