1. First of all, we need to have a public micro-channel number (classification of types of subscription number, service number, company number) of our public micro-channel number must be a service number only it only makes micro-channel payment interface ..
And the public micro-channel number must be certified micro letter to apply for micro-channel payment interface.
2. Application JSAPI products
3. After the application is successful but also to the public micro-channel number configured at the function settings
The domain business domain name and domain name and web security interfaces js authoritative name are you doing to fill micro-channel payment function can be accessed if the Web site domain name
4. You get into the micro-channel micro-channel merchant payment number. Products -> Configure development product configuration
Fill in your website address invoking the public micro-channel number as https://www.baidu.com/ payment method name / parameter value to
5. Such basic payment environment has built a good course on the development of micro-channel can have a basic reference document into expensive environment to build , can be developed.
The steps are: 1. When developing JSAPI payment, you must get the call to the user's openid unified under a single interface and cached (not every time gained)
public function pay(){ vendor('pay.WxPayData'); vendor('pay.JsApiPay'); vendor('pay.WxPayApi'); vendor('pay.WxPayConfig'); vendor('pay.WxPayNotify'); $orderModel = M('weiz_order'); //①、获取用户openid try{ $tools = new \JsApiPay(); $openId = $tools->GetOpenid(); if(empty($openId)&&$openId ==""){ if(session('expire_time')>time()){ $openId = session('openId'); } }else{ session('openId',$openId); session('expire_time',time()+300);//300秒过期时间 }
2.调用微信支付的统一下单接口
/②、统一下单 $input = new \WxPayUnifiedOrder(); $input->SetBody($body);//body商品描述 $input->SetAttach("附加数据");//attahch附加数据暂时不用附加数据 $input->SetOut_trade_no($out_trade_no);//out_trade_no商户订单号 $input->SetTotal_fee($total_fee);//订单总金额 $input->SetTime_start(date("YmdHis",time()));//交易开始时间 $input->SetTime_expire(date("YmdHis", time() + 600));//交易结束时间prepay_id只有两小时的有效期 $input->SetGoods_tag("无");//订单优惠标记暂时无 $input->SetNotify_url("http://event.com/index.php/Weixinpay/NotifyProcess");//异步回调地址不能携带参数 $input->SetTrade_type("JSAPI");//交易类型JSAPI $input->SetOpenid($openId);//通过code来获取用户标识openID $config = new \WxPayConfig();//获取商户配置 $order = \WxPayApi::unifiedOrder($config, $input); //统一下单接口返回的预支付交易会话标识 $jsApiParameters = $tools->GetJsApiParameters($order);//获取jsapi支付的参数(json格式)
3.将获取到的预支付交易会话标识$jsApiParameters 分配给客户端 只有这个标识才能调起微信支付(所以这个参数很重要如果为空则支付失败)
4.前台拿到预支付交易会话标识后进行调起微信支付 其实按照JSAPI字面意思就是通过客户端的JS来调用支付API
$(function(){ function onBridgeReady(){ // alert(JSON.stringify(jsapi)); WeixinJSBridge.invoke( 'getBrandWCPayRequest',{$jsApiParameters}, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ){ // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 var authToken = $('.authToken').val(); }else if (res.err_msg == "get_brand_wcpay_request:cancel"){ $('.pay-fail-content').css('display','block'); $(".tip").fadeIn(); setInterval(function(){ $('.tip').fadeOut(5000); },3000);//延迟3s执行tip谈出 }else{ //处理支付失败的逻辑 var wid = $('.wid').val(); } }); }
5.当然微信支付提供了多个接口如查询订单,退款等不同的接口看项目需求。这里异步通知和同步通知就不写在这了 自己可以百度和查看微信开发文档
6.JSAPI支付功能常见错误是
a.微信公众号配置错误
b,微信商户号产品配置错误(支付授权目录)
c.统一下单接口请求参数要确保和微信开发平台一致
d.后台与客户端的预支付交易标识参数的传递出现了问题
最后建议 测试工具使用微信开发工具进行测试 虽然其没有权限调起微信这支付但能查出你的问题所在
谢谢!