入坑一下午,爬坑各种套。微信支付爬坑进行时
准备条件
下载压缩包extend.zip。直接解压在目录即可。
包简介:
首先感谢Mikkle的微信包和微信支付包。该包的教程可以详细关注。
http://www.thinkphp.cn/extend/1042.html
然后感谢微信支付官网的例子,详细关注
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
准备条件最重要的是设置微信支付官方的地址
第一坑
支付授权目录,一定要到要填写到指定到具体的模块和控制器中。还不能使用大写
支付地址为:www.thinkphp.cn/pay.php?payment='.$add['payment'].'&ordersn='.$add['ordersn']设置支付授权目录:www.thinkphp.cn支付地址为:www.thinkphp.cn/index/wxpay/pay/id/123设置支付授权目录:www.thinkphp.cn/index/wxpay/pay/id支付地址为:www.thinkphp.cn/index/wxpay/pay/设置支付授权目录:www.thinkphp.cn/index/wxpay/pay/
复制代码
整个支付流程,详细查看(https://www.kancloud.cn/mikkle/thinkphp5_study/376461
)
第一步 获取openid
private $open_id;public function _initialize(){ini_set('date.timezone','Asia/Shanghai');$tools=new \JqTool\WxPay\JsPay();if (!Session::get('openid')){$openId= $tools->setConfig()->GetOpenid();Session::set('openid',$openId);}else{$this->open_id=Session::get('openid');}config($this->wx_config);$this->notify_url='http://'.$_SERVER['HTTP_HOST'].'/mobile/Chong/notify';}
复制代码
第二步 根据openid进行统一下单获取订单号
第三步 根据统一下单的订单号 获取获取JsApi$getParameters参数
<?phpnamespace app\index\controller;use mikkle\tp_wxpay\JsApi_pub as JaApi;use mikkle\tp_wxpay\UnifiedOrder_pub as UnifiedOrder;use think\Controller;use think\Session;class Wx extends Controller{protected $wx_config=['wechat_appid'=>'',//微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看'wechat_mchid'=>'',//受理商ID,身份标识 商户号'wechat_appkey'=>'',//商户支付**Key。审核通过后,在微信发送的邮件中查看'wechat_appsecret'=>'',//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看//证书路径,注意应该填写绝对路径 不用证书也是能支付的'sslcert_path'=>'','sslkey_path'=> '',];private $open_id;private $notify_url;public function _initialize(){ini_set('date.timezone','Asia/Shanghai');$tools=new \JqTool\WxPay\JsPay();if (!Session::get('openid')){$openId= $tools->setConfig()->GetOpenid();Session::set('openid',$openId);}else{$this->open_id=Session::get('openid');}config($this->wx_config);$this->notify_url='http://'.$_SERVER['HTTP_HOST'].'/mobile/Chong/notify';}function index(){$data=$this->payByOrderNo();$this->assign('amount',$data['amount']);$this->assign('order_no',$data['order_no']);$this->assign("jsApiParameters" ,$data['jsApiParameters']);$this->assign('openid',$this->open_id);return $this->fetch();}function payByOrderNo(){$data['order_no']=time();$data['amount']=1;$unified_order = $this->unifiedOrder($data); //统一下单$jsApiParameters=$this->getParameters($unified_order);return ['code'=>1001,'order_no'=> $data['order_no'],'jsApiParameters'=>$jsApiParameters,'amount'=>$data['amount'],];}protected function unifiedOrder($data=[]){$unifiedOrder = new UnifiedOrder();$unifiedOrder->setParameter("openid",$this->open_id); // openid$unifiedOrder->setParameter("body",'商品订单号'+$data['order_no']); // 商品描术$unifiedOrder->setParameter("out_trade_no",$data['order_no'].'_'.$unifiedOrder->createNoncestr(6)); // 商户订单号$unifiedOrder->setParameter("total_fee",$data['amount']*100); // 总金额$unifiedOrder->setParameter("notify_url",$this->notify_url); // 通知地址$unifiedOrder->setParameter("trade_type","JSAPI"); // 交易类型return $unifiedOrder->getPrepayId();}protected function getParameters($unified_order=''){$jsApi= new JaApi();$jsApi->setPrepayId($unified_order);$jsApiParameters = $jsApi->getParameters();return $jsApiParameters;}}
复制代码
最后 JsApi中支付
<center><h1 onclick='callpay()'>点击支付</h1></center>{$jsApiParameters}<script>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();}}//调用微信JS api 支付function jsApiCall(){WeixinJSBridge.invoke('getBrandWCPayRequest',{$jsApiParameters},function(res){console.log(res.err_msg)/*if(res.err_msg == "get_brand_wcpay_request:ok" ) {okAjax('{$order_no}','{$order_no}');}else{WeixinJSBridge.log(res.err_msg);// alert('{$order_no}');alert('支付失败!'+res.err_code+res.err_desc+res.err_msg);}*/});}</script>
复制代码
代码粘贴完毕。
再说接下来的坑。
哎!只能说搞微信官方的支付,是无处不是坑。
回调方法,我没有用官方的回调,太麻烦不想用,看我的多简单。
function notify(){// $out_trade_no= '2017082950482';$xml = $GLOBALS['HTTP_RAW_POST_DATA'];// file_put_contents('1.txt',$xml);libxml_disable_entity_loader(true);$arr= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);if($arr['return_code']=='SUCCESS'){//自己的业务逻辑,随便修改数据库 随便加钱,如果这里有问题,可以使用file_put_contents保存成文件,查看是否有回调数据。//好像是修改成功给官方回应的信息$data['return_code']='SUCCESS';$data['return_msg']='OK';return toXml($data);}}
复制代码
添加一个公功函数
//将数组转换xml格式function toXml($arr){if (!is_array($arr) || count($arr)<=0){return '数组数据异常!';}$xml = "<xml>";foreach ($arr as $key=>$val){if (is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">";}else{$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";}}$xml.="</xml>";return $xml;}
复制代码
哎,官方写那么多,被我用这几行代码给成功回调。所以官方文档也是个坑。
到处都是坑,坑到还是自己。
再多的坑 ,还是要看官方的文档,以免别人包有问题。这里我也留下一个坑,就是在jspay.php文件里面的配置信息,需要您仔细看一下。