入坑一下午,爬坑各种套。微信支付爬坑进行时
准备条件
下载压缩包extend.zip。直接解压在目录即可。

包简介:
首先感谢Mikkle的微信包和微信支付包。该包的教程可以详细关注。
http://www.thinkphp.cn/extend/1042.html
然后感谢微信支付官网的例子,详细关注
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

准备条件最重要的是设置微信支付官方的地址
爬坑教程-微信h5支付

第一坑
支付授权目录,一定要到要填写到指定到具体的模块和控制器中。还不能使用大写

  1. 支付地址为:www.thinkphp.cn/pay.php?payment='.$add['payment'].'&ordersn='.$add['ordersn']
  2. 设置支付授权目录:www.thinkphp.cn
  3.  
  4. 支付地址为:www.thinkphp.cn/index/wxpay/pay/id/123
  5. 设置支付授权目录:www.thinkphp.cn/index/wxpay/pay/id
  6.  
  7. 支付地址为:www.thinkphp.cn/index/wxpay/pay/
  8. 设置支付授权目录:www.thinkphp.cn/index/wxpay/pay/

复制代码

 

整个支付流程,详细查看(https://www.kancloud.cn/mikkle/thinkphp5_study/376461
)
第一步 获取openid

  1.   private $open_id;
  2.     public function _initialize(){
  3.         ini_set('date.timezone','Asia/Shanghai');
  4.         $tools=new \JqTool\WxPay\JsPay();
  5.         if (!Session::get('openid')){
  6.             $openId= $tools->setConfig()->GetOpenid();
  7.             Session::set('openid',$openId);
  8.         }else{
  9.             $this->open_id=Session::get('openid');
  10.         }
  11.         config($this->wx_config);
  12.         $this->notify_url='http://'.$_SERVER['HTTP_HOST'].'/mobile/Chong/notify';
  13.     }

复制代码

 

第二步 根据openid进行统一下单获取订单号
第三步 根据统一下单的订单号 获取获取JsApi$getParameters参数

  1. <?php
  2.  
  3. namespace app\index\controller;
  4.  
  5.  
  6. use mikkle\tp_wxpay\JsApi_pub as JaApi;
  7. use mikkle\tp_wxpay\UnifiedOrder_pub as UnifiedOrder;
  8. use think\Controller;
  9. use think\Session;
  10.  
  11. class Wx extends Controller
  12. {
  13.  
  14.     protected $wx_config=[
  15.         'wechat_appid'=>'',//微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
  16.         'wechat_mchid'=>'',//受理商ID,身份标识 商户号
  17.         'wechat_appkey'=>'',//商户支付**Key。审核通过后,在微信发送的邮件中查看
  18.         'wechat_appsecret'=>'',//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
  19.  
  20.         //证书路径,注意应该填写绝对路径  不用证书也是能支付的
  21.         'sslcert_path'=>'',
  22.         'sslkey_path'=> '',
  23.     ];
  24.     private $open_id;
  25.     private $notify_url;
  26.     public function _initialize(){
  27.         ini_set('date.timezone','Asia/Shanghai');
  28.         $tools=new \JqTool\WxPay\JsPay();
  29.         if (!Session::get('openid')){
  30.             $openId= $tools->setConfig()->GetOpenid();
  31.             Session::set('openid',$openId);
  32.         }else{
  33.             $this->open_id=Session::get('openid');
  34.         }
  35.         config($this->wx_config);
  36.         $this->notify_url='http://'.$_SERVER['HTTP_HOST'].'/mobile/Chong/notify';
  37.     }
  38.     function index(){
  39.         $data=$this->payByOrderNo();
  40.         $this->assign('amount',$data['amount']);
  41.         $this->assign('order_no',$data['order_no']);
  42.         $this->assign("jsApiParameters" ,$data['jsApiParameters']);
  43.         $this->assign('openid',$this->open_id);
  44.         return $this->fetch();
  45.     }
  46.     function  payByOrderNo(){
  47.         $data['order_no']=time();
  48.         $data['amount']=1;
  49.         $unified_order = $this->unifiedOrder($data);  //统一下单
  50.         $jsApiParameters=$this->getParameters($unified_order);
  51.         return ['code'=>1001,'order_no'=> $data['order_no'],'jsApiParameters'=>$jsApiParameters,
  52.             'amount'=>$data['amount'],];
  53.     }
  54.     protected function unifiedOrder($data=[]){
  55.         $unifiedOrder = new UnifiedOrder();
  56.         $unifiedOrder->setParameter("openid",$this->open_id);             // openid
  57.         $unifiedOrder->setParameter("body",'商品订单号'+$data['order_no']);         // 商品描术
  58.         $unifiedOrder->setParameter("out_trade_no",$data['order_no'].'_'.$unifiedOrder->createNoncestr(6));  // 商户订单号
  59.         $unifiedOrder->setParameter("total_fee",$data['amount']*100);    // 总金额
  60.         $unifiedOrder->setParameter("notify_url",$this->notify_url);  // 通知地址
  61.         $unifiedOrder->setParameter("trade_type","JSAPI");      // 交易类型
  62.         return $unifiedOrder->getPrepayId();
  63.     }
  64.     protected function getParameters($unified_order=''){
  65.         $jsApi= new JaApi();
  66.         $jsApi->setPrepayId($unified_order);
  67.         $jsApiParameters = $jsApi->getParameters();
  68.         return $jsApiParameters;
  69.     }
  70.  
  71. }

复制代码

 

最后 JsApi中支付

  1. <center><h1 onclick='callpay()'>点击支付</h1></center>
  2. {$jsApiParameters}
  3. <script>
  4.     function callpay()
  5.     {
  6.         if (typeof WeixinJSBridge == "undefined"){
  7.             if( document.addEventListener ){
  8.                 document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  9.             }else if (document.attachEvent){
  10.                 document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
  11.                 document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  12.             }
  13.         }else{
  14.             jsApiCall();
  15.         }
  16.     }
  17.         //调用微信JS api 支付
  18.     function jsApiCall()
  19.     {
  20.         WeixinJSBridge.invoke(
  21.             'getBrandWCPayRequest',
  22.                 {$jsApiParameters},
  23.             function(res){
  24.             console.log(res.err_msg)
  25.                /*
  26.                 if(res.err_msg == "get_brand_wcpay_request:ok" ) {
  27.                     okAjax('{$order_no}','{$order_no}');
  28.                 }else{
  29.                     WeixinJSBridge.log(res.err_msg);
  30.                      // alert('{$order_no}');
  31.                      alert('支付失败!'+res.err_code+res.err_desc+res.err_msg);
  32.                  }
  33.                  */
  34.             }
  35.         );
  36.     }
  37. </script>

复制代码

 

代码粘贴完毕。
再说接下来的坑。
哎!只能说搞微信官方的支付,是无处不是坑。

回调方法,我没有用官方的回调,太麻烦不想用,看我的多简单。

  1.   function notify(){
  2. //   $out_trade_no= '2017082950482';
  3.      $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  4. //   file_put_contents('1.txt',$xml);
  5.      libxml_disable_entity_loader(true);
  6.      $arr= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  7.      if($arr['return_code']=='SUCCESS'){
  8.            //自己的业务逻辑,随便修改数据库  随便加钱,如果这里有问题,可以使用file_put_contents保存成文件,查看是否有回调数据。
  9.                    
  10.  
  11.                      //好像是修改成功给官方回应的信息
  12.                       $data['return_code']='SUCCESS';
  13.                       $data['return_msg']='OK';
  14.                       return toXml($data);
  15.      }
  16.   }

复制代码

 

添加一个公功函数

  1. //将数组转换xml格式
  2. function toXml($arr){
  3.   if (!is_array($arr) || count($arr)<=0){
  4.     return '数组数据异常!';
  5.   }
  6.   $xml = "<xml>";
  7.   foreach ($arr as $key=>$val){
  8.     if (is_numeric($val)){
  9.       $xml.="<".$key.">".$val."</".$key.">";
  10.     }else{
  11.       $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  12.     }
  13.   }
  14.   $xml.="</xml>";
  15.   return $xml;
  16. }

复制代码

 

哎,官方写那么多,被我用这几行代码给成功回调。所以官方文档也是个坑。
到处都是坑,坑到还是自己。

再多的坑 ,还是要看官方的文档,以免别人包有问题。这里我也留下一个坑,就是在jspay.php文件里面的配置信息,需要您仔细看一下。

相关文章:

  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2021-06-24
  • 2021-09-05
  • 2021-04-04
  • 2022-12-23
猜你喜欢
  • 2021-04-15
  • 2021-05-28
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-10-31
相关资源
相似解决方案