Home > Backend Development > PHP Tutorial > ThinkPHP WeChat code scanning payment interface

ThinkPHP WeChat code scanning payment interface

WBOY
Release: 2016-07-29 08:56:01
Original
939 people have browsed it

Recently I have been struggling with WeChat scan code payment. I read the official WeChat documentation and searched many web pages. I found that it does not match the documentation/demo. Now I have figured it out myself (the file name has been changed). I will post it and share it.

1. Place useful official lib files and related files in the vendor

2. Call the same payment interface

WechatController

<span>//<span>调用同一支付接口<br><span>public function <span>native(<span>$v_oid<span>=<span>'',<span>$v_amount<span>=<span>''){<br><span>ini_set(<span>'date.timezone',<span>'Asia/Shanghai');<br><span>error_reporting(<span>E_ERROR);<br><span>//<span>添加相关的类文件<br><span><span>vendor(<span>'Wxpay.WxPayApi');<br><span>vendor(<span>'Wxpay.log');<br><span>vendor(<span>'Wxpay.NativePay');<br><span>//<span>初始化日志<br><span><span>$logHandler<span>= new \<span>CLogFileHandler(<span>"../logs/"<span>.<span>date(<span>'Y-m-d')<span>.<span>'.log');<br><span>$log <span>= \<span>Log<span>::<span>Init(<span>$logHandler, <span>15);<br><span>//<span>接受订单信息<br><span><span>if(<span>trim(<span>$v_oid)<span><><span>"")               <span>//<span>判断是否有传递订单号<br><span>    {<br><span>$v_oid <span>= <span>trim(<span>$v_oid);<br>    }<br><span>else<br><span>    {<br><span>$this<span>-><span>error(<span>'<span>订单号为空<span>',<span>U(<span>'User/index'));<br><span>exit;<br>    }<br><span>$v_amount <span>= <span>trim(<span>$v_amount<span>*<span>100);                   <span>//<span>支付金额<br><span><br><span><span>//<span>调用同一支付接口<br><span><span>$notify <span>= new \<span>NativePay();<br><span>$input <span>= new \<span>WxPayUnifiedOrder();<br><span>//<span>商品描述<span>----------------------------<span>需要参数传递<span>/<span>统一信息<br><span><span>$input<span>-><span>SetBody(<span>"<span>商品描述位置<span>");<br><span>//<span>附加数据<br><span><span>$input<span>-><span>SetAttach(<span>"<span>附加数据位置<span>");<br><span>//<span>商户订单号<br><span><span>$input<span>-><span>SetOut_trade_no(<span>$v_oid);<br><span>//<span>总金额<br><span><span>$input<span>-><span>SetTotal_fee(<span>$v_amount);<br><span>//<span>开始时间<br><span><span>$input<span>-><span>SetTime_start(<span>date(<span>"YmdHis"));<br><span>//<span>结束时间<br><span><span>$input<span>-><span>SetTime_expire(<span>date(<span>"YmdHis", <span>time() <span>+ <span>600));<br><span>//<span>商品标记<br><span><span>$input<span>-><span>SetGoods_tag(<span>"<span>商品标记位置<span>");<br><span>//<span>异步通知地址<span>,<span>不能携带任何参数<br><span><span>$input<span>-><span>SetNotify_url(<span>U(<span>'NotifyProcess'));<br><span>//<span>交易类型<br><span><span>$input<span>-><span>SetTrade_type(<span>"NATIVE");<br><span>//<span>商品<span>id<br><span><span>$input<span>-><span>SetProduct_id(<span>"123456789");<br><span>//<span>获取同一支付接口结果<br><span><span>$result <span>= <span>$notify<span>-><span>GetPayUrl(<span>$input);<br><span>$url2 <span>= <span>$result[<span>"code_url"];<br><span>$this<span>-><span>assign(<span>'data',<span>urlencode(<span>$url2));<br><span>$this<span>-><span>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><span>三.</span><span><span>微信支付生成二维码</span><br><span>public function <span>qrcode(){<br><span>error_reporting(<span>E_ERROR);<br><span>vendor(<span>'Wxpay.phpqrcode.phpqrcode');<br><span>$url <span>= <span>$_GET[<span>"data"];<br>    \<span>QRcode<span>::<span>png(<span>$url);<br>}<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span>四.异步处理</span><br><span>具体处理流程和方式按个人需求</span>
Copy after login
<br><span>//<span>微信异步查询订单机制<br><span>public function <span>Queryorder(<span>$transaction_id)<br>{<br><span>$input <span>= new \<span>WxPayOrderQuery();<br><span>$input<span>-><span>SetTransaction_id(<span>$transaction_id);<br><span>$result <span>= \<span>WxPayApi<span>::<span>orderQuery(<span>$input);<br>    \<span>Log<span>::<span>DEBUG(<span>"query:" <span>. <span>json_encode(<span>$result));<br><span>if(<span>array_key_exists(<span>"return_code", <span>$result)<br><span>&& <span>array_key_exists(<span>"result_code", <span>$result)<br><span>&& <span>$result[<span>"return_code"] <span>== <span>"SUCCESS"<br><span><span>&& <span>$result[<span>"result_code"] <span>== <span>"SUCCESS")<br>    {<br><span>return true;<br>    }<br><span>return false;<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><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span>五.再来个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><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></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><<span>form <span>action=<span>"" <span>method=<span>"post" <span>id=<span>"myForm-table"><br>    <<span>h2><span>订单信息</<span>h2><br>    <<span>div <span>class=<span>"orderNumber"><br>        <<span>p <span>class=<span>"a"><span>订单号:<span>"{$v_oid}"</<span>p><br>        <<span>p <span>class=<span>"b"><br>            <<span>span <span>class=<span>"left"><span>应付金额:<<span>strong><<span>em><span>{$v_amount}</<span>em><span>元</<span>strong></<span>span><br>            <<span>span <span>class=<span>"right"><span>付款后,飞行之翼客服会与您联系,如有问题请咨询<span>24<span>小时飞行热线 <span>400-700-2016</<span>span><br>        </<span>p><br>    </<span>div><br>    <<span>h2><span>微信支付</<span>h2><br>    <<span>img <span>src=<span>"{:U('qrcode')}?data={$data}" <span>alt=<span>"<span>微信扫码<span>"><br>    <<span>div <span>class=<span>"order-submit"><<span>a <span>href=<span>"javascript:<span>history<span>.<span>go<span>(-1);" <span>class=<span>"submitOrder Order"><span>返回其他支付方式</<span>a></<span>div><br></<span>form></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span>成功的图就不附上了,这是我遇到的问题,由于要重构,所以比较乱,无效代码也比较多,需要整理,记录下来,分享一二</span>
Copy after login
<span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></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>
Copy after login

The above introduces the ThinkPHP WeChat QR code scanning payment interface, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template