天天看點

微信公衆号支付 使用基于thinkphp 使用微信官網的sdk

1、下載下傳sdk:首先需要在微信官網下載下傳微信支付sdk,本人将其命名為wxpay,在網站根目錄下建立一個檔案夾Public,将wxpay檔案放入該目錄下。

2、配置appid、APPSECRET等4項:在微信公衆号中找到下面四個配置項目;

const APPID = 'wxbd6********';  ---綁定支付的APPID(必須配置,開戶郵件中可檢視)

const MCHID = '***********';商戶号(必須配置,開戶郵件中可檢視)

const KEY = '*************************';商戶支付密鑰,參考開戶郵件設定(必須配置,登入商戶平台自行設定)

const APPSECRET = '**************';公衆帳号secert(僅JSAPI支付的時候需要配置, 登入公衆平台,進入開發者中心可設定),

然後找到Public/wxpay/lib/WxPay.Config.php,  配置上面四個選項。

3、配置網頁授權域名:在微信公衆賬戶中右邊欄目找到 “公衆号設定”-》“功能設定”  配置網頁授權域名。

4、配置支付授權目錄:進入公衆号賬戶,點選右邊清單項  “微信支付”-》“開發配置”  配置支付授權目錄,測試授權目錄并添加測試白名單

5、在進入Public/wxpay/lib/WxPay.Api.php  檔案,找到postXmlCurl  方法。将其中兩項

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗

改為:curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//嚴格校驗2

6、thinkphp中支付測試控制器如下:

<?php

namespace Home\Controller;

use Think\Controller;

class TestController extends Controller {

    //首頁

    public function index(){

    ini_set('date.timezone','Asia/Shanghai');

    //error_reporting(E_ERROR);

    require_once getcwd()."/Public/wxpay/lib/WxPay.Api.php";

    require_once getcwd()."/Public/wxpay/example/WxPay.JsApiPay.php";

    require_once getcwd()."/Public/wxpay/example/log.php";

    //①、擷取使用者openid

    $tools = new \JsApiPay();

    $openId = $tools->GetOpenid();

    //②、統一下單

    $input = new \WxPayUnifiedOrder();

    $input->SetBody("test");

    $input->SetAttach("test");

    $input->SetOut_trade_no(\WxPayConfig::MCHID.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://".$_SERVER['HTTP_HOST']."/index.php/Home/Test/notify");

    $input->SetTrade_type("JSAPI");

    $input->SetOpenid($openId);

    $order = \WxPayApi::unifiedOrder($input);

    echo '<font color="#f00"><b>統一下單支付單資訊</b></font><br/>';

    //printf_info($order);

    $jsApiParameters = $tools->GetJsApiParameters($order);

    $this->assign('jsApiParameters',$jsApiParameters);

    //擷取共享收貨位址js函數參數

    $editAddress = $tools->GetEditAddressParameters();

    $this->assign('editAddress',$editAddress);

    $this->display();

    }

         //設定接收微信支付異步通知回調位址

    protected function notify(){

    }

   7、index.html如下:

<!DOCTYPE html>

<html>

<head >

    <meta charset="UTF-8">

    <title></title>

     <style type="text/css">

        ul {

            margin-left:10px;

            margin-right:10px;

            margin-top:10px;

            padding: 0;

        }

        li {

            width: 32%;

            float: left;

            margin: 0px;

            margin-left:1%;

            padding: 0px;

            height: 100px;

            display: inline;

            line-height: 100px;

            color: #fff;

            font-size: x-large;

            word-break:break-all;

            word-wrap : break-word;

            margin-bottom: 5px;

        }

        a {

            -webkit-tap-highlight-color: rgba(0,0,0,0);

        text-decoration:none;

            color:#fff;

        }

        a:link{

            -webkit-tap-highlight-color: rgba(0,0,0,0);

        text-decoration:none;

            color:#fff;

        }

        a:visited{

            -webkit-tap-highlight-color: rgba(0,0,0,0);

        text-decoration:none;

            color:#fff;

        }

        a:hover{

            -webkit-tap-highlight-color: rgba(0,0,0,0);

        text-decoration:none;

            color:#fff;

        }

        a:active{

            -webkit-tap-highlight-color: rgba(0,0,0,0);

        text-decoration:none;

            color:#fff;

        }

    </style>

</head>

<body>

<script type="text/javascript">

//調用微信JS api 支付

function jsApiCall()

{

WeixinJSBridge.invoke(

'getBrandWCPayRequest',

{$jsApiParameters},

function(res){

WeixinJSBridge.log(res.err_msg);

alert(res.err_code+res.err_desc+res.err_msg);

}

);

}

function callpay()

{

if (typeof WeixinJSBridge == "undefined"){

   if( document.addEventListener ){

       document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);

   }else if (document.attachEvent){

       document.attachEvent('WeixinJSBridgeReady', jsApiCall); 

       document.attachEvent('onWeixinJSBridgeReady', jsApiCall);

   }

}else{

   jsApiCall();

}

}

</script>

<script type="text/javascript">

//擷取共享位址

function editAddress()

{

WeixinJSBridge.invoke(

'editAddress',

{$editAddress},

function(res){

var value1 = res.proviceFirstStageName;

var value2 = res.addressCitySecondStageName;

var value3 = res.addressCountiesThirdStageName;

var value4 = res.addressDetailInfo;

var tel = res.telNumber;

alert(value1 + value2 + value3 + value4 + ":" + tel);

}

);

}

window.onload = function(){

if (typeof WeixinJSBridge == "undefined"){

   if( document.addEventListener ){

       document.addEventListener('WeixinJSBridgeReady', editAddress, false);

   }else if (document.attachEvent){

       document.attachEvent('WeixinJSBridgeReady', editAddress); 

       document.attachEvent('onWeixinJSBridgeReady', editAddress);

   }

}else{

editAddress();

}

};

</script>

<br/>

    <font color="#9ACD32"><b>該筆訂單支付金額為<span style="color:#f00;font-size:50px">1分</span>錢</b></font><br/><br/>

<div align="center">

<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" οnclick="callpay()" >立即支付</button>

</div>

</body>

</html>

}

支付基本配置完成:回調函數裡面處理自己的業務邏輯

繼續閱讀