1. 程式人生 > >thinkphp3.2整和微信公眾號支付詳細流程 demo

thinkphp3.2整和微信公眾號支付詳細流程 demo

公眾號支付是指在微信app中訪問的頁面通過js直接調起微信支付;

首先第一個步驟登入微信公眾平臺然後

1.設定域名(設定授權域名和支付域名)

①設定網頁授權域名並且設定白名單(新增你自己伺服器公網ip)如下圖所示

         


②設定支付域名,去微信商戶後臺,產品中心的 開發配置中設定支付授權目錄;把域名改為自己的;如下圖所示


二:匯入sdk

/ThinkPHP/Library/Vendor/Weixinpay

該sdk是PHP白均瑤部落格碼雲中的開源專案  點選下載sdk

Weixinpay.php

<?php



error_reporting(E_ALL);
ini_set('display_errors', '1');


// 定義時區
ini_set('date.timezone','Asia/Shanghai');


class Weixinpay {
    // 定義配置項
    private $config=array(
        'APPID'              => '', // 微信支付APPID
        'MCHID'              => '', // 微信支付MCHID 商戶收款賬號
        'KEY'                => '', // 微信支付KEY
        'APPSECRET'          => '',  //公眾帳號secert
        'NOTIFY_URL'         => 'http://baijunyao.com/Api/WeixPay/notify/order_number/', // 接收支付狀態的連線  改成自己的域名
        );


    // 建構函式
    public function __construct(){
        // 如果是在thinkphp中 那麼需要補全/Application/Common/Conf/config.php中的配置
        // 如果不是在thinkphp框架中使用;那麼註釋掉下面一行程式碼;直接補全 private $config 即可
        $this->config=C('WEIXINPAY_CONFIG');
    }


    /**
     * 統一下單
     * @param  array $order 訂單 必須包含支付所需要的引數 body(產品描述)、total_fee(訂單金額)、out_trade_no(訂單號)、product_id(產品id)、trade_type(型別:JSAPI,NATIVE,APP)
     */
    public function unifiedOrder($order){
        // 獲取配置項
        $weixinpay_config=$this->config;
        $config=array(
            'appid'=>$weixinpay_config['APPID'],
            'mch_id'=>$weixinpay_config['MCHID'],
            'nonce_str'=>'test',
            'spbill_create_ip'=>'192.168.0.1',
            'notify_url'=>$weixinpay_config['NOTIFY_URL']
            );
        // 合併配置資料和訂單資料
        $data=array_merge($order,$config);
        // 生成簽名
        $sign=$this->makeSign($data);
        $data['sign']=$sign;
        $xml=$this->toXml($data);
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';//接收xml資料的檔案
        $header[] = "Content-type: text/xml";//定義content-type為xml,注意是陣列
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 相容本地沒有指定curl.cainfo路徑的錯誤
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            // 顯示報錯資訊;終止繼續執行
            die(curl_error($ch));
        }
        curl_close($ch);
        $result=$this->toArray($response);
        // 顯示錯誤資訊
        if ($result['return_code']=='FAIL') {
            die($result['return_msg']);
        }
        $result['sign']=$sign;
        $result['nonce_str']='test';
        return $result;
    }




    /**
     * 驗證
     * @return array 返回陣列格式的notify資料
     */
    public function notify(){
        // 獲取xml
        $xml=file_get_contents('php://input', 'r'); 
        // 轉成php陣列
        $data=$this->toArray($xml);
        // 儲存原sign
        $data_sign=$data['sign'];
        // sign不參與簽名
        unset($data['sign']);
        $sign=$this->makeSign($data);
        // 判斷簽名是否正確  判斷支付狀態
        if ($sign===$data_sign && $data['return_code']=='SUCCESS' && $data['result_code']=='SUCCESS') {
            $result=$data;
        }else{
            $result=false;
        }
        // 返回狀態給微信伺服器
        if ($result) {
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        }else{
            $str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[簽名失敗]]></return_msg></xml>';
        }
        echo $str;
        return $result;
    }


    /**
     * 輸出xml字元
     * @throws WxPayException
    **/
    public function toXml($data){
        if(!is_array($data) || count($data) <= 0){
            throw new WxPayException("陣列資料異常!");
        }
        $xml = "<xml>";
        foreach ($data as $key=>$val){
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml; 
    }


    /**
     * 生成簽名
     * @return 簽名,本函式不覆蓋sign成員變數,如要設定簽名需要呼叫SetSign方法賦值
     */
    public function makeSign($data){
        // 去空
        $data=array_filter($data);
        //簽名步驟一:按字典序排序引數
        ksort($data);
        $string_a=http_build_query($data);
        $string_a=urldecode($string_a);
        //簽名步驟二:在string後加入KEY
        $config=$this->config;
        $string_sign_temp=$string_a."&key=".$config['KEY'];
        //簽名步驟三:MD5加密
        $sign = md5($string_sign_temp);
        // 簽名步驟四:所有字元轉為大寫
        $result=strtoupper($sign);
        return $result;
    }


    /**
     * 將xml轉為array
     * @param  string $xml xml字串
     * @return array       轉換得到的陣列
     */
    public function toArray($xml){   
        //禁止引用外部xml實體
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
        return $result;
    }


    /**
     * 獲取jssdk需要用到的資料
     * @return array jssdk需要用到的資料
     */
    public function getParameters(){
        // 獲取配置項
        $config=$this->config;
        // 如果沒有get引數沒有code;則重定向去獲取openid;
        if (!isset($_GET['code'])) {
            // 獲取訂單號
            $out_trade_no=I('get.out_trade_no',1,'intval');
            // 返回的url
            $redirect_uri=U('Api/Weixinpay/pay','','',true);
            $redirect_uri=urlencode($redirect_uri);
            $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$config['APPID'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state='.$out_trade_no.'#wechat_redirect';
            redirect($url);
        }else{
            // 如果有code引數;則表示獲取到openid
            $code=I('get.code');
            // 取出訂單號
            $out_trade_no=I('get.state',0,'intval');
            // 組合獲取prepay_id的url
            $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$config['APPID'].'&secret='.$config['APPSECRET'].'&code='.$code.'&grant_type=authorization_code';
            // curl獲取prepay_id
            $result=$this->https_request
($url);
            $result=json_decode($result,true);
            $openid=$result['openid'];
            // 訂單資料  請根據訂單號out_trade_no 從資料庫中查出實際的body、total_fee、out_trade_no、product_id
            $order=array(
                'body'=>'test',// 商品描述(需要根據自己的業務修改)
                'total_fee'=>1,// 訂單金額  以(分)為單位(需要根據自己的業務修改)
                'out_trade_no'=>$out_trade_no,// 訂單號(需要根據自己的業務修改)
                'product_id'=>'1',// 商品id(需要根據自己的業務修改)
                'trade_type'=>'JSAPI',// JSAPI公眾號支付
                'openid'=>$openid// 獲取到的openid
            );
            // 統一下單 獲取prepay_id
            $unified_order=$this->unifiedOrder($order);
            // 獲取當前時間戳
            $time=time();
            // 組合jssdk需要用到的資料
            $data=array(
                'appId'=>$config['APPID'], //appid
                'timeStamp'=>strval($time), //時間戳
                'nonceStr'=>$unified_order['nonce_str'],// 隨機字串
                'package'=>'prepay_id='.$unified_order['prepay_id'],// 預支付交易會話標識
                'signType'=>'MD5'//加密方式
            );
            // 生成簽名
            $data['paySign']=$this->makeSign($data);
            return $data;
        }
    }




  

需要注意函式中getParameters中的商品資料需要根據業務實際情況從資料庫中獲取;

三:配置項

/Application/Common/Conf/config.php
  1. 'WEIXINPAY_CONFIG'=> array(
  2. 'APPID'=>'',// 微信支付APPID
  3. 'MCHID'=>'',// 微信支付MCHID 商戶收款賬號
  4. 'KEY'=>'',// 微信支付KEY
  5. 'APPSECRET'=>'',// 公眾帳號secert (公眾號支付專用)
  6. 'NOTIFY_URL'=>'http://baidexuan.com/Api/Weixinpay/notify',// 接收支付狀態的連線
  7. ),
複製程式碼
在微信公眾平臺和微信支付平臺湊齊上面這些引數;

則需要在自己的公共函式中增加https_request
/Application/Common/Common/function.php

/**
 * 使用curl獲取遠端資料
 * @param  string $url url連線
 * @return string      獲取到的資料
 */
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

四:支付方法

/Application/Api/Controller/WeixinpayController.class.php
  1. publicfunction pay(){
  2. // 匯入微信支付sdk
  3. Vendor('Weixinpay.Weixinpay');
  4.     $wxpay=new \Weixinpay();
  5. // 獲取jssdk需要用到的資料
  6.     $data=$wxpay->getParameters();
  7. // 將資料分配到前臺頁面
  8.     $assign=array(
  9. 'data'=>json_encode($data)
  10. );
  11.     $this->assign($assign);
  12.     $this->display();
  13. }
複製程式碼需要html的配合:/home/order/pay.html
  1. <!DOCTYPE html>
    <html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>支付</title>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/public.css"/>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/Bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bottomTap.css"/>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/myStyle.css"/>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/pay.css"/>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/layer/need/layer.css"/>
<script type="text/javascript" src="__PUBLIC__/layer/layer.js" ></script>
<script type="text/javascript" src="__PUBLIC__/owl/js/jquery-1.8.3.min.js" ></script>
</head>
<body>
<!--頂部標題-->
<div class="nav text-center">
<a href="#" onClick="javascript:history.back(-1);"><button type="button" class="mybtn pull-left back">
<img src="__PUBLIC__/img/list/right.png"/>
</button></a>
<span class="font16">發起支付</span>
</div>
<!-- <input type="hidden" value="{$orderid}" id="orderid"> -->
<!-- <input type="hidden" value="{$code}" id="code"> -->
<!--頂部標題-->
<!-- <div class="container-fluid cont">
<div class="row">
<div class="col-xs-12 pad-up15 bor-b-f2">
<img class="wx" src="__PUBLIC__/img/pay/weixin.png"/>
<span class="font15">微信支付</span>
<span class="circle pull-right"></span>
</div>
<input type="hidden" value="{$orderId}" id="orderid">
<div class="col-xs-12 pad-up15 bor-b-f2">
<img class="wx" src="__PUBLIC__/img/pay/ali.png"/>
<span class="font15">支付寶支付</span>
<span class="circle pull-right"></span>
</div>
</div>
</div> -->
<div class="container-full bg-fff toal-list ">
<div class="col-xs-12 padd_0 font17">
<div class="pull-right">
<!-- <span class="color-669900 t-all">支付:¥{$sumprice}</span> -->

<button type="button" class="set " onclick="getOrder()">
支付
</button>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="__PUBLIC__/js/jquery-3.1.1.min.js" ></script>
<script type="text/javascript">

function onBridgeReady(){
    var data={$data};
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', data, 
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
  window.location.href = '/index.php/home/user/getcouponcenter';
                // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在使用者支付成功後返回    ok,但並不保證它絕對可靠。
            }   //支付過程中使用者取消
                                    if(res.err_msg == "get_brand_wcpay_request:cancel" ) {
            
                                           layer.open({
          content: '支付失敗'
          ,skin: 'msg'
          ,time: 3 //3秒後自動關閉
        });
                                      }
                                      if(res.err_msg == "get_brand_wcpay_request:fail" ) {
                                             
                                           layer.open({
          content: '支付失敗'
          ,skin: 'msg'
          ,time: 3 //3秒後自動關閉
        });
                                      }
        }
    );
}
function getOrder(){
    var data={$data};
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', data, 
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
  window.location.href = '/index.php/home/user/getcouponcenter';
                // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在使用者支付成功後返回    ok,但並不保證它絕對可靠。
            }   //支付過程中使用者取消
                                if(res.err_msg == "get_brand_wcpay_request:cancel" ) {
       
                                      layer.open({
      content: '支付失敗'
      ,skin: 'msg'
      ,time: 3 //3秒後自動關閉
    });
                                 }
                                 if(res.err_msg == "get_brand_wcpay_request:fail" ) {
                                         
                                      layer.open({
      content: '支付失敗'
      ,skin: 'msg'
      ,time: 3 //3秒後自動關閉
    });
                                 }
        }
    );
}
if (typeof WeixinJSBridge == "undefined"){
     if( document.addEventListener ){
         document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
     }else if (document.attachEvent){
         document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
         document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
     }
}else{
      onBridgeReady();
}


</script>
</html>

複製程式碼呼叫示例:/Application/Home/Controller/orderController.class.php 中的makeorder方法
  1. /**
  2.  * 生成預支付訂單,拿著這個訂單,發起微信jsapi 支付
  3.  */
  4. publicfunction makeorder(){
  5.     此處生成預支付訂單,拿著這個訂單,發起微信jsapi 支付
  6. // 組合url
  7.     $url=U('home/order/pay',array('out_trade_no'=>$out_trade_no));
  8. // 前往支付
  9.     redirect($url);
  10. }
五:非同步接收通知

/Application/Api/Controller/WeixinpayController.class.php
 * notify_url接收頁面
 */
public function notify(){
    // ↓↓↓下面的file_put_contents是用來簡單檢視非同步發過來的資料 測試完可以刪除;↓↓↓
    // 獲取xml
    $xml=file_get_contents('php://input', 'r');
    //轉成php陣列 禁止引用外部xml實體
    libxml_disable_entity_loader(true);
    $data= json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
    file_put_contents('./notify.text', $data);
    // ↑↑↑上面的file_put_contents是用來簡單檢視非同步發過來的資料 測試完可以刪除;↑↑↑
    // 匯入微信支付sdk
    Vendor('Weixinpay.Weixinpay');
    $wxpay=new \Weixinpay();
    $result=$wxpay->notify();
    if ($result) {
        // 驗證成功 修改資料庫的訂單狀態等 $result['out_trade_no']為訂單id


    }
}
如果出現簽名錯誤;

跟自己生產的簽名對比;

然後對比配置;查詢不一致的地方;

如果遇到問題,請在下方留言