第一步獲取code_url,第二步生成支付二維碼,第三回調函數修改數據php
//購買產品展現支付二維碼 public function yijihuiyuan(){ Loader::import("weixinpay.lib.WxPay", EXTEND_PATH, ".Api.php"); Loader::import("weixinpay.example.log", EXTEND_PATH, ".php"); Loader::import("weixinpay.example.WxPay", EXTEND_PATH, ".Config.php"); $notify=new NativePay(); //初始化日誌 //$logHandler= new \CLogFileHandler("../logs/".date('Y-m-d').'.log'); // $logHandler= new \CLogFileHandler(EXTEND_PATH."weixinpay/logs/".date('Y-m-d').'.log'); // $log = \Log::Init($logHandler, 15); $input = new \WxPayUnifiedOrder(); $input->SetBody("第一次微信掃碼支付"); $input->SetAttach("測試"); $input->SetOut_trade_no("swdkyj123456789".date("YmdHis")); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://test.gdzhisheng.top/index.php/index/mine/notify.html"); $input->SetTrade_type("NATIVE"); $input->SetProduct_id("12235413214070356458058"); $result = $notify->GetPayUrl($input); $url2 = $result["code_url"]; $url=urlencode($url2); $this->assign("url",$url); return $this->fetch(); }
前臺頁面獲取到code_url後生成二維碼html
<img alt="模式二掃碼支付" src="{:url('qrcode')}?data={$url}"/>
將後臺配置好qrcode方法數據庫
//生成二維碼 public function qrcode() { Loader::import("weixinpay.example.phpqrcode.phpqrcode", EXTEND_PATH, ".php"); $url = urldecode($_GET["data"]); if(substr($url, 0, 6) == "weixin"){ \QRcode::png($url); }else{ header('HTTP/1.1 404 Not Found'); } }
這時就會出現支付二維碼,而後咱們設置回調函數api
//微信回調接口 public function notify(){ // $this->array= 3030; $post = $_REQUEST; if ($post == null) { $post = file_get_contents("php://input"); } if ($post == null) { $post = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; } if (empty($post) || $post == null || $post == '') { //阻止微信接口反覆回調接口 文檔地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7,下面這句很是重要!!! $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'; echo $str; exit('Notify 非法回調'); } libxml_disable_entity_loader(true); //禁止引用外部xml實體 $xml = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);//XML轉數組 $post_data = (array)$xml; //若是微信支付成功,添加一條訂單信息 $data['openid']=$post_data['openid']; $data['trade_type']=$post_data['trade_type']; $data['total_fee']=$post_data['total_fee']; $data['transaction_id']=$post_data['transaction_id']; $data['result_code']=$post_data['result_code']; $data['out_trade_no']=$post_data['out_trade_no']; $data['time_end']=$post_data['time_end']; $data['cash_fee']=$post_data['cash_fee']; $data['bank_type']=$post_data['bank_type']; $rel=Db("sw_order")->insert($data); if($rel){ echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"; exit('Notify 非法回調'); } // } }
支付成功後微信服務器就會調用咱們設置的回調地址返回一些參數,咱們再根據參數修改數據庫的數據。數組