ThinkPHP 5 整合支付宝微信支付(支付宝H5,微信H5、APP支付、公众号支付)

前端之家收集整理的这篇文章主要介绍了ThinkPHP 5 整合支付宝微信支付(支付宝H5,微信H5、APP支付、公众号支付)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

 Pay.PHP支付控制器

PHP // +---------------------------------------------------------------------- <span style="color: #0000ff">use<span style="color: #000000"> think\Db;
<span style="color: #0000ff">use<span style="color: #000000"> think\Request;
<span style="color: #0000ff">use<span style="color: #000000"> think\Session;
<span style="color: #0000ff">use<span style="color: #000000"> think\Cookie;
<span style="color: #0000ff">use<span style="color: #000000"> app\phone\model\<span style="color: #ff0000">Weixin;
<span style="color: #0000ff">use<span style="color: #000000"> app\phone\model\<span style="color: #ff0000">Weixin_pay;
<span style="color: #0000ff">use<span style="color: #000000"> app\phone\model\<span style="color: #ff0000">Alipay;

<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"> _initialize()
{
<span style="color: #800080">$this->alipay_config = <span style="color: #0000ff">array<span style="color: #000000">(
<span style="color: #008000">//<span style="color: #008000">应用ID,您的APPID。
'app_id' => '<span style="color: #ff0000">your app_id',<span style="color: #008000">//<span style="color: #008000">支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
'alipay_public_key' => '<span style="color: #ff0000">your alipay_public_key',<span style="color: #008000">//<span style="color: #008000">商户私钥,您的原始格式RSA私钥
'merchant_private_key' => '<span style="color: #ff0000">your merchant_private_key',<span style="color: #008000">//<span style="color: #008000">异步通知地址
'notify_url' => "<span style="color: #ff0000">your notify_url",<span style="color: #008000">//<span style="color: #008000">同步跳转
'return_url' => "<span style="color: #ff0000">your return_url",<span style="color: #008000">//<span style="color: #008000">编码格式
'charset' => "UTF-8",<span style="color: #008000">//<span style="color: #008000">签名方式
'sign_type' => "RSA2",<span style="color: #008000">//<span style="color: #008000">支付宝网关
'gatewayUrl' => "https://openapi.alipay.com/gateway.do",<span style="color: #008000">//<span style="color: #008000">支付宝partner,2088开头数字
'partner' => '<span style="color: #ff0000">your partner',<span style="color: #008000">//<span style="color: #008000">支付宝密钥
'md5_key' => '<span style="color: #ff0000">your key'<span style="color: #000000">
);
<span style="color: #800080">$this->wechat_config = <span style="color: #0000ff">array<span style="color: #000000">(
<span style="color: #008000">//<span style="color: #008000">您的APPID
'app_id' => '<span style="color: #ff0000">your app_id',<span style="color: #008000">//<span style="color: #008000">您的md5_key
'md5_key' => '<span style="color: #ff0000">your md5_key',<span style="color: #008000">//<span style="color: #008000">您的mch_id
'mch_id' => '<span style="color: #ff0000">your mch_id',<span style="color: #008000">//<span style="color: #008000">您的appsecret
'appsecret' => '<span style="color: #ff0000">your appsecret',<span style="color: #008000">//<span style="color: #008000">您的回调地址可以在这里配置使用方法的时候传,也可以在模型里面配置。
'notify_url' => '<span style="color: #ff0000">your notify_url',<span style="color: #000000">
);
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
微信h5支付
 </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; wechat_pay()
{
    </span><span style="color: #0000ff"&gt;if</span> (input('post.token') ==<span style="color: #000000"&gt; TOKEN) {
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 判断<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>是在微信浏览器打开还是其他浏览器打开。</span>
        <span style="color: #800080"&gt;$userAgent</span> = <span style="color: #800080"&gt;$_SERVER</span>['HTTP_USER_AGENT'<span style="color: #000000"&gt;];
        </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #008080"&gt;strpos</span>(<span style="color: #800080"&gt;$userAgent</span>,'MicroMessenger'<span style="color: #000000"&gt;)) {
            </span><span style="color: #800080"&gt;$wechat</span> = <span style="color: #0000ff"&gt;new</span> Weixin(<span style="color: #800080"&gt;$this</span>->wechat_config['appsecret'],<span style="color: #800080"&gt;$this</span>->wechat_config['app_id'<span style="color: #000000"&gt;]);
            </span><span style="color: #800080"&gt;$wechat_pay</span> = <span style="color: #0000ff"&gt;new</span> Weixin_pay(<span style="color: #800080"&gt;$this</span>->wechat_config['appsecret'],<span style="color: #800080"&gt;$this</span>->wechat_config['app_id'],<span style="color: #800080"&gt;$this</span>->wechat_config['md5_key'],<span style="color: #800080"&gt;$this</span>->wechat_config['mch_id'<span style="color: #000000"&gt;]);
            </span><span style="color: #800080"&gt;$code</span> = input('post.code'<span style="color: #000000"&gt;);
            </span><span style="color: #0000ff"&gt;if</span> (session::has('openid'<span style="color: #000000"&gt;)) {
                </span><span style="color: #800080"&gt;$openid</span> = session::get('openid'<span style="color: #000000"&gt;);
                </span><span style="color: #800080"&gt;$total_fee</span> = 1<span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$out_trade_no</span> = '12345678945641231'<span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$body</span> = "充值金币支付测试"<span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat_pay</span>->pay(<span style="color: #800080"&gt;$openid</span>,<span style="color: #800080"&gt;$total_fee</span>,<span style="color: #800080"&gt;$body</span>,<span style="color: #800080"&gt;$out_trade_no</span><span style="color: #000000"&gt;);
                </span><span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$arr</span>['url'] = '本控制器下的地址/' . 'wx?data=' . <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
                </span><span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</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;if</span> (!<span style="color: #0000ff"&gt;empty</span>(<span style="color: #800080"&gt;$code</span><span style="color: #000000"&gt;)) {
                    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat</span>->get_access_token(<span style="color: #800080"&gt;$code</span><span style="color: #000000"&gt;);
                    </span><span style="color: #800080"&gt;$userinfo</span> = <span style="color: #800080"&gt;$wechat</span>->get_openid_userinfo(<span style="color: #800080"&gt;$res</span>['access_token'],<span style="color: #800080"&gt;$res</span>['openid'<span style="color: #000000"&gt;]);
                    </span><span style="color: #800080"&gt;$openid</span> = <span style="color: #800080"&gt;$userinfo</span>['openid'<span style="color: #000000"&gt;];
                    session</span>::set('openid',<span style="color: #800080"&gt;$openid</span><span style="color: #000000"&gt;);
                    </span><span style="color: #800080"&gt;$total_fee</span> = 1<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$out_trade_no</span> = '12345678945641231'<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$body</span> = "充值金币支付测试"<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat_pay</span>->pay(<span style="color: #800080"&gt;$openid</span>,<span style="color: #800080"&gt;$out_trade_no</span><span style="color: #000000"&gt;);
                    </span><span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$arr</span>['url'] = '本控制器下的地址/' . 'wx?data=' . <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</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;$link</span> = 'http://' . <span style="color: #800080"&gt;$_SERVER</span>['HTTP_HOST'] . '/#/cz'; <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 前端充值金币<a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>地址。</span>
                    cookie::set('url',<span style="color: #800080"&gt;$link</span>,300<span style="color: #000000"&gt;);
                    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;$link 这个地址是在微信打开你项目时才会需要的,地址后面带上<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的code进行<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token。</span>
                    <span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat</span>->code_shouquan(<span style="color: #800080"&gt;$link</span><span style="color: #000000"&gt;);
                    </span><span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$arr</span>['url'] = <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
                    </span><span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;);
                }
            }
        } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
            </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;非微信浏览器</span>
            <span style="color: #800080"&gt;$wechat_pay</span> = <span style="color: #0000ff"&gt;new</span> Weixin_pay(<span style="color: #800080"&gt;$this</span>->wechat_config['appsecret'],<span style="color: #800080"&gt;$this</span>->wechat_config['mch_id'<span style="color: #000000"&gt;]);
            </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;h5支付不用传递openid 此处与微信jsapi支付不同</span>
            <span style="color: #800080"&gt;$openid</span> = ""<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$total_fee</span> = 1<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$out_trade_no</span> = '12345678945641231'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$body</span> = "充值金币h5支付测试"<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat_pay</span>->payh5(<span style="color: #800080"&gt;$openid</span>,<span style="color: #800080"&gt;$out_trade_no</span><span style="color: #000000"&gt;);
            </span><span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$arr</span>['url'] = <span style="color: #800080"&gt;$res</span>['mweb_url'];<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;微信唤起H5支付地址</span>
            <span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</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;$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000"&gt;;
    }
    </span><span style="color: #0000ff"&gt;echo</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/gongzhonghaozhifu/" target="_blank" class="keywords">公众号支付</a>。
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; wx()
{
    </span><span style="color: #800080"&gt;$data</span> = input('get.data'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$this</span>->assign('data',<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; view();
}

</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>微信code
 </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; code()
{
    </span><span style="color: #800080"&gt;$code</span> = input('param.code'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$this</span>->redirect(cookie::get('url') . '?code=' . <span style="color: #800080"&gt;$code</span><span style="color: #000000"&gt;);
}

</span><span style="color: #008000"&gt;/*</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><span style="color: #000000"&gt; wechat_notify()
{
    </span><span style="color: #800080"&gt;$ccc</span> = <span style="color: #008080"&gt;file_get_contents</span>('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://input'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$Weixin_pay</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; Weixin_pay();
    </span><span style="color: #800080"&gt;$data</span> = <span style="color: #800080"&gt;$Weixin_pay</span>->xmlToArray(<span style="color: #800080"&gt;$ccc</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$data</span>['result_code'] == 'SUCCESS' &amp;&amp; <span style="color: #800080"&gt;$data</span>['return_code'] == 'SUCCESS'<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>订单号</span>
        <span style="color: #800080"&gt;$Ordersn</span> = <span style="color: #800080"&gt;$data</span>['out_trade_no'];<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;订单号
        // 处理支付成功后的逻辑业务</span>

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

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
微信APP支付
 </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; app_wechat_pay()
{
    </span><span style="color: #0000ff"&gt;if</span> (input('post.token') ==<span style="color: #000000"&gt; TOKEN) {
        </span><span style="color: #800080"&gt;$wechat_pay</span> = <span style="color: #0000ff"&gt;new</span> Weixin_pay(<span style="color: #800080"&gt;$this</span>->wechat_config['appsecret'],<span style="color: #800080"&gt;$this</span>->wechat_config['mch_id'<span style="color: #000000"&gt;]);
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;h5支付不用传递openid 此处与微信jsapi支付不同</span>
        <span style="color: #800080"&gt;$openid</span> = ""<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$total_fee</span> = 1<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$body</span> = "充值金币APP支付测试"<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$order_sn</span> = 123456789;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;订单号</span>
        <span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$wechat_pay</span>->app_pay(<span style="color: #800080"&gt;$body</span>,<span style="color: #800080"&gt;$order_sn</span>,<span style="color: #800080"&gt;$total_fee</span><span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$res</span>['prepay_id']) {<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;判断返回参数中是否有prepay_id</span>
            <span style="color: #800080"&gt;$order</span> = <span style="color: #800080"&gt;$wechat_pay</span>->getOrder(<span style="color: #800080"&gt;$res</span>['prepay_id']);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;执行二次签名返回参数</span>
            <span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$arr</span>['order'] = <span style="color: #800080"&gt;$order</span><span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</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;$arr</span>['errcode'] = '1003'<span style="color: #000000"&gt;;
            </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = <span style="color: #800080"&gt;$res</span>['err_code_des'<span style="color: #000000"&gt;];
            </span><span style="color: #800080"&gt;$data</span> = json_encode(<span style="color: #800080"&gt;$arr</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;$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000"&gt;;
    }
    </span><span style="color: #0000ff"&gt;echo</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;
支付宝开始支付
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; alipay()
{</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;发起支付宝支付</span>
    <span style="color: #0000ff"&gt;if</span> (input('post.token') ==<span style="color: #000000"&gt; TOKEN) {
        </span><span style="color: #800080"&gt;$link</span> = 'http://' . <span style="color: #800080"&gt;$_SERVER</span>['HTTP_HOST'] . '/#/cz'<span style="color: #000000"&gt;;
        cookie</span>::set('url',300<span style="color: #000000"&gt;);
        </span><span style="color: #800080"&gt;$res</span>['out_trade_no'] = 123456798123<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$res</span>['subject'] = '充值'<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$res</span>['body'] = '测试'<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$res</span>['money'] = 0.01<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$Pay</span> = <span style="color: #0000ff"&gt;new</span> Alipay(<span style="color: #800080"&gt;$this</span>->alipay_config['partner'],<span style="color: #800080"&gt;$this</span>->alipay_config['md5_key'<span style="color: #000000"&gt;]);
        </span><span style="color: #800080"&gt;$result</span> = <span style="color: #800080"&gt;$Pay</span>-><span style="color: #000000"&gt;alipay([
            </span>'notify_url' => 'your notify_url','return_url' => 'your return_url','out_trade_no' => <span style="color: #800080"&gt;$res</span>['out_trade_no'],'subject' => <span style="color: #800080"&gt;$res</span>['subject'],'total_fee' => <span style="color: #800080"&gt;$res</span>['money'],'body' => <span style="color: #800080"&gt;$res</span>['body'],<span style="color: #000000"&gt;
        ]);
        </span><span style="color: #800080"&gt;$arr</span>['errcode'] = '0'<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$arr</span>['errmsg'] = 'ok'<span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$result</span>['msg'<span style="color: #000000"&gt;];
    } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
        </span><span style="color: #800080"&gt;$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000"&gt;;
    }
    </span><span style="color: #0000ff"&gt;echo</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;
支付宝回调
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; alipay_notify()
{</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;异步订单<a href="https://www.jb51.cc/tag/tongzhi/" target="_blank" class="keywords">通知</a></span>
    <span style="color: #800080"&gt;$wx</span> = sys_config_get('payment'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$Pay</span> = <span style="color: #0000ff"&gt;new</span> Alipay(<span style="color: #800080"&gt;$wx</span>['aliwappay']['partner'],<span style="color: #800080"&gt;$wx</span>['aliwappay']['md5_key'<span style="color: #000000"&gt;]);
    </span><span style="color: #800080"&gt;$out_trade_no</span> = input('out_trade_no'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$trade_status</span> = input('trade_status'<span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$trade_status</span> == 'TRADE_SUCCESS'<span style="color: #000000"&gt;) {
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 处理支付成功后的逻辑业务</span>

<span style="color: #000000">
} <span style="color: #0000ff">else<span style="color: #000000"> {
<span style="color: #008000">//<span style="color: #008000"> 处理支付失败后的逻辑业务
<span style="color: #000000"> }
}
}

模型:Weixin.PHP

PHP

namespace app\phone\model;

<span style="color: #0000ff">use<span style="color: #000000"> think\Db;
<span style="color: #0000ff">use<span style="color: #000000"> think\Validate;
<span style="color: #0000ff">use<span style="color: #000000"> think\Loader;
<span style="color: #0000ff">use<span style="color: #000000"> think\Model;

<span style="color: #0000ff">class Weixin <span style="color: #0000ff">extends<span style="color: #000000"> model
{
<span style="color: #0000ff">protected <span style="color: #800080">$appScrect<span style="color: #000000">;
<span style="color: #0000ff">protected <span style="color: #800080">$appId<span style="color: #000000">;

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> __construct(<span style="color: #800080"&gt;$appScrect</span> = "",<span style="color: #800080"&gt;$appId</span> = ""<span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$this</span>->appScrect = <span style="color: #800080"&gt;$appScrect</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$this</span>->appId = <span style="color: #800080"&gt;$appId</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
 * 因为本项目需求,前端是Vue<a href="https://www.jb51.cc/tag/houtai/" target="_blank" class="keywords">后台</a>是TP5两个项目不在一个服务器所以我的url是return 回控制器通过接口返回给前端
 * 如果是正常html<a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>可以使用header("Location: $url"); 以下<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>都是
 * </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> code_shouquan(<span style="color: #800080"&gt;$link</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$redirect_uri</span> = <span style="color: #008080"&gt;urlencode</span>('网址/pay/code');<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;微信<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>网页授权地址
    // 1、引导<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进入授权<a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>同意授权,<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>code
    // 2、通过code换取网页授权access_token
    // 3、如果需要,开发者可以刷新网页授权access_token,避免过期
    // 4、通过网页授权access_token和openid<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>基本信息(<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>UnionID机制)</span>
    <span style="color: #800080"&gt;$url</span> = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . <span style="color: #800080"&gt;$this</span>->appId . "&amp;redirect_uri=" . <span style="color: #800080"&gt;$redirect_uri</span> . "&amp;response_type=code&amp;scope=snsapi_userinfo&amp;state=1234#wechat_redirect"<span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;;
}


</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> get_access_token(<span style="color: #800080"&gt;$code</span><span style="color: #000000"&gt;)
{

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;检测缓存中是否有access_token(2小时),如果存在直接返回,不存在则检测缓存中的refresh_token(30天),// refresh_token如果存在<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>刷新缓存;如果不存在重新发起授权code授权</span>
    <span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . <span style="color: #800080"&gt;$this</span>->appId . "&amp;secret=" . <span style="color: #800080"&gt;$this</span>->appScrect . "&amp;code=" . <span style="color: #800080"&gt;$code</span> . "&amp;grant_type=authorization_code"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$this</span>->sendRequest(<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> get_refresh_token(<span style="color: #800080"&gt;$refresh_token</span><span style="color: #000000"&gt;)
{

    </span><span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" . <span style="color: #800080"&gt;$this</span>->appId . "&amp;grant_type=refresh_token&amp;refresh_token=" . <span style="color: #800080"&gt;$refresh_token</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$this</span>->sendRequest(<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> get_openid_userinfo(<span style="color: #800080"&gt;$access_token</span>,<span style="color: #800080"&gt;$openid</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/sns/userinfo?access_token=" . <span style="color: #800080"&gt;$access_token</span> . "&amp;openid=" . <span style="color: #800080"&gt;$openid</span> . "&amp;lang=zh_CN"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$this</span>->sendRequest(<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;;
}

</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;

发送请求
<span style="color: #008000">*/
<span style="color: #0000ff">public <span style="color: #0000ff">function sendRequest(<span style="color: #800080">$url<span style="color: #000000">)
{
<span style="color: #800080">$ch =<span style="color: #000000"> curl_init();
curl_setopt(<span style="color: #800080">$ch,CURLOPT_URL,<span style="color: #800080">$url<span style="color: #000000">);
curl_setopt(<span style="color: #800080">$ch,CURLOPT_SSL_VERIFYPEER,<span style="color: #0000ff">FALSE<span style="color: #000000">);
curl_setopt(<span style="color: #800080">$ch,CURLOPT_SSL_VERIFYHOST,CURLOPT_RETURNTRANSFER,1<span style="color: #000000">);
<span style="color: #800080">$output = curl_exec(<span style="color: #800080">$ch<span style="color: #000000">);
curl_close(<span style="color: #800080">$ch<span style="color: #000000">);
<span style="color: #0000ff">return json_decode(<span style="color: #800080">$output,<span style="color: #0000ff">true<span style="color: #000000">);
}
}

Weixin.PHP

PHP namespace app\phone\model; <span style="color: #0000ff">class Weixin_pay <span style="color: #0000ff">extends<span style="color: #000000"> model
{
<span style="color: #0000ff">protected
<span style="color: #800080">$appScrect
;<span style="color: #008000">//
<span style="color: #008000">微信公众平台的appscrect

<span style="color: #0000ff">protected <span style="color: #800080">$appId;<span style="color: #008000">//<span style="color: #008000">微信公众平台appid
<span style="color: #0000ff">protected <span style="color: #800080">$key;<span style="color: #008000">//<span style="color: #008000">微信商户平台配置的秘钥
<span style="color: #0000ff">protected <span style="color: #800080">$mch_id;<span style="color: #008000">//<span style="color: #008000">微信商户号
<span style="color: #0000ff">protected <span style="color: #800080">$values = <span style="color: #0000ff">array<span style="color: #000000">();

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> __construct(<span style="color: #800080"&gt;$appScrect</span>="",<span style="color: #800080"&gt;$appId</span>="",<span style="color: #800080"&gt;$key</span>="",<span style="color: #800080"&gt;$mch_id</span> =''<span style="color: #000000"&gt;){
    </span><span style="color: #800080"&gt;$this</span>->appScrect=<span style="color: #800080"&gt;$appScrect</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$this</span>->appId=<span style="color: #800080"&gt;$appId</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$this</span>-><span style="color: #008080"&gt;key</span>=<span style="color: #800080"&gt;$key</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$this</span>->mch_id=<span style="color: #800080"&gt;$mch_id</span><span style="color: #000000"&gt;;

}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> app_pay(<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: #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;$notify_url</span> = '';<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;可以在这填写回调地址也可以<a href="https://www.jb51.cc/tag/zengjia/" target="_blank" class="keywords">增加</a>一个参数传过来。也可以直接在构造里面加一个notify_<a href="https://www.jb51.cc/tag/URLdizhi/" target="_blank" class="keywords">URL地址</a></span>
    <span style="color: #800080"&gt;$onoce_str</span> = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;createNoncestr();
    </span><span style="color: #800080"&gt;$data</span>["appid"] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;appId;
    </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>-><span style="color: #000000"&gt;mch_id;
    </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;$sign</span> = <span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span>["sign"] = <span style="color: #800080"&gt;$sign</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; return $xml;
    //将微信返回的结果xml转成数组</span>
    <span style="color: #800080"&gt;$response</span> = <span style="color: #800080"&gt;$this</span>->xmlToArray(<span style="color: #800080"&gt;$response</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;返回数据</span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$response</span><span style="color: #000000"&gt;;
}
</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>-><span style="color: #000000"&gt;appId;
    </span><span style="color: #800080"&gt;$data</span>["noncestr"] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;createNoncestr();;
    </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>-><span style="color: #000000"&gt;mch_id;
    </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/gongzhonghaozhifu/" target="_blank" class="keywords">公众号支付</a>
 </span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> pay(<span style="color: #800080"&gt;$openid</span>,<span style="color: #800080"&gt;$out_trade_no</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;$notify_url</span> = ''<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$onoce_str</span> = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;createNoncestr();
    </span><span style="color: #800080"&gt;$data</span>["appid"] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;appId;
    </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>-><span style="color: #000000"&gt;mch_id;
    </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"] = "JSAPI"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$data</span>["openid"] = <span style="color: #800080"&gt;$openid</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$sign</span> = <span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span>["sign"] = <span style="color: #800080"&gt;$sign</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转成数组</span>
    <span style="color: #800080"&gt;$response</span> = <span style="color: #800080"&gt;$this</span>->xmlToArray(<span style="color: #800080"&gt;$response</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$jsapi</span>=<span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;();
    </span><span style="color: #800080"&gt;$timeStamp</span> = <span style="color: #008080"&gt;time</span><span style="color: #000000"&gt;();
    </span><span style="color: #800080"&gt;$jsapi</span>['appId']=(<span style="color: #800080"&gt;$response</span>["appid"<span style="color: #000000"&gt;]);    
    </span><span style="color: #800080"&gt;$jsapi</span>['timeStamp']=("<span style="color: #800080"&gt;$timeStamp</span>"<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$jsapi</span>['nonceStr']=(<span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;createNoncestr());
    </span><span style="color: #800080"&gt;$jsapi</span>['package']=("prepay_id=" . <span style="color: #800080"&gt;$response</span>['prepay_id'<span style="color: #000000"&gt;]);
    </span><span style="color: #800080"&gt;$jsapi</span>['signType']=("MD5"<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$jsapi</span>['paySign']=(<span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$jsapi</span><span style="color: #000000"&gt;));
    </span><span style="color: #800080"&gt;$parameters</span> = json_encode(<span style="color: #800080"&gt;$jsapi</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; halt($jsapi);
    //请求数据,统一下单  </span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$parameters</span><span style="color: #000000"&gt;; 
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> payh5(<span style="color: #800080"&gt;$openid</span>,<span style="color: #800080"&gt;$out_trade_no</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;$notify_url</span> = ''<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$onoce_str</span> = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;createNoncestr();
    </span><span style="color: #800080"&gt;$data</span>["appid"] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;appId;
    </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>-><span style="color: #000000"&gt;mch_id;
    </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"] = "MWEB"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$data</span>["scene_info"] = "{'h5_info': {'type':'Wap','wap_url':  <span style="color: #800080"&gt;$notify_url</span>,'wap_name': '测试充值'}}"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$sign</span> = <span style="color: #800080"&gt;$this</span>->getSign(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span>["sign"] = <span style="color: #800080"&gt;$sign</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: #008000"&gt;//</span><span style="color: #008000"&gt; return $data;</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转成数组</span>
    <span style="color: #800080"&gt;$response</span> = <span style="color: #800080"&gt;$this</span>->xmlToArray(<span style="color: #800080"&gt;$response</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;请求数据,统一下单  </span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$response</span><span style="color: #000000"&gt;; 
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;function</span> getNonceStr(<span style="color: #800080"&gt;$length</span> = 32<span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$chars</span> = "abcdefghijklmnopqrstuvwxyz0123456789"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$str</span> =""<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: #008080"&gt;substr</span>(<span style="color: #800080"&gt;$chars</span>,<span style="color: #008080"&gt;mt_rand</span>(0,<span style="color: #008080"&gt;strlen</span>(<span style="color: #800080"&gt;$chars</span>)-1),1<span style="color: #000000"&gt;);
    }
    </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;   /*<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>签名*/</span>
<span style="color: #0000ff"&gt;public</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: #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 '【string1】'.$String.'</br>';
    //签名步骤二:在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>-><span style="color: #008080"&gt;key</span><span style="color: #000000"&gt;;
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;echo "【string2】".$String."</br>";
    //签名步骤三:MD5加密</span>
    <span style="color: #800080"&gt;$String</span> = <span style="color: #008080"&gt;md5</span>(<span style="color: #800080"&gt;$String</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;echo "【string3】 ".$String."</br>";
    //签名步骤四:所有字符转为大写</span>
    <span style="color: #800080"&gt;$result_</span> = <span style="color: #008080"&gt;strtoupper</span>(<span style="color: #800080"&gt;$String</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;echo "【result】 ".$result_."</br>";</span>
    <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$result_</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/suiji/" target="_blank" class="keywords">随机</a>字符串,不长于32位
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> createNoncestr( <span style="color: #800080"&gt;$length</span> = 32<span style="color: #000000"&gt; ){
    </span><span style="color: #800080"&gt;$chars</span> = "abcdefghijklmnopqrstuvwxyz0123456789"<span style="color: #000000"&gt;; 
    </span><span style="color: #800080"&gt;$str</span> =""<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: #008080"&gt;substr</span>(<span style="color: #800080"&gt;$chars</span>,1<span style="color: #000000"&gt;); 
    } 
    </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;public</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: #000000"&gt;{
            </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;*
*  作用:将xml转为array
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> xmlToArray(<span style="color: #800080"&gt;$xml</span><span style="color: #000000"&gt;){  
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将XML转为array       </span>
    <span style="color: #800080"&gt;$array_data</span> = json_decode(json_encode(<span style="color: #008080"&gt;simplexml_load_string</span>(<span style="color: #800080"&gt;$xml</span>,'SimpleXMLElement',LIBXML_NOCDATA)),<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$array_data</span><span style="color: #000000"&gt;;
}


</span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;*
*  作用:以post方式提交xml到对应的接口url
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;public</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>,<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>,<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;;
        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;public</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: #008080"&gt;getenv</span>('HTTP_CLIENT_IP') &amp;&amp; <span style="color: #008080"&gt;strcasecmp</span>(<span style="color: #008080"&gt;getenv</span>('HTTP_CLIENT_IP'),'unknown'<span style="color: #000000"&gt;)) {

        </span><span style="color: #800080"&gt;$ip</span> = <span style="color: #008080"&gt;getenv</span>('HTTP_CLIENT_IP'<span style="color: #000000"&gt;);

    } </span><span style="color: #0000ff"&gt;elseif</span>(<span style="color: #008080"&gt;getenv</span>('HTTP_X_FORWARDED_FOR') &amp;&amp; <span style="color: #008080"&gt;strcasecmp</span>(<span style="color: #008080"&gt;getenv</span>('HTTP_X_FORWARDED_FOR'),'unknown'<span style="color: #000000"&gt;)) {

        </span><span style="color: #800080"&gt;$ip</span> = <span style="color: #008080"&gt;getenv</span>('HTTP_X_FORWARDED_FOR'<span style="color: #000000"&gt;);

    } </span><span style="color: #0000ff"&gt;elseif</span>(<span style="color: #008080"&gt;getenv</span>('REMOTE_ADDR') &amp;&amp; <span style="color: #008080"&gt;strcasecmp</span>(<span style="color: #008080"&gt;getenv</span>('REMOTE_ADDR'),'unknown'<span style="color: #000000"&gt;)) {

        </span><span style="color: #800080"&gt;$ip</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: #0000ff"&gt;isset</span>(<span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR']) &amp;&amp; <span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR'] &amp;&amp; <span style="color: #008080"&gt;strcasecmp</span>(<span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR'],'unknown'<span style="color: #000000"&gt;)) {

        </span><span style="color: #800080"&gt;$ip</span> = <span style="color: #800080"&gt;$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000"&gt;];

    }

    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #008080"&gt;preg_match</span> ( '/[\d\.]{7,15}/',<span style="color: #800080"&gt;$ip</span>,<span style="color: #800080"&gt;$matches</span> ) ? <span style="color: #800080"&gt;$matches</span> [0] : ''<span style="color: #000000"&gt;;
}


</span><span style="color: #008000"&gt;/*</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> 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: #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: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> MakeSign(<span style="color: #800080"&gt;$unifiedorder</span><span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$this</span>->values=<span style="color: #800080"&gt;$unifiedorder</span><span style="color: #000000"&gt;;
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;签名步骤一:按字典序排序参数
    // ksort($this->values);</span>
    <span style="color: #800080"&gt;$string</span> = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;ToUrlParams();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;halt($string);
    //签名步骤二:在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>-><span style="color: #008080"&gt;key</span><span style="color: #000000"&gt;;
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;签名步骤三:MD5加密</span>
    <span style="color: #800080"&gt;$string</span> = <span style="color: #008080"&gt;md5</span>(<span style="color: #800080"&gt;$string</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;签名步骤四:所有字符转为大写</span>
    <span style="color: #800080"&gt;$result</span> = <span style="color: #008080"&gt;strtoupper</span>(<span style="color: #800080"&gt;$string</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$result</span><span style="color: #000000"&gt;;
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; ToUrlParams()
{
    </span><span style="color: #800080"&gt;$buff</span> = ""<span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;foreach</span> (<span style="color: #800080"&gt;$this</span>->values <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;$k</span> != "sign" &amp;&amp; <span style="color: #800080"&gt;$v</span> != "" &amp;&amp; !<span style="color: #008080"&gt;is_array</span>(<span style="color: #800080"&gt;$v</span><span style="color: #000000"&gt;)){
            </span><span style="color: #800080"&gt;$buff</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;$buff</span> = <span style="color: #008080"&gt;trim</span>(<span style="color: #800080"&gt;$buff</span>,"&amp;"<span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$buff</span><span style="color: #000000"&gt;;
}


      </span><span style="color: #0000ff"&gt;function</span> array2xml(<span style="color: #800080"&gt;$array</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;$array</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;$key</span><span style="color: #000000"&gt;)){
            </span><span style="color: #800080"&gt;$key</span>="item id=\"<span style="color: #800080"&gt;$key</span>\""<span style="color: #000000"&gt;;
        }</span><span style="color: #0000ff"&gt;else</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/wenzi/" target="_blank" class="keywords">文字</a>为key</span>
            <span style="color: #0000ff"&gt;list</span>(<span style="color: #800080"&gt;$key</span>,)=<span style="color: #008080"&gt;explode</span>(' ',<span style="color: #800080"&gt;$key</span><span style="color: #000000"&gt;);
        } 
        </span><span style="color: #800080"&gt;$xml</span>.="<<span style="color: #800080"&gt;$key</span>>"<span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$xml</span>.=<span style="color: #008080"&gt;is_array</span>(<span style="color: #800080"&gt;$val</span>)?<span style="color: #800080"&gt;$this</span>->_array2xml(<span style="color: #800080"&gt;$val</span>):<span style="color: #800080"&gt;$val</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/wenzi/" target="_blank" class="keywords">文字</a>为key</span>
        <span style="color: #0000ff"&gt;list</span>(<span style="color: #800080"&gt;$key</span>,<span style="color: #800080"&gt;$key</span><span style="color: #000000"&gt;);
        </span><span style="color: #800080"&gt;$xml</span>.="</<span style="color: #800080"&gt;$key</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;return</span> <span style="color: #800080"&gt;$xml</span><span style="color: #000000"&gt;;
}

   </span><span style="color: #0000ff"&gt;function</span> xml2array(<span style="color: #800080"&gt;$xml</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/jinzhi/" target="_blank" class="keywords">禁止</a>引用外部xml实体</span>
    libxml_disable_entity_loader(<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$values</span> = json_decode(json_encode(<span style="color: #008080"&gt;simplexml_load_string</span>(<span style="color: #800080"&gt;$xml</span>,<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);  
    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$values</span><span style="color: #000000"&gt;;
}


</span><span style="color: #0000ff"&gt;public</span>  <span style="color: #0000ff"&gt;function</span> request_post(<span style="color: #800080"&gt;$url</span> = '',<span style="color: #800080"&gt;$param</span> = ''<span style="color: #000000"&gt;)
{
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #0000ff"&gt;empty</span>(<span style="color: #800080"&gt;$url</span>) || <span style="color: #0000ff"&gt;empty</span>(<span style="color: #800080"&gt;$param</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: #800080"&gt;$postUrl</span> = <span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$curlPost</span> = <span style="color: #800080"&gt;$param</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$ch</span> = curl_init(); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;初始化curl</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$postUrl</span>); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;抓取指定网页</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,0); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;设置header</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,1); <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>,1); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;post提交方式</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$curlPost</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span> = curl_exec(<span style="color: #800080"&gt;$ch</span>); <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;运行curl</span>
    curl_close(<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;function</span> curl_post_ssl(<span style="color: #800080"&gt;$url</span>,<span style="color: #800080"&gt;$vars</span>,<span style="color: #800080"&gt;$second</span>=30,<span style="color: #800080"&gt;$aHeader</span>=<span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;())
{
    </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;curl_setopt($ch,CURLOPT_VERBOSE,'1');</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$second</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,1<span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,<span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSLCERT,'/data/cert/<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>.pem'<span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSLCERTPASSWD,'1234'<span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_SSLKEYTYPE,CURLOPT_SSLKEY,'/data/cert/<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>_private.pem'<span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span>( <span style="color: #008080"&gt;count</span>(<span style="color: #800080"&gt;$aHeader</span>) >= 1<span style="color: #000000"&gt; ){
            curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_HTTPHEADER,<span style="color: #800080"&gt;$aHeader</span><span style="color: #000000"&gt;);
    }
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$vars</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$data</span> = curl_exec(<span style="color: #800080"&gt;$ch</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;if</span>(<span style="color: #800080"&gt;$data</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: #0000ff"&gt;return</span> <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;;
    }
}

}

Alipay.PHP

PHP namespace app\phone\model;

<span style="color: #0000ff">use<span style="color: #000000"> think\Validate;
<span style="color: #0000ff">use think\<span style="color: #008080">Log<span style="color: #000000">;
<span style="color: #0000ff">use<span style="color: #000000"> think\Model;
<span style="color: #0000ff">use<span style="color: #000000"> lib\<span style="color: #ff0000">AlipaySubmit;

<span style="color: #0000ff">class Alipay <span style="color: #0000ff">extends<span style="color: #000000"> Model
{
<span style="color: #0000ff">protected <span style="color: #800080">$partner<span style="color: #000000">;
<span style="color: #0000ff">protected <span style="color: #800080">$md5_key<span style="color: #000000">;

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> __construct(<span style="color: #800080"&gt;$partner</span> = "",<span style="color: #800080"&gt;$md5_key</span> = ""<span style="color: #000000"&gt;)
{
    </span><span style="color: #800080"&gt;$this</span>->partner = <span style="color: #800080"&gt;$partner</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$this</span>->md5_key = <span style="color: #800080"&gt;$md5_key</span><span style="color: #000000"&gt;;
}

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #800080"&gt;$alipay_config</span> =<span style="color: #000000"&gt; [
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; 'partner'         => '',//支付宝partner,2088开头数字
    // 'seller_id'         => '',//支付宝partner,2088开头数字
    // 'key'             => '',//支付宝密钥</span>
    'sign_type' => 'MD5','input_charset' => 'utf-8','cacert' => '','transport' => 'http','payment_type' => '1','service' => '',<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;https://docs.open.alipay.com/api_1  看你需要哪个API接口</span>
    'anti_phishing_key' => '','exter_invoke_ip' => '','app_pay' => 'Y',<span style="color: #000000"&gt;
];

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> alipay(<span style="color: #800080"&gt;$data</span> =<span style="color: #000000"&gt; [])
{</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;发起支付宝支付</span>
    <span style="color: #800080"&gt;$validate</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; Validate([
        [</span>'out_trade_no','require|alphaNum','订单编号输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>1|订单编号输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>2'],<span style="color: #000000"&gt;
        [</span>'total_fee','require|number|gt:0','金额输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>|金额输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>|金额输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>'],<span style="color: #000000"&gt;
        [</span>'subject','require','请输入<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a>'],<span style="color: #000000"&gt;
        [</span>'body','请输入描述'],<span style="color: #000000"&gt;
        [</span>'notify_url','异步<a href="https://www.jb51.cc/tag/tongzhi/" target="_blank" class="keywords">通知</a>地址不为空'],<span style="color: #000000"&gt;
    ]);
    </span><span style="color: #0000ff"&gt;if</span> (!<span style="color: #800080"&gt;$validate</span>->check(<span style="color: #800080"&gt;$data</span><span style="color: #000000"&gt;)) {
        </span><span style="color: #0000ff"&gt;return</span> ['code' => 0,'msg' => <span style="color: #800080"&gt;$validate</span>-><span style="color: #000000"&gt;getError()];
    }
    </span><span style="color: #800080"&gt;$config</span> = self::<span style="color: #800080"&gt;$alipay_config</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$config</span>['partner'] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;partner;
    </span><span style="color: #800080"&gt;$config</span>['seller_id'] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;partner;
    </span><span style="color: #800080"&gt;$config</span>['key'] = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;md5_key;
    vendor(</span>'alipay.alipay'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$parameter</span> =<span style="color: #000000"&gt; [
        </span>"service" => <span style="color: #800080"&gt;$config</span>['service'],"partner" => <span style="color: #800080"&gt;$config</span>['partner'],"seller_id" => <span style="color: #800080"&gt;$config</span>['seller_id'],"payment_type" => <span style="color: #800080"&gt;$config</span>['payment_type'],"notify_url" => <span style="color: #800080"&gt;$data</span>['notify_url'],"return_url" => <span style="color: #800080"&gt;$data</span>['return_url'],"anti_phishing_key" => <span style="color: #800080"&gt;$config</span>['anti_phishing_key'],"exter_invoke_ip" => <span style="color: #800080"&gt;$config</span>['exter_invoke_ip'],"out_trade_no" => <span style="color: #800080"&gt;$data</span>['out_trade_no'],"subject" => <span style="color: #800080"&gt;$data</span>['subject'],"total_fee" => <span style="color: #800080"&gt;$data</span>['total_fee'],"body" => <span style="color: #800080"&gt;$data</span>['body'],"_input_charset" => <span style="color: #800080"&gt;$config</span>['input_charset'],<span style="color: #000000"&gt;

    ];
    </span><span style="color: #800080"&gt;$alipaySubmit</span> = <span style="color: #0000ff"&gt;new</span> AlipaySubmit(<span style="color: #800080"&gt;$config</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;return</span> ['code' => 1,'msg' => <span style="color: #800080"&gt;$alipaySubmit</span>->buildRequestForm(<span style="color: #800080"&gt;$parameter</span>,"get","确认"<span style="color: #000000"&gt;)];
}

}

?>

文件:

wx.html在使用微信客户端支付时需要的页面

微信公众号支付
Email:361169632@qq.com注明出处:
原文链接:https://www.f2er.com/thinkphp/69301.html

猜你在找的ThinkPHP相关文章