tp5.0 微信jsapi支付流程
1、下载官方sdk包放在extend目录下
2、在公众号里面关联商户号,在商户号里面配置域名
3、在Wxpay.Config.php这个文件里面把你的公众号APPID、APPSECRET,商户号MCHID,自己设置的商户秘钥KEY全部设置好;
例:public function GetAppId()
{
return 'wx12345678912';//这里填公众号APPID(还有几个地方大家依次填入,我就不示范了)
}
4、创建paywx控制器
引入文件:require_once APP_PATH . '/../extend/WxPay/lib/WxPay.Api.php';
require_once APP_PATH . '/../extend/WxPay/example/WxPay.JsApiPay.php';
require_once APP_PATH . '/../extend/WxPay/example/WxPay.Config.php';
require_once APP_PATH . '/../extend/WxPay/lib/WxPay.Data.php';
require_once APP_PATH . '/../extend/WxPay/example/log.php';
(注意:这里引入路径大小写要和你文件名大小写一致,不然会报错WxPay.JsApiPay.php、WxPay.Config.php
里面的文件路径也要修改一下例:require_once APP_PATH . '/../extend/WxPay/lib/WxPay.Api.php';)
(路径自己拼,../上一级,根据自己sdk存放目录自己拼,上面是我的路径)
创建方法:直接把微信jsapi里面的方法搬过来修改一下
public function index(){
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
(这里的openid和你网页授权的openid一样,如果你以前写过获取openid的方
法这里就不用写了,直接引入就好,如果没有写也不要用sdk包里面的方法,直接去
对接网页授权的接口就好,返回一个openid,我感觉比sdk里面的方法简单)
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no("sdkphp".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://paysdk.weixin.qq.com/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$config = new \WxPayConfig();
$order = \WxPayApi::unifiedOrder($config, $input);
$jsApiParameters = $tools->GetJsApiParameters($order);
$this->assign('jsApiParameters',$jsApiParameters);
$this->display();
return view();
}
(注意:我这里文件引入正确之后但是调用时前面要加\,我也不知道为啥
例$tools = new JsApiPay()这是官方的,$tools = new \JsApiPay()这是我的)
5、创建视图
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付样例-支付</title>
<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>
该笔订单支付金额为1分钱
立即支付