wxpay.api.php tp5,Thinkphp5最简洁集成(JSAPI)微信支付的方法

1.把官方SDK经过调整,跳过一些坑,放到vendor目录下,具体路径为:vendor/pay/wxway。文件名要特殊处理一些把官方文件名中的点换成下划线。因为vendor助手函数在引入时路径中用的是点。

7f321e480a5d30dbf99ab8fc7bfa3505.png

2.在控制器中的操作代码

namespace app\mobile\controller;

use think\Controller;

use think\Db;

use think\Url;

use think\Request;

class Pay extends Controller

{

public function _initialize()

{

//统一设置root路径

url::root('/index.php');

}

//微信配置,并vendor助手函数引入微信支付类

private function _weixin_config(){

define('WXPAY_APPID', "wxd292116802a37f05"); //微信公众号APPID

define('WXPAY_MCHID', "1487533902"); //微信商户号MCHID

define('WXPAY_KEY', "77c717a097a6a204bd89f3c8348afd5c"); //微信商户自定义32位KEY

define('WXPAY_APPSECRET', "b1c7f40ce811312b45107801ccd13227"); //微信公众号appsecret

//vendor助手函数引入微信支付类

vendor('pay.wxpay.WxPay_Api');

vendor('pay.wxpay.WxPay_JsApiPay');

}

public function weixin(){

$this->_weixin_config();

//实例化相应的类

$notify = new \WxPayApi();

$t = new \JsApiPay();

$input = new \WxPayUnifiedOrder();

//获取openid

$openId = $t->GetOpenid();

//以下都是发起支付必须的参数

$input->SetBody('购买两天xikai使用权');

$input->SetAttach('雇用一个教授');

$input->SetOut_trade_no('u'.date('ymdhis').substr(microtime(),2,4));

$input->SetTotal_fee(1);

$input->SetTime_start(date("YmdHis"));

$input->SetTime_expire(date("YmdHis", time() + 600));

$input->SetNotify_url('http://shop.i1.mofire.net');

$input->SetTrade_type('JSAPI');

$input->SetOpenid($openId);

//统一支付

$result = $notify->unifiedOrder($input);

//获得jsapi参数,这个要在模板中用

$jsApiParameters = $t->GetJsApiParameters($result);

if($result['return_code'] != 'SUCCESS'){

echo "支付错误";

}else{

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

return $this->fetch();

}

}

}

3.模板中代码实例

微信支付样例-支付

//调用微信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();

}

}

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);

}

}

};

该笔订单支付金额为1分

立即支付

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值