ThinkPHP5.0 实现 app微信支付功能

前端之家收集整理的这篇文章主要介绍了ThinkPHP5.0 实现 app微信支付功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

相对于之前随笔写的来说,PHP对接app微信支付功能就相对简单的多了,最近有加我的朋友问到app微信支付,所以我把app微信支付的demo展示出来供大家参考一下,希望对做这个功能的人员给予参考和帮助,

首先还是到支付页面

微信支付的步骤大致和支付宝支付的步骤差不多,这里我讲的详细一点。

第一步:建立一个支付控制器pay.PHP,是由上个页面选择好支付方式之后确认支付后需要经过的控制器(这里选择的是微信支付),代码 如下:

PHP namespace app\mobile\controller; <span style="color: #0000ff">class Pay <span style="color: #0000ff">extends<span style="color: #000000"> Controller
{
<span style="color: #0000ff">public <span style="color: #0000ff">function<span style="color: #000000"> pay_order()
{
<span style="color: #800080">$res = <span style="color: #0000ff">new<span style="color: #000000"> OrderGoods();
<span style="color: #008000">//<span style="color: #008000">获取订单号
<span style="color: #800080">$where['id'] = input('post.order_sn'<span style="color: #000000">);
<span style="color: #800080">$reoderSn = input('post.order_sn'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">查询订单信息
<span style="color: #800080">$order_info = <span style="color: #800080">$res->where(<span style="color: #800080">$where)-><span style="color: #000000">find();
<span style="color: #008000">//<span style="color: #008000">获取支付方式
<span style="color: #800080">$pay_type = input('post.pay_type');<span style="color: #008000">//<span style="color: #008000">微信支付 或者支付宝支付
//获取支付金额
<span style="color: #800080">$money = input('post.totle_sum'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">判断支付方式
<span style="color: #0000ff">switch (<span style="color: #800080">$pay_type<span style="color: #000000">) {
<span style="color: #0000ff">case 'ali';<span style="color: #008000">//<span style="color: #008000">如果支付方式为支付宝支付

            <span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;case</span> 'wx'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$type</span>['pay_type'] = 'wx';<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;更新支付方式为微信</span>
            <span style="color: #800080"&gt;$res</span>->where(<span style="color: #800080"&gt;$where</span>)->update(<span style="color: #800080"&gt;$type</span><span style="color: #000000"&gt;);

            </span><span style="color: #800080"&gt;$wx</span> = <span style="color: #0000ff"&gt;new</span> Wxpay();<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;实例化微信支付控制器</span>

            <span style="color: #800080"&gt;$body</span> = '订单号' . <span style="color: #800080"&gt;$order_info</span>;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;支付说明</span>

            <span style="color: #800080"&gt;$out_trade_no</span> = <span style="color: #800080"&gt;$reoderSn</span>;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;订单号</span>

            <span style="color: #800080"&gt;$total_fee</span> = <span style="color: #800080"&gt;$money</span> * 100;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;支付金额(乘以100)</span>

            <span style="color: #800080"&gt;$notify_url</span> = '';<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;回调地址</span>

            <span style="color: #800080"&gt;$order</span> = <span style="color: #800080"&gt;$wx</span>->getPrePayOrder(<span style="color: #800080"&gt;$body</span>,<span style="color: #800080"&gt;$out_trade_no</span>,<span style="color: #800080"&gt;$total_fee</span>,<span style="color: #800080"&gt;$notify_url</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>微信支付的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>

            <span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$order</span>['prepay_id']){<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;判断返回参数中是否有prepay_id</span>

                <span style="color: #800080"&gt;$order1</span> = <span style="color: #800080"&gt;$wx</span>->getOrder(<span style="color: #800080"&gt;$order</span>['prepay_id']);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;执行二次签名返回参数</span>

                <span style="color: #0000ff"&gt;echo</span> json_encode(<span style="color: #0000ff"&gt;array</span>('status' => 1,'prepay_order' => no_null(<span style="color: #800080"&gt;$order1</span><span style="color: #000000"&gt;)));
            } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
                </span><span style="color: #0000ff"&gt;echo</span> json_encode(<span style="color: #0000ff"&gt;array</span>('status' => 0,'msg' => <span style="color: #800080"&gt;$order</span>['err_code_des'<span style="color: #000000"&gt;]));
            }
            </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
    }
}

}

然后创建微信支付控制器了。命名为wx.PHP,用于执行微信支付,代码如下:

PHP namespace app\mobile\controller; WxPay = 'appid' => "", 'mch_id' => "",40493402",/*微信申请成功之后邮件中的商户id*/ 'api_key' => "" </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>预支付订单</span> <span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> getPrePayOrder(<span style="color: #800080"&gt;$body</span>,<span style="color: #800080"&gt;$notify_url</span><span style="color: #000000"&gt;){ </span><span style="color: #800080"&gt;$url</span> = "https://api.mch.weixin.qq.com/pay/unifiedorder"<span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$onoce_str</span> = <span style="color: #800080"&gt;$this</span>->getRandChar(32<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$data</span>["appid"] = <span style="color: #800080"&gt;$this</span>->config["appid"<span style="color: #000000"&gt;]; </span><span style="color: #800080"&gt;$data</span>["body"] = <span style="color: #800080"&gt;$body</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$data</span>["mch_id"] = <span style="color: #800080"&gt;$this</span>->config['mch_id'<span style="color: #000000"&gt;]; </span><span style="color: #800080"&gt;$data</span>["nonce_str"] = <span style="color: #800080"&gt;$onoce_str</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$data</span>["notify_url"] = <span style="color: #800080"&gt;$notify_url</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$data</span>["out_trade_no"] = <span style="color: #800080"&gt;$out_trade_no</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$data</span>["spbill_create_ip"] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;get_client_ip(); </span><span style="color: #800080"&gt;$data</span>["total_fee"] = <span style="color: #800080"&gt;$total_fee</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$data</span>["trade_type"] = "APP"<span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$s</span> = <span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$data</span>,<span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$data</span>["sign"] = <span style="color: #800080"&gt;$s</span><span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$xml</span> = <span style="color: #800080"&gt;$this</span>->arrayToXml(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$response</span> = <span style="color: #800080"&gt;$this</span>->postXmlCurl(<span style="color: #800080"&gt;$xml</span>,<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将微信返回的结果xml转成数组

// return $this->xmlstr_to_array($response);
<span style="color: #0000ff">return xmlToArray(<span style="color: #800080">$response<span style="color: #000000">);
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;执行第二次签名,才能返回给客户端使用</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> getOrder(<span style="color: #800080"&gt;$prepayId</span><span style="color: #000000"&gt;){
    </span><span style="color: #800080"&gt;$data</span>["appid"] = <span style="color: #800080"&gt;$this</span>->config["appid"<span style="color: #000000"&gt;];
    </span><span style="color: #800080"&gt;$data</span>["noncestr"] = <span style="color: #800080"&gt;$this</span>->getRandChar(32<span style="color: #000000"&gt;);;
    </span><span style="color: #800080"&gt;$data</span>["package"] = "Sign=WXPay"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$data</span>["partnerid"] = <span style="color: #800080"&gt;$this</span>->config['mch_id'<span style="color: #000000"&gt;];
    </span><span style="color: #800080"&gt;$data</span>["prepayid"] = <span style="color: #800080"&gt;$prepayId</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$data</span>["timestamp"] = <span style="color: #008080"&gt;time</span><span style="color: #000000"&gt;();
    </span><span style="color: #800080"&gt;$s</span> = <span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$data</span>,<span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span>["sign"] = <span style="color: #800080"&gt;$s</span><span style="color: #000000"&gt;;

    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
    <a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>签名
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;function</span> getSign(<span style="color: #800080"&gt;$Obj</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #0000ff"&gt;foreach</span> (<span style="color: #800080"&gt;$Obj</span> <span style="color: #0000ff"&gt;as</span> <span style="color: #800080"&gt;$k</span> => <span style="color: #800080"&gt;$v</span><span style="color: #000000"&gt;)
    {
        </span><span style="color: #800080"&gt;$Parameters</span>[<span style="color: #008080"&gt;strtolower</span>(<span style="color: #800080"&gt;$k</span>)] = <span style="color: #800080"&gt;$v</span><span style="color: #000000"&gt;;
    }
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;签名步骤一:按字典序排序参数</span>
    <span style="color: #008080"&gt;ksort</span>(<span style="color: #800080"&gt;$Parameters</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$String</span> = <span style="color: #800080"&gt;$this</span>->formatBizQueryParaMap(<span style="color: #800080"&gt;$Parameters</span>,<span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;echo "【string】 =".$String."@H_<a href="https://www.jb51.cc/tag/502/" target="_blank" class="keywords">502</a>_263@";
    //签名步骤二:在string后加入KEY</span>
    <span style="color: #800080"&gt;$String</span> = <span style="color: #800080"&gt;$String</span>."&amp;key=".<span style="color: #800080"&gt;$this</span>->config['api_key'<span style="color: #000000"&gt;];

<span style="color: #008000">//<span style="color: #008000"> echo "
";
//签名步骤三:MD5加密
<span style="color: #800080">$result = <span style="color: #008080">strtoupper(<span style="color: #008080">md5(<span style="color: #800080">$String<span style="color: #000000">));
<span style="color: #0000ff">return <span style="color: #800080">$result
<span style="color: #000000">;
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>指定长度的<a href="https://www.jb51.cc/tag/suiji/" target="_blank" class="keywords">随机</a>字符串</span>
<span style="color: #0000ff"&gt;function</span> getRandChar(<span style="color: #800080"&gt;$length</span><span style="color: #000000"&gt;){
    </span><span style="color: #800080"&gt;$str</span> = <span style="color: #0000ff"&gt;null</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$strPol</span> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$max</span> = <span style="color: #008080"&gt;strlen</span>(<span style="color: #800080"&gt;$strPol</span>)-1<span style="color: #000000"&gt;;

    </span><span style="color: #0000ff"&gt;for</span>(<span style="color: #800080"&gt;$i</span>=0;<span style="color: #800080"&gt;$i</span><<span style="color: #800080"&gt;$length</span>;<span style="color: #800080"&gt;$i</span>++<span style="color: #000000"&gt;){
        </span><span style="color: #800080"&gt;$str</span>.=<span style="color: #800080"&gt;$strPol</span>[<span style="color: #008080"&gt;rand</span>(0,<span style="color: #800080"&gt;$max</span>)];<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;rand($min,$max)<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>介于min和max两个数之间的一个<a href="https://www.jb51.cc/tag/suiji/" target="_blank" class="keywords">随机</a>整数</span>

<span style="color: #000000"> }

    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$str</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;数组转xml</span>
<span style="color: #0000ff"&gt;function</span> arrayToXml(<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$xml</span> = "<xml>"<span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;foreach</span> (<span style="color: #800080"&gt;$arr</span> <span style="color: #0000ff"&gt;as</span> <span style="color: #800080"&gt;$key</span>=><span style="color: #800080"&gt;$val</span><span style="color: #000000"&gt;)
    {
        </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #008080"&gt;is_numeric</span>(<span style="color: #800080"&gt;$val</span><span style="color: #000000"&gt;))
        {
            </span><span style="color: #800080"&gt;$xml</span>.="<".<span style="color: #800080"&gt;$key</span>."&gt;".<span style="color: #800080"&gt;$val</span>."</".<span style="color: #800080"&gt;$key</span>."&gt;"<span style="color: #000000"&gt;;

        }
        </span><span style="color: #0000ff"&gt;else</span>
            <span style="color: #800080"&gt;$xml</span>.="<".<span style="color: #800080"&gt;$key</span>."&gt;<![CDATA[".<span style="color: #800080"&gt;$val</span>."]]></".<span style="color: #800080"&gt;$key</span>."&gt;"<span style="color: #000000"&gt;;
    }
    </span><span style="color: #800080"&gt;$xml</span>.="</xml>"<span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$xml</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;post https请求,CURLOPT_POSTFIELDS xml格式</span>
<span style="color: #0000ff"&gt;function</span> postXmlCurl(<span style="color: #800080"&gt;$xml</span>,<span style="color: #800080"&gt;$url</span>,<span style="color: #800080"&gt;$second</span>=30<span style="color: #000000"&gt;)
{
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;初始化curl</span>
    <span style="color: #800080"&gt;$ch</span> =<span style="color: #000000"&gt; curl_init();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;超时时间</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,CURLOPT_TIMEOUT,<span style="color: #800080"&gt;$second</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;这里设置代理,如果有的话
    //curl_setopt($ch,CURLOPT_PROXY,'8.8.8.8');
    //curl_setopt($ch,CURLOPT_PROXYPORT,8080);</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,CURLOPT_URL,<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSL_VERIFYPEER,<span style="color: #0000ff"&gt;FALSE</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSL_VERIFYHOST,<span style="color: #0000ff"&gt;FALSE</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;设置header</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,CURLOPT_HEADER,<span style="color: #0000ff"&gt;FALSE</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;要求结果为字符串且<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>到屏幕上</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,CURLOPT_RETURNTRANSFER,<span style="color: #0000ff"&gt;TRUE</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;post提交方式</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,CURLOPT_POST,<span style="color: #0000ff"&gt;TRUE</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080"&gt;$xml</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;运行curl</span>
    <span style="color: #800080"&gt;$data</span> = curl_exec(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;返回结果</span>
    <span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;)
    {
        curl_close(</span><span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;;
    }
    </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;
    {
        </span><span style="color: #800080"&gt;$error</span> = curl_errno(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;echo</span> "curl出错,<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>码:<span style="color: #800080"&gt;$error</span>"."<br>"<span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;echo</span> "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a>@H_<a href="https://www.jb51.cc/tag/502/" target="_blank" class="keywords">502</a>_263@"<span style="color: #000000"&gt;;
        curl_close(</span><span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;;
    }
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
    <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>当前服务器的IP
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; get_client_ip()
{
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000"&gt;]) {
        </span><span style="color: #800080"&gt;$cip</span> = <span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000"&gt;];
    } </span><span style="color: #0000ff"&gt;elseif</span> (<span style="color: #008080"&gt;getenv</span>("REMOTE_ADDR"<span style="color: #000000"&gt;)) {
        </span><span style="color: #800080"&gt;$cip</span> = <span style="color: #008080"&gt;getenv</span>("REMOTE_ADDR"<span style="color: #000000"&gt;);
    } </span><span style="color: #0000ff"&gt;elseif</span> (<span style="color: #008080"&gt;getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000"&gt;)) {
        </span><span style="color: #800080"&gt;$cip</span> = <span style="color: #008080"&gt;getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000"&gt;);
    } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
        </span><span style="color: #800080"&gt;$cip</span> = "unknown"<span style="color: #000000"&gt;;
    }
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$cip</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将数组转成uri字符串</span>
<span style="color: #0000ff"&gt;function</span> formatBizQueryParaMap(<span style="color: #800080"&gt;$paraMap</span>,<span style="color: #800080"&gt;$urlencode</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$buff</span> = ""<span style="color: #000000"&gt;;
    </span><span style="color: #008080"&gt;ksort</span>(<span style="color: #800080"&gt;$paraMap</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;foreach</span> (<span style="color: #800080"&gt;$paraMap</span> <span style="color: #0000ff"&gt;as</span> <span style="color: #800080"&gt;$k</span> => <span style="color: #800080"&gt;$v</span><span style="color: #000000"&gt;)
    {
        </span><span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$urlencode</span><span style="color: #000000"&gt;)
        {
            </span><span style="color: #800080"&gt;$v</span> = <span style="color: #008080"&gt;urlencode</span>(<span style="color: #800080"&gt;$v</span><span style="color: #000000"&gt;);
        }
        </span><span style="color: #800080"&gt;$buff</span> .= <span style="color: #008080"&gt;strtolower</span>(<span style="color: #800080"&gt;$k</span>) . "=" . <span style="color: #800080"&gt;$v</span> . "&amp;"<span style="color: #000000"&gt;;
    }
    </span><span style="color: #800080"&gt;$reqPar</span><span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #008080"&gt;strlen</span>(<span style="color: #800080"&gt;$buff</span>) > 0<span style="color: #000000"&gt;)
    {
        </span><span style="color: #800080"&gt;$reqPar</span> = <span style="color: #008080"&gt;substr</span>(<span style="color: #800080"&gt;$buff</span>,<span style="color: #008080"&gt;strlen</span>(<span style="color: #800080"&gt;$buff</span>)-1<span style="color: #000000"&gt;);
    }
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$reqPar</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;*
xml转成数组
 </span><span style="color: #008000"&gt;*/</span>

<span style="color: #008000">//@H639502@loadXML($xmlstr);
// return $this->domnode_to_array($doc->documentElement);
// }
<span style="color: #0000ff">function domnode_to_array(<span style="color: #800080">$node<span style="color: #000000">) {
<span style="color: #800080">$output = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">switch (<span style="color: #800080">$node-><span style="color: #000000">nodeType) {
<span style="color: #0000ff">case XML_CDATA_SECTION_NODE:
<span style="color: #0000ff">case XML_TEXT_NODE:
<span style="color: #800080">$output = <span style="color: #008080">trim(<span style="color: #800080">$node-><span style="color: #000000">textContent);
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case XML_ELEMENT_NODE:
<span style="color: #0000ff">for (<span style="color: #800080">$i=0,<span style="color: #800080">$m=<span style="color: #800080">$node->childNodes->length; <span style="color: #800080">$i<<span style="color: #800080">$m; <span style="color: #800080">$i++<span style="color: #000000">) {
<span style="color: #800080">$child = <span style="color: #800080">$node->childNodes->item(<span style="color: #800080">$i<span style="color: #000000">);
<span style="color: #800080">$v = <span style="color: #800080">$this->domnode_to_array(<span style="color: #800080">$child<span style="color: #000000">);
<span style="color: #0000ff">if(<span style="color: #0000ff">isset(<span style="color: #800080">$child-><span style="color: #000000">tagName)) {
<span style="color: #800080">$t = <span style="color: #800080">$child-><span style="color: #000000">tagName;
<span style="color: #0000ff">if(!<span style="color: #0000ff">isset(<span style="color: #800080">$output[<span style="color: #800080">$t<span style="color: #000000">])) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #0000ff">array<span style="color: #000000">();
}
<span style="color: #800080">$output[<span style="color: #800080">$t][] = <span style="color: #800080">$v<span style="color: #000000">;
}
<span style="color: #0000ff">elseif(<span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #800080">$output = (<span style="color: #0000ff">string) <span style="color: #800080">$v<span style="color: #000000">;
}
}
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$output<span style="color: #000000">)) {
<span style="color: #0000ff">if(<span style="color: #800080">$node->attributes-><span style="color: #000000">length) {
<span style="color: #800080">$a = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">foreach(<span style="color: #800080">$node->attributes <span style="color: #0000ff">as <span style="color: #800080">$attrName => <span style="color: #800080">$attrNode<span style="color: #000000">) {
<span style="color: #800080">$a[<span style="color: #800080">$attrName] = (<span style="color: #0000ff">string) <span style="color: #800080">$attrNode-><span style="color: #000000">value;
}
<span style="color: #800080">$output['@attributes'] = <span style="color: #800080">$a<span style="color: #000000">;
}
<span style="color: #0000ff">foreach (<span style="color: #800080">$output <span style="color: #0000ff">as <span style="color: #800080">$t => <span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$v) && <span style="color: #008080">count(<span style="color: #800080">$v)==1 && <span style="color: #800080">$t!='@attributes'<span style="color: #000000">) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #800080">$v[0<span style="color: #000000">];
}
}
}
<span style="color: #0000ff">break<span style="color: #000000">;
}
<span style="color: #0000ff">return <span style="color: #800080">$output<span style="color: #000000">;
}
}

微信支付控制器里面基本不用动,只需要改几个参数就行了。很简单。

回调控制器和方法我这里就不写了,很简单的,不懂得可以私信。

流程已经没了,执行一下程序,看一下返回给前端的参数示例:

把这些参数通过和前端交互给到前端就行了,接下来就是前端拿着参数去进行支付,支付成功系统会自动调用你写的回调地址改写你所需要的代码逻辑。

 至于微信控制器wx.PHP存放的位置参考之前的那个app支付宝支付文章就行了,一样的。希望以上说明对需要做这个的有所帮助。

内容,为了尊重他人劳动,转载请注明本文地址:

原文链接:https://www.f2er.com/thinkphp/71862.html

猜你在找的ThinkPHP相关文章