微信公众号支付 使用基于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>
}

支付基本配置完成:回调函数里面处理自己的业务逻辑

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