微信公衆號支付 使用基於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 lang="en">
    <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" onclick="callpay()" >立即支付</button>
</div>

</body>
</html>
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章