Home > php教程 > php手册 > body text

ThinkPHP微信扫码支付接口 - 红心老K

WBOY
Release: 2016-05-20 11:54:36
Original
1005 people have browsed it

最近折腾微信扫码支付,看了微信官方文档,找了很多网页,发现和文档/demo不匹配,现在自己算是弄出来了(文件名称有所更改),贴出来分享一下

 

一.将有用的官方lib文件和使用的相关文件放置到vendor当中

二.调用同一支付接口

WechatController

<span style="color: #aeaeae; font-size: 18px;">//<span style="color: #aeaeae; font-family: '宋体';">调用同一支付接口<br><span style="color: #fbde2d;">public function <span style="color: #ff6400;">native(<span style="color: #ff6400;">$v_oid<span style="color: #fbde2d;">=<span style="color: #61ce3c;">'',<span style="color: #ff6400;">$v_amount<span style="color: #fbde2d;">=<span style="color: #61ce3c;">''){<br>    <span style="color: #8da6ce;">ini_set(<span style="color: #61ce3c;">'date.timezone',<span style="color: #61ce3c;">'Asia/Shanghai');<br>    <span style="color: #8da6ce;">error_reporting(<span style="color: #d8fa3c;">E_ERROR);<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">添加相关的类文件<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #ff6400;">vendor(<span style="color: #61ce3c;">'Wxpay.WxPayApi');<br>    <span style="color: #ff6400;">vendor(<span style="color: #61ce3c;">'Wxpay.log');<br>    <span style="color: #ff6400;">vendor(<span style="color: #61ce3c;">'Wxpay.NativePay');<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">初始化日志<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$logHandler<span style="color: #fbde2d;">= new \<span style="color: #ff6400;">CLogFileHandler(<span style="color: #61ce3c;">"../logs/"<span style="color: #fbde2d;">.<span style="color: #8da6ce;">date(<span style="color: #61ce3c;">'Y-m-d')<span style="color: #fbde2d;">.<span style="color: #61ce3c;">'.log');<br>    <span style="color: #026609;">$log <span style="color: #fbde2d;">= \<span style="color: #ff6400;">Log<span style="color: #fbde2d;">::<span style="color: #ff6400;">Init(<span style="color: #026609;">$logHandler, <span style="color: #d8fa3c;">15);<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">接受订单信息<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #fbde2d;">if(<span style="color: #8da6ce;">trim(<span style="color: #ff6400;">$v_oid)<span style="color: #fbde2d;"><span style="color: #61ce3c;">"")               <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">判断是否有传递订单号<br><span style="color: #aeaeae; font-family: '宋体';">    {<br>        <span style="color: #ff6400;">$v_oid <span style="color: #fbde2d;">= <span style="color: #8da6ce;">trim(<span style="color: #ff6400;">$v_oid);<br>    }<br>    <span style="color: #fbde2d;">else<br><span style="color: #fbde2d;">    {<br>        <span style="color: #026609;">$this<span style="color: #fbde2d;">-><span style="color: #ff6400;">error(<span style="color: #61ce3c;">'<span style="color: #61ce3c; font-family: '宋体';">订单号为空<span style="color: #61ce3c;">',<span style="color: #ff6400;">U(<span style="color: #61ce3c;">'User/index'));<br>        <span style="color: #fbde2d;">exit;<br>    }<br><br>    <span style="color: #ff6400;">$v_amount <span style="color: #fbde2d;">= <span style="color: #8da6ce;">trim(<span style="color: #ff6400;">$v_amount<span style="color: #fbde2d;">*<span style="color: #d8fa3c;">100);                   <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">支付金额<br><span style="color: #aeaeae; font-family: '宋体';"><br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">调用同一支付接口<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$notify <span style="color: #fbde2d;">= new \<span style="color: #ff6400;">NativePay();<br>    <span style="color: #026609;">$input <span style="color: #fbde2d;">= new \<span style="color: #ff6400;">WxPayUnifiedOrder();<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">商品描述<span style="color: #aeaeae;">----------------------------<span style="color: #aeaeae; font-family: '宋体';">需要参数传递<span style="color: #aeaeae;">/<span style="color: #aeaeae; font-family: '宋体';">统一信息<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetBody(<span style="color: #61ce3c;">"<span style="color: #61ce3c; font-family: '宋体';">商品描述位置<span style="color: #61ce3c;">");<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">附加数据<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetAttach(<span style="color: #61ce3c;">"<span style="color: #61ce3c; font-family: '宋体';">附加数据位置<span style="color: #61ce3c;">");<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">商户订单号<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetOut_trade_no(<span style="color: #ff6400;">$v_oid);<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">总金额<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetTotal_fee(<span style="color: #ff6400;">$v_amount);<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">开始时间<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetTime_start(<span style="color: #8da6ce;">date(<span style="color: #61ce3c;">"YmdHis"));<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">结束时间<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetTime_expire(<span style="color: #8da6ce;">date(<span style="color: #61ce3c;">"YmdHis", <span style="color: #8da6ce;">time() <span style="color: #fbde2d;">+ <span style="color: #d8fa3c;">600));<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">商品标记<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetGoods_tag(<span style="color: #61ce3c;">"<span style="color: #61ce3c; font-family: '宋体';">商品标记位置<span style="color: #61ce3c;">");<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">异步通知地址<span style="color: #aeaeae;">,<span style="color: #aeaeae; font-family: '宋体';">不能携带任何参数<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetNotify_url(<span style="color: #ff6400;">U(<span style="color: #61ce3c;">'NotifyProcess'));<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">交易类型<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetTrade_type(<span style="color: #61ce3c;">"NATIVE");<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">商品<span style="color: #aeaeae;">id<br><span style="color: #aeaeae;">    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetProduct_id(<span style="color: #61ce3c;">"123456789");<br>    <span style="color: #aeaeae;">//<span style="color: #aeaeae; font-family: '宋体';">获取同一支付接口结果<br><span style="color: #aeaeae; font-family: '宋体';">    <span style="color: #026609;">$result <span style="color: #fbde2d;">= <span style="color: #026609;">$notify<span style="color: #fbde2d;">-><span style="color: #ff6400;">GetPayUrl(<span style="color: #026609;">$input);<br>    <span style="color: #026609;">$url2 <span style="color: #fbde2d;">= <span style="color: #026609;">$result[<span style="color: #61ce3c;">"code_url"];<br><br>    <span style="color: #026609;">$this<span style="color: #fbde2d;">-><span style="color: #ff6400;">assign(<span style="color: #61ce3c;">'data',<span style="color: #8da6ce;">urlencode(<span style="color: #026609;">$url2));<br>    <span style="color: #026609;">$this<span style="color: #fbde2d;">-><span style="color: #ff6400;">assign('v_oid',$v_oid);    $this->assign('v_amount',$v_amount/100);    $this->display();}三.生成二维码</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Copy after login
<span style="color: #aeaeae; font-size: 18px;"><span style="color: #000000;">三.</span><span style="color: #aeaeae; font-family: '宋体';"><span style="color: #000000;">微信支付生成二维码</span><br><span style="color: #fbde2d;">public function <span style="color: #ff6400;">qrcode(){<br>    <span style="color: #8da6ce;">error_reporting(<span style="color: #d8fa3c;">E_ERROR);<br>    <span style="color: #ff6400;">vendor(<span style="color: #61ce3c;">'Wxpay.phpqrcode.phpqrcode');<br>    <span style="color: #026609;">$url <span style="color: #fbde2d;">= <span style="color: #026609;">$_GET[<span style="color: #61ce3c;">"data"];<br>    \<span style="color: #ff6400;">QRcode<span style="color: #fbde2d;">::<span style="color: #ff6400;">png(<span style="color: #026609;">$url);<br>}<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span style="font-size: 18px;">四.异步处理</span><br><span style="font-size: 18px;">具体处理流程和方式按个人需求</span>
Copy after login
<br><span style="color: #aeaeae; font-size: 18px;">//<span style="color: #aeaeae; font-family: '宋体';">微信异步查询订单机制<br><span style="color: #fbde2d;">public function <span style="color: #ff6400;">Queryorder(<span style="color: #ff6400;">$transaction_id)<br>{<br>    <span style="color: #026609;">$input <span style="color: #fbde2d;">= new \<span style="color: #ff6400;">WxPayOrderQuery();<br>    <span style="color: #026609;">$input<span style="color: #fbde2d;">-><span style="color: #ff6400;">SetTransaction_id(<span style="color: #ff6400;">$transaction_id);<br>    <span style="color: #026609;">$result <span style="color: #fbde2d;">= \<span style="color: #ff6400;">WxPayApi<span style="color: #fbde2d;">::<span style="color: #ff6400;">orderQuery(<span style="color: #026609;">$input);<br>    \<span style="color: #ff6400;">Log<span style="color: #fbde2d;">::<span style="color: #ff6400;">DEBUG(<span style="color: #61ce3c;">"query:" <span style="color: #fbde2d;">. <span style="color: #ff6400;">json_encode(<span style="color: #026609;">$result));<br>    <span style="color: #fbde2d;">if(<span style="color: #8da6ce;">array_key_exists(<span style="color: #61ce3c;">"return_code", <span style="color: #026609;">$result)<br>        <span style="color: #fbde2d;">&& <span style="color: #8da6ce;">array_key_exists(<span style="color: #61ce3c;">"result_code", <span style="color: #026609;">$result)<br>        <span style="color: #fbde2d;">&& <span style="color: #026609;">$result[<span style="color: #61ce3c;">"return_code"] <span style="color: #fbde2d;">== <span style="color: #61ce3c;">"SUCCESS"<br><span style="color: #61ce3c;">        <span style="color: #fbde2d;">&& <span style="color: #026609;">$result[<span style="color: #61ce3c;">"result_code"] <span style="color: #fbde2d;">== <span style="color: #61ce3c;">"SUCCESS")<br>    {<br>        <span style="color: #fbde2d;">return true;<br>    }<br>    <span style="color: #fbde2d;">return false;<br>}<br><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: #aeaeae; font-family: '宋体';"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #fbde2d;"><span style="color: #000000;">五.再来个form表单展示二维码</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: #aeaeae; font-size: 18px;"><span style="color: #aeaeae; font-family: '宋体';"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #fbde2d;"><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Copy after login
<span style="font-size: 18px;">form <span style="color: #ff6400;">action=<span style="color: #61ce3c; font-weight: bold;">"" <span style="color: #ff6400;">method=<span style="color: #61ce3c; font-weight: bold;">"post" <span style="color: #ff6400;">id=<span style="color: #61ce3c; font-weight: bold;">"myForm-table"><br>    h2><span style="font-weight: bold; font-family: '宋体';">订单信息<span style="color: #7f90aa;">h2><br>    div <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"orderNumber"><br>        p <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"a"><span style="font-weight: bold; font-family: '宋体';">订单号:<span style="font-weight: bold;">"{$v_oid}"<span style="color: #7f90aa;">p><br>        p <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"b"><br>            span <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"left"><span style="font-weight: bold; font-family: '宋体';">应付金额:strong>em><span style="font-weight: bold;">{$v_amount}<span style="color: #7f90aa;">em><span style="font-weight: bold; font-family: '宋体';">元<span style="color: #7f90aa;">strong><span style="color: #7f90aa;">span><br>            span <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"right"><span style="font-weight: bold; font-family: '宋体';">付款后,飞行之翼客服会与您联系,如有问题请咨询<span style="font-weight: bold;">24<span style="font-weight: bold; font-family: '宋体';">小时飞行热线 <span style="font-weight: bold;">400-700-2016<span style="color: #7f90aa;">span><br>        <span style="color: #7f90aa;">p><br>    <span style="color: #7f90aa;">div><br>    h2><span style="font-weight: bold; font-family: '宋体';">微信支付<span style="color: #7f90aa;">h2><br>    img <span style="color: #ff6400;">src=<span style="color: #61ce3c; font-weight: bold;">"{:U('qrcode')}?data={$data}" <span style="color: #ff6400;">alt=<span style="color: #61ce3c; font-weight: bold;">"<span style="color: #61ce3c; font-weight: bold; font-family: '宋体';">微信扫码<span style="color: #61ce3c; font-weight: bold;">"><br>    div <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"order-submit">a <span style="color: #ff6400;">href=<span style="color: #61ce3c; font-weight: bold;">"javascript:<span style="color: #ff6400; font-weight: bold;">history<span style="color: #61ce3c; font-weight: bold;">.<span style="color: #979748; font-weight: bold;">go<span style="color: #61ce3c; font-weight: bold;">(-1);" <span style="color: #ff6400;">class=<span style="color: #61ce3c; font-weight: bold;">"submitOrder Order"><span style="font-weight: bold; font-family: '宋体';">返回其他支付方式<span style="color: #7f90aa;">a><span style="color: #7f90aa;">div><br><span style="color: #7f90aa;">form></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><br><br><span style="font-size: 18px;">成功的图就不附上了,这是我遇到的问题,由于要重构,所以比较乱,无效代码也比较多,需要整理,记录下来,分享一二</span>
Copy after login
<span style="color: #aeaeae; font-size: 18px;"><span style="color: #aeaeae; font-family: '宋体';"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #ff6400;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #ff6400;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #8da6ce;"><span style="color: #61ce3c;"><span style="color: #026609;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #026609;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #61ce3c;"><span style="color: #fbde2d;"><span style="color: #fbde2d;"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Copy after login
<br><br>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template