微信掃碼支付

1. 官網下載微信支付demo,把 lib 文件夾拿出來,放到tp5根目錄 vendor 文件夾下,重命名爲wxpay:php

2. 把 example 文件夾下的 WxPay.Config.php 拿出來,放到wxpay目錄下,修改require_once,並配置支付參數:html

3. 修改 WxPay.Api.php 第二個require_once:  require_once "WxPay.Config.php";json

4. 微信

public function wx_pay($order_sn){

        header("Content-type:text/html;charset=utf-8");

        require VENDOR_PATH.'/wxpay/WxPay.Api.php'; //引入微信支付
        $input = new \WxPayUnifiedOrder();//統一下單
        $config = new \WxPayConfig();//配置參數

        //$paymoney = input('post.paymoney'); //支付金額
        $paymoney = 1; //測試寫死
        $out_trade_no = 1000000002; //商戶訂單號(自定義)
        $goods_name = '掃碼支付'.$paymoney.'元'; //商品名稱(自定義)
        $input->SetBody($goods_name);
        $input->SetAttach($goods_name);
        $input->SetOut_trade_no($out_trade_no);
//        $input->SetTotal_fee($paymoney*100);//金額乘以100
        $input->SetTotal_fee($paymoney);//金額乘以100
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url("http://shop.bjxlxd.com/index/wxpay/wxpaynotifyurl"); //回調地址
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id("123456789");//商品id

        $result = \WxPayApi::unifiedOrder($config, $input);

        if($result['result_code']=='SUCCESS' && $result['return_code']=='SUCCESS') {
            $url = 'http://shop.bjxlxd.com/public/qrcode.php?data=' . $result["code_url"];
            $this->assign('url',$url);
        }else{
            $this->error('參數錯誤');
        }
        return view();
    }

 

// 微信回調
public function wxpaynotifyurl()
{
    $wxData = file_get_contents("php://input");
    $data = $this -> XMLDataParse($wxData);

    $where['order_sn'] = $data['out_trade_no'];

    $update['payment_time'] = time();
    $update['status'] = 1;
    Db::name('order') -> where($where) -> update($update);
}

 

//xml格式數據解析函數
public function XMLDataParse($data){
    $xml = simplexml_load_string($data,NULL,LIBXML_NOCDATA);
    $array=json_decode(json_encode($xml),true);
    return $array;
}

 

5. 前臺要生成掃碼支付的二維碼。把demo中 example 文件夾下的 phpqrcode文件夾 和 qrcode.php 拿出來,我放到了項目根目錄/public下面:函數

6. post

<img class="wxpay_img" src="{$url}" alt="掃碼支付">
相關文章
相關標籤/搜索