首页 > 作文

thinkphp5实现微信扫码支付

更新时间:2023-04-08 04:04:13 阅读: 评论:0

本文实例为大家分享了thinkphp5微信扫码支付的具体代码,供大家参考,具体学乐教育内容如下

配置wxpay.config.php

控制器

//微信支付//参数 订单 价格 public function wxpay($order_number,$money) {  header("content-type:text/html;chart=utf-8");   //require_once vendor_path.'/alipaymobile/config.php';  require_once vendor_path.'/wxpay/wxpay.api.php';//引入微信支付  require_once vendor_path.'/wxpay/wxpay.notify.php';  require_once vendor_path.'/wxpay/phpqrcode/phpqrcode.php';  $input = new \wxpayunifiedorder();//统一下单  $config = new \wxpayconfig();//配置参数  $notify = new \qrcode();  //$paymoney = input('post.paymoney'); //支付金额  $paymoney = $money; //测试写死  //$paymoney = 0.01; //测试写死  //$str = date('ymd').substr(implode(null, array_map('ord', str_split(substr(uniqid()圣诞祝福语英文, 7, 13), 1))), 0, 8);//生成订单号   $out_trade_no = $order_number; //商户订单号(自定义)  $goods_name = '扫码支付'.$paymoney.'元'; //商品名称(自定义)  $input->tbody($goods_name);  $input->tattach($goods_name);  $input->tout_trade_no($out_trade_no);  $input->ttotal_fee($paymoney*100);//金额乘以100  $input->ttime_start(date("ymdhis"));  $input->ttime_expire(date("ymdhis", time() + 600));  $input->tgoods_tag("test");  //回调地址 注意能访问  $input->tnotify_url("http://www.域名.com/index.php/index/index/wxpaynotifyurl"); //回调地址  $input->ttrade_type("native");  $input->tproduct_id('123456789');//商品id  $result = \wxpayapi::unifiedorder($config, $input);  if($result['result_code']=='success' && $result['return_code']=='success') {   $url = $result["code_url"];   $this->assign('money',$paymoney);   $this->assign('url',$url);   $this->assign('num',$out_trade_no);   //映射视图 微信二维码需要自己生成    return view("../../../template/wxpay");  }el{   $this->error('参数错误');   }  // return view(); }

视图 生成二维码 进行扫描付款

<!doctype html><html>童年趣事作文300字<head><meta chart="utf-8"><title>微信支付页面</title><style>.wxsm{ width:1200px; margin:0 auto; overflow:hidden;}.wxsmt{ width:100%; height:40px; border-bottom:1px solid #ccc;}.wxsmt h2{ font-size:14px; line-height:40px; color:#232323; font-weight:400; text-align:center;}.wxsmc{ width:100%; overflow:hidden;}.smcr{ overflow:hidden; margin:0 auto; }.smcr h2{ font-size:14px; font-weight:400; line-height:40px; text-align:ce美发店名nter;}.smcr h2 span{ font-size:20px; color:#f23535;}.smcrb{ width:100%; overflow:hidden;;}.smm{ width:218px; height:284px; border:1px solid #3cb035; background:#3cb035; margin:0 auto}.smm img{ width:218px; height:218px; background:#fff;}.smm span{ display:block; color:#fff; line-height:66px; text-align:center;}</style><script src="__temp__/js/jquery-1.10.1.min.js"></script></head><body><div class="wxsm"> <div class="wxsmt">  <h2>订单提交成功,请尽快付款</h2> </div> <div class="wxsmc">  <div class="smcr">   <h2>应付金额:<span>{$money}</span>元</h2>   <div class="smcrb">    <div class="smm">     <img src="/vendor/wxpay/qrcode.php?data=<?php echo urlencode($url);?>"/>     <span>打开微信,扫描二维码支付</span>    </div>   </div>  </div> </div></div><script>  //设置每隔1000毫秒执行一次load() 方法  var myintval=tinterval(function(){loads()},1000);  function loads(){     var xmlhttp;     // 轮询的浏览器设置     if (window.xmlhttprequest){      // code for ie7+, firefox, chrome, opera, safari      xmlhttp=new xmlhttprequest();     }el{      // code for ie6, ie5      xmlhttp=new activexobject("microsoft.xmlhttp");     }     xmlhttp.onreadystatechange=function(){      if (xmlhttp.readystate==4 && xmlhttp.status==200){       trade_state=json.par(xmlhttp.respontext);       console.log(trade_state);//打印查看轮询的状态,也可以关闭。      //判断订单支付状态 并用document.getelementbyid方法赋值到mydiv中;      if(trade_state.code=='success'){               //延迟3000毫秒执行tz() 方法       clearinterval(myintval);       //进行跳转。        var url = "{:url('index/center')}";        var http = window.location.protocol;        var zhu = window.location.host;        ttimeout("location.href='"+http+"//"+zhu+""+url+"'",1500);       }el if(trade_state.code=='refund'){               clearinterval(myintval);       }el if(trade_state.code=='notpay'){                }el if(trade_state.code=='clod'){               clearinterval(myintval);      }el if(trade_state.code=='revoked'){               clearinterval(myintval);      }el if(trade_state.code=='urpaying'){              }el if(trade_state.code=='payerror'){               clearinterval(myintval);       }             }     }   //orderquery.php 文件返回订单状态,通过订单状态确定支付状态   xmlhttp.open("post","/vendor/wxpay/orderquery.php",fal);   //下面这句话必须有   //把标签/值对添加到要发送的头文件。   xmlhttp.trequestheader("content-type","application/x-www-form-urlencoded");   xmlhttp.nd("out_trade_no=<?php echo $num;?>");     } </script></body></html>

支付回调

public function wxpaynotifyurl() {  //$xml = $globals['http_raw_post_data']; //返回的xml $xml = file_get_contents("php://input"); //$results = db('fund') -> where('id',1) -> update(['a'=>$xml]);exit();  $xmlarr = $this->init($xml);  file_put_contents(dirname(__file__).'/xml.txt',$xml); //记录日志 支付成功后查看xml.txt文件是否有内容 如果有xml格式文件说明回调成功    $out_trade_no=$xmlarr['out_trade_no']; //订单号  $total_fee=$xmlarr['total_fee']/100; //回调回来的xml文件中金额是以上海高复分为单位的  $result_code=$xmlarr['result_code']; //状态  //$result = db('order') -> where(['order' => $out_trade_no]) -> find();  //if($result['price'] == $total_fee){   if($result_code=='success'){ //数据库操作    //处理数据库操作 例如修改订单状态 给账户充值等等      echo 'success'; //返回成功给微信端 一定要带上不然微信会一直回调8次    exit;    }el{ //失败    return fal;    exit;   }     } public function init($xml) {  $array_data = json_decode(json_encode(simplexml_load_string($xml, 'simplexmlelement', libxml_nocdata)), true);  return $array_data; }

简单的写了一下 能实现简单的thinkphp5+微信扫码支付的大体流程 若有有问题的地方 还请各位大神指点

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-08 04:04:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/4e58120e927654f6aac2fc4abd98a213.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:thinkphp5实现微信扫码支付.doc

本文 PDF 下载地址:thinkphp5实现微信扫码支付.pdf

标签:回调   状态   订单   金额
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图