Pay.PHP支付控制器
<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">/*</span><span style="color: #008000">
微信h5支付
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> wechat_pay()
{
</span><span style="color: #0000ff">if</span> (input('post.token') ==<span style="color: #000000"> TOKEN) {
</span><span style="color: #008000">//</span><span style="color: #008000"> 判断<a href="/tag/yonghu/" target="_blank" class="keywords">用户</a>是在微信浏览器打开还是其他浏览器打开。</span>
<span style="color: #800080">$userAgent</span> = <span style="color: #800080">$_SERVER</span>['HTTP_USER_AGENT'<span style="color: #000000">];
</span><span style="color: #0000ff">if</span> (<span style="color: #008080">strpos</span>(<span style="color: #800080">$userAgent</span>,'MicroMessenger'<span style="color: #000000">)) {
</span><span style="color: #800080">$wechat</span> = <span style="color: #0000ff">new</span> Weixin(<span style="color: #800080">$this</span>->wechat_config['appsecret'],<span style="color: #800080">$this</span>->wechat_config['app_id'<span style="color: #000000">]);
</span><span style="color: #800080">$wechat_pay</span> = <span style="color: #0000ff">new</span> Weixin_pay(<span style="color: #800080">$this</span>->wechat_config['appsecret'],<span style="color: #800080">$this</span>->wechat_config['app_id'],<span style="color: #800080">$this</span>->wechat_config['md5_key'],<span style="color: #800080">$this</span>->wechat_config['mch_id'<span style="color: #000000">]);
</span><span style="color: #800080">$code</span> = input('post.code'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (session::has('openid'<span style="color: #000000">)) {
</span><span style="color: #800080">$openid</span> = session::get('openid'<span style="color: #000000">);
</span><span style="color: #800080">$total_fee</span> = 1<span style="color: #000000">;
</span><span style="color: #800080">$out_trade_no</span> = '12345678945641231'<span style="color: #000000">;
</span><span style="color: #800080">$body</span> = "充值金币支付测试"<span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$wechat_pay</span>->pay(<span style="color: #800080">$openid</span>,<span style="color: #800080">$total_fee</span>,<span style="color: #800080">$body</span>,<span style="color: #800080">$out_trade_no</span><span style="color: #000000">);
</span><span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['url'] = '本控制器下的地址/' . 'wx?data=' . <span style="color: #800080">$res</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #0000ff">if</span> (!<span style="color: #0000ff">empty</span>(<span style="color: #800080">$code</span><span style="color: #000000">)) {
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$wechat</span>->get_access_token(<span style="color: #800080">$code</span><span style="color: #000000">);
</span><span style="color: #800080">$userinfo</span> = <span style="color: #800080">$wechat</span>->get_openid_userinfo(<span style="color: #800080">$res</span>['access_token'],<span style="color: #800080">$res</span>['openid'<span style="color: #000000">]);
</span><span style="color: #800080">$openid</span> = <span style="color: #800080">$userinfo</span>['openid'<span style="color: #000000">];
session</span>::set('openid',<span style="color: #800080">$openid</span><span style="color: #000000">);
</span><span style="color: #800080">$total_fee</span> = 1<span style="color: #000000">;
</span><span style="color: #800080">$out_trade_no</span> = '12345678945641231'<span style="color: #000000">;
</span><span style="color: #800080">$body</span> = "充值金币支付测试"<span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$wechat_pay</span>->pay(<span style="color: #800080">$openid</span>,<span style="color: #800080">$out_trade_no</span><span style="color: #000000">);
</span><span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['url'] = '本控制器下的地址/' . 'wx?data=' . <span style="color: #800080">$res</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$link</span> = 'http://' . <span style="color: #800080">$_SERVER</span>['HTTP_HOST'] . '/#/cz'; <span style="color: #008000">//</span><span style="color: #008000"> 前端充值金币<a href="/tag/yemian/" target="_blank" class="keywords">页面</a>地址。</span>
cookie::set('url',<span style="color: #800080">$link</span>,300<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">$link 这个地址是在微信打开你项目时才会需要的,地址后面带上<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>的code进行<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token。</span>
<span style="color: #800080">$res</span> = <span style="color: #800080">$wechat</span>->code_shouquan(<span style="color: #800080">$link</span><span style="color: #000000">);
</span><span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['url'] = <span style="color: #800080">$res</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
}
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #008000">//</span><span style="color: #008000">非微信浏览器</span>
<span style="color: #800080">$wechat_pay</span> = <span style="color: #0000ff">new</span> Weixin_pay(<span style="color: #800080">$this</span>->wechat_config['appsecret'],<span style="color: #800080">$this</span>->wechat_config['mch_id'<span style="color: #000000">]);
</span><span style="color: #008000">//</span><span style="color: #008000">h5支付不用传递openid 此处与微信jsapi支付不同</span>
<span style="color: #800080">$openid</span> = ""<span style="color: #000000">;
</span><span style="color: #800080">$total_fee</span> = 1<span style="color: #000000">;
</span><span style="color: #800080">$out_trade_no</span> = '12345678945641231'<span style="color: #000000">;
</span><span style="color: #800080">$body</span> = "充值金币h5支付测试"<span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$wechat_pay</span>->payh5(<span style="color: #800080">$openid</span>,<span style="color: #800080">$out_trade_no</span><span style="color: #000000">);
</span><span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['url'] = <span style="color: #800080">$res</span>['mweb_url'];<span style="color: #008000">//</span><span style="color: #008000">微信唤起H5支付地址</span>
<span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000">;
}
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
在微信打开的时候唤起微信<a href="/tag/gongzhonghaozhifu/" target="_blank" class="keywords">公众号支付</a>。
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> wx()
{
</span><span style="color: #800080">$data</span> = input('get.data'<span style="color: #000000">);
</span><span style="color: #800080">$this</span>->assign('data',<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span><span style="color: #000000"> view();
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>微信code
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> code()
{
</span><span style="color: #800080">$code</span> = input('param.code'<span style="color: #000000">);
</span><span style="color: #800080">$this</span>->redirect(cookie::get('url') . '?code=' . <span style="color: #800080">$code</span><span style="color: #000000">);
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
微信回调
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> wechat_notify()
{
</span><span style="color: #800080">$ccc</span> = <span style="color: #008080">file_get_contents</span>('<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>://input'<span style="color: #000000">);
</span><span style="color: #800080">$Weixin_pay</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Weixin_pay();
</span><span style="color: #800080">$data</span> = <span style="color: #800080">$Weixin_pay</span>->xmlToArray(<span style="color: #800080">$ccc</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$data</span>['result_code'] == 'SUCCESS' && <span style="color: #800080">$data</span>['return_code'] == 'SUCCESS'<span style="color: #000000">) {
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>订单号</span>
<span style="color: #800080">$Ordersn</span> = <span style="color: #800080">$data</span>['out_trade_no'];<span style="color: #008000">//</span><span style="color: #008000">订单号
// 处理支付成功后的逻辑业务</span>
<span style="color: #000000"> }
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
微信APP支付
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> app_wechat_pay()
{
</span><span style="color: #0000ff">if</span> (input('post.token') ==<span style="color: #000000"> TOKEN) {
</span><span style="color: #800080">$wechat_pay</span> = <span style="color: #0000ff">new</span> Weixin_pay(<span style="color: #800080">$this</span>->wechat_config['appsecret'],<span style="color: #800080">$this</span>->wechat_config['mch_id'<span style="color: #000000">]);
</span><span style="color: #008000">//</span><span style="color: #008000">h5支付不用传递openid 此处与微信jsapi支付不同</span>
<span style="color: #800080">$openid</span> = ""<span style="color: #000000">;
</span><span style="color: #800080">$total_fee</span> = 1<span style="color: #000000">;
</span><span style="color: #800080">$body</span> = "充值金币APP支付测试"<span style="color: #000000">;
</span><span style="color: #800080">$order_sn</span> = 123456789;<span style="color: #008000">//</span><span style="color: #008000">订单号</span>
<span style="color: #800080">$res</span> = <span style="color: #800080">$wechat_pay</span>->app_pay(<span style="color: #800080">$body</span>,<span style="color: #800080">$order_sn</span>,<span style="color: #800080">$total_fee</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$res</span>['prepay_id']) {<span style="color: #008000">//</span><span style="color: #008000">判断返回参数中是否有prepay_id</span>
<span style="color: #800080">$order</span> = <span style="color: #800080">$wechat_pay</span>->getOrder(<span style="color: #800080">$res</span>['prepay_id']);<span style="color: #008000">//</span><span style="color: #008000">执行二次签名返回参数</span>
<span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['order'] = <span style="color: #800080">$order</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$arr</span>['errcode'] = '1003'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = <span style="color: #800080">$res</span>['err_code_des'<span style="color: #000000">];
</span><span style="color: #800080">$data</span> = json_encode(<span style="color: #800080">$arr</span><span style="color: #000000">);
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000">;
}
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
支付宝开始支付
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> alipay()
{</span><span style="color: #008000">//</span><span style="color: #008000">发起支付宝支付</span>
<span style="color: #0000ff">if</span> (input('post.token') ==<span style="color: #000000"> TOKEN) {
</span><span style="color: #800080">$link</span> = 'http://' . <span style="color: #800080">$_SERVER</span>['HTTP_HOST'] . '/#/cz'<span style="color: #000000">;
cookie</span>::set('url',300<span style="color: #000000">);
</span><span style="color: #800080">$res</span>['out_trade_no'] = 123456798123<span style="color: #000000">;
</span><span style="color: #800080">$res</span>['subject'] = '充值'<span style="color: #000000">;
</span><span style="color: #800080">$res</span>['body'] = '测试'<span style="color: #000000">;
</span><span style="color: #800080">$res</span>['money'] = 0.01<span style="color: #000000">;
</span><span style="color: #800080">$Pay</span> = <span style="color: #0000ff">new</span> Alipay(<span style="color: #800080">$this</span>->alipay_config['partner'],<span style="color: #800080">$this</span>->alipay_config['md5_key'<span style="color: #000000">]);
</span><span style="color: #800080">$result</span> = <span style="color: #800080">$Pay</span>-><span style="color: #000000">alipay([
</span>'notify_url' => 'your notify_url','return_url' => 'your return_url','out_trade_no' => <span style="color: #800080">$res</span>['out_trade_no'],'subject' => <span style="color: #800080">$res</span>['subject'],'total_fee' => <span style="color: #800080">$res</span>['money'],'body' => <span style="color: #800080">$res</span>['body'],<span style="color: #000000">
]);
</span><span style="color: #800080">$arr</span>['errcode'] = '0'<span style="color: #000000">;
</span><span style="color: #800080">$arr</span>['errmsg'] = 'ok'<span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$result</span>['msg'<span style="color: #000000">];
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$data</span> = '{"errcode":"1001","errmsg":"TOKEN无效"}'<span style="color: #000000">;
}
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
支付宝回调
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> alipay_notify()
{</span><span style="color: #008000">//</span><span style="color: #008000">异步订单<a href="/tag/tongzhi/" target="_blank" class="keywords">通知</a></span>
<span style="color: #800080">$wx</span> = sys_config_get('payment'<span style="color: #000000">);
</span><span style="color: #800080">$Pay</span> = <span style="color: #0000ff">new</span> Alipay(<span style="color: #800080">$wx</span>['aliwappay']['partner'],<span style="color: #800080">$wx</span>['aliwappay']['md5_key'<span style="color: #000000">]);
</span><span style="color: #800080">$out_trade_no</span> = input('out_trade_no'<span style="color: #000000">);
</span><span style="color: #800080">$trade_status</span> = input('trade_status'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$trade_status</span> == 'TRADE_SUCCESS'<span style="color: #000000">) {
</span><span style="color: #008000">//</span><span style="color: #008000"> 处理支付成功后的逻辑业务</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
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">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$appScrect</span> = "",<span style="color: #800080">$appId</span> = ""<span style="color: #000000">)
{
</span><span style="color: #800080">$this</span>->appScrect = <span style="color: #800080">$appScrect</span><span style="color: #000000">;
</span><span style="color: #800080">$this</span>->appId = <span style="color: #800080">$appId</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
* 因为本项目需求,前端是Vue<a href="/tag/houtai/" target="_blank" class="keywords">后台</a>是TP5两个项目不在一个服务器所以我的url是return 回控制器通过接口返回给前端
* 如果是正常html<a href="/tag/yemian/" target="_blank" class="keywords">页面</a>可以使用header("Location: $url"); 以下<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>都是
* </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> code_shouquan(<span style="color: #800080">$link</span><span style="color: #000000">)
{
</span><span style="color: #800080">$redirect_uri</span> = <span style="color: #008080">urlencode</span>('网址/pay/code');<span style="color: #008000">//</span><span style="color: #008000">微信<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>网页授权地址
// 1、引导<a href="/tag/yonghu/" target="_blank" class="keywords">用户</a>进入授权<a href="/tag/yemian/" target="_blank" class="keywords">页面</a>同意授权,<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>code
// 2、通过code换取网页授权access_token
// 3、如果需要,开发者可以刷新网页授权access_token,避免过期
// 4、通过网页授权access_token和openid<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="/tag/yonghu/" target="_blank" class="keywords">用户</a>基本信息(<a href="/tag/zhichi/" target="_blank" class="keywords">支持</a>UnionID机制)</span>
<span style="color: #800080">$url</span> = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . <span style="color: #800080">$this</span>->appId . "&redirect_uri=" . <span style="color: #800080">$redirect_uri</span> . "&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect"<span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$url</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> get_access_token(<span style="color: #800080">$code</span><span style="color: #000000">)
{
</span><span style="color: #008000">//</span><span style="color: #008000">检测缓存中是否有access_token(2小时),如果存在直接返回,不存在则检测缓存中的refresh_token(30天),// refresh_token如果存在<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>刷新缓存;如果不存在重新发起授权code授权</span>
<span style="color: #800080">$url</span> = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . <span style="color: #800080">$this</span>->appId . "&secret=" . <span style="color: #800080">$this</span>->appScrect . "&code=" . <span style="color: #800080">$code</span> . "&grant_type=authorization_code"<span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$this</span>->sendRequest(<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$res</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> get_refresh_token(<span style="color: #800080">$refresh_token</span><span style="color: #000000">)
{
</span><span style="color: #800080">$url</span> = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" . <span style="color: #800080">$this</span>->appId . "&grant_type=refresh_token&refresh_token=" . <span style="color: #800080">$refresh_token</span><span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$this</span>->sendRequest(<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$res</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> get_openid_userinfo(<span style="color: #800080">$access_token</span>,<span style="color: #800080">$openid</span><span style="color: #000000">)
{
</span><span style="color: #800080">$url</span> = "https://api.weixin.qq.com/sns/userinfo?access_token=" . <span style="color: #800080">$access_token</span> . "&openid=" . <span style="color: #800080">$openid</span> . "&lang=zh_CN"<span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$this</span>->sendRequest(<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$res</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
发送请求
<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
{
<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">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$appScrect</span>="",<span style="color: #800080">$appId</span>="",<span style="color: #800080">$key</span>="",<span style="color: #800080">$mch_id</span> =''<span style="color: #000000">){
</span><span style="color: #800080">$this</span>->appScrect=<span style="color: #800080">$appScrect</span><span style="color: #000000">;
</span><span style="color: #800080">$this</span>->appId=<span style="color: #800080">$appId</span><span style="color: #000000">;
</span><span style="color: #800080">$this</span>-><span style="color: #008080">key</span>=<span style="color: #800080">$key</span><span style="color: #000000">;
</span><span style="color: #800080">$this</span>->mch_id=<span style="color: #800080">$mch_id</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> app_pay(<span style="color: #800080">$body</span>,<span style="color: #800080">$out_trade_no</span>,<span style="color: #800080">$total_fee</span><span style="color: #000000">){
</span><span style="color: #800080">$url</span> = "https://api.mch.weixin.qq.com/pay/unifiedorder"<span style="color: #000000">;
</span><span style="color: #800080">$notify_url</span> = '';<span style="color: #008000">//</span><span style="color: #008000">可以在这填写回调地址也可以<a href="/tag/zengjia/" target="_blank" class="keywords">增加</a>一个参数传过来。也可以直接在构造里面加一个notify_<a href="/tag/URLdizhi/" target="_blank" class="keywords">URL地址</a></span>
<span style="color: #800080">$onoce_str</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">createNoncestr();
</span><span style="color: #800080">$data</span>["appid"] = <span style="color: #800080">$this</span>-><span style="color: #000000">appId;
</span><span style="color: #800080">$data</span>["body"] = <span style="color: #800080">$body</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["mch_id"] = <span style="color: #800080">$this</span>-><span style="color: #000000">mch_id;
</span><span style="color: #800080">$data</span>["nonce_str"] = <span style="color: #800080">$onoce_str</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["notify_url"] = <span style="color: #800080">$notify_url</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["out_trade_no"] = <span style="color: #800080">$out_trade_no</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["spbill_create_ip"] = <span style="color: #800080">$this</span>-><span style="color: #000000">get_client_ip();
</span><span style="color: #800080">$data</span>["total_fee"] = <span style="color: #800080">$total_fee</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["trade_type"] = "APP"<span style="color: #000000">;
</span><span style="color: #800080">$sign</span> = <span style="color: #800080">$this</span>->getSign(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span>["sign"] = <span style="color: #800080">$sign</span><span style="color: #000000">;
</span><span style="color: #800080">$xml</span> = <span style="color: #800080">$this</span>->arrayToXml(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->postXmlCurl(<span style="color: #800080">$xml</span>,<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000"> return $xml;
//将微信返回的结果xml转成数组</span>
<span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->xmlToArray(<span style="color: #800080">$response</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">返回数据</span>
<span style="color: #0000ff">return</span> <span style="color: #800080">$response</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">执行第二次签名,才能返回给客户端使用</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> getOrder(<span style="color: #800080">$prepayId</span><span style="color: #000000">){
</span><span style="color: #800080">$data</span>["appid"] = <span style="color: #800080">$this</span>-><span style="color: #000000">appId;
</span><span style="color: #800080">$data</span>["noncestr"] = <span style="color: #800080">$this</span>-><span style="color: #000000">createNoncestr();;
</span><span style="color: #800080">$data</span>["package"] = "Sign=WXPay"<span style="color: #000000">;
</span><span style="color: #800080">$data</span>["partnerid"] = <span style="color: #800080">$this</span>-><span style="color: #000000">mch_id;
</span><span style="color: #800080">$data</span>["prepayid"] = <span style="color: #800080">$prepayId</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["timestamp"] = <span style="color: #008080">time</span><span style="color: #000000">();
</span><span style="color: #800080">$s</span> = <span style="color: #800080">$this</span>->getSign(<span style="color: #800080">$data</span>,<span style="color: #0000ff">false</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span>["sign"] = <span style="color: #800080">$s</span><span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
<a href="/tag/gongzhonghaozhifu/" target="_blank" class="keywords">公众号支付</a>
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> pay(<span style="color: #800080">$openid</span>,<span style="color: #800080">$out_trade_no</span><span style="color: #000000">){
</span><span style="color: #800080">$url</span> = "https://api.mch.weixin.qq.com/pay/unifiedorder"<span style="color: #000000">;
</span><span style="color: #800080">$notify_url</span> = ''<span style="color: #000000">;
</span><span style="color: #800080">$onoce_str</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">createNoncestr();
</span><span style="color: #800080">$data</span>["appid"] = <span style="color: #800080">$this</span>-><span style="color: #000000">appId;
</span><span style="color: #800080">$data</span>["body"] = <span style="color: #800080">$body</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["mch_id"] = <span style="color: #800080">$this</span>-><span style="color: #000000">mch_id;
</span><span style="color: #800080">$data</span>["nonce_str"] = <span style="color: #800080">$onoce_str</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["notify_url"] = <span style="color: #800080">$notify_url</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["out_trade_no"] = <span style="color: #800080">$out_trade_no</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["spbill_create_ip"] = <span style="color: #800080">$this</span>-><span style="color: #000000">get_client_ip();
</span><span style="color: #800080">$data</span>["total_fee"] = <span style="color: #800080">$total_fee</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["trade_type"] = "JSAPI"<span style="color: #000000">;
</span><span style="color: #800080">$data</span>["openid"] = <span style="color: #800080">$openid</span><span style="color: #000000">;
</span><span style="color: #800080">$sign</span> = <span style="color: #800080">$this</span>->getSign(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span>["sign"] = <span style="color: #800080">$sign</span><span style="color: #000000">;
</span><span style="color: #800080">$xml</span> = <span style="color: #800080">$this</span>->arrayToXml(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->postXmlCurl(<span style="color: #800080">$xml</span>,<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">将微信返回的结果xml转成数组</span>
<span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->xmlToArray(<span style="color: #800080">$response</span><span style="color: #000000">);
</span><span style="color: #800080">$jsapi</span>=<span style="color: #0000ff">array</span><span style="color: #000000">();
</span><span style="color: #800080">$timeStamp</span> = <span style="color: #008080">time</span><span style="color: #000000">();
</span><span style="color: #800080">$jsapi</span>['appId']=(<span style="color: #800080">$response</span>["appid"<span style="color: #000000">]);
</span><span style="color: #800080">$jsapi</span>['timeStamp']=("<span style="color: #800080">$timeStamp</span>"<span style="color: #000000">);
</span><span style="color: #800080">$jsapi</span>['nonceStr']=(<span style="color: #800080">$this</span>-><span style="color: #000000">createNoncestr());
</span><span style="color: #800080">$jsapi</span>['package']=("prepay_id=" . <span style="color: #800080">$response</span>['prepay_id'<span style="color: #000000">]);
</span><span style="color: #800080">$jsapi</span>['signType']=("MD5"<span style="color: #000000">);
</span><span style="color: #800080">$jsapi</span>['paySign']=(<span style="color: #800080">$this</span>->getSign(<span style="color: #800080">$jsapi</span><span style="color: #000000">));
</span><span style="color: #800080">$parameters</span> = json_encode(<span style="color: #800080">$jsapi</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000"> halt($jsapi);
//请求数据,统一下单 </span>
<span style="color: #0000ff">return</span> <span style="color: #800080">$parameters</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> payh5(<span style="color: #800080">$openid</span>,<span style="color: #800080">$out_trade_no</span><span style="color: #000000">){
</span><span style="color: #800080">$url</span> = "https://api.mch.weixin.qq.com/pay/unifiedorder"<span style="color: #000000">;
</span><span style="color: #800080">$notify_url</span> = ''<span style="color: #000000">;
</span><span style="color: #800080">$onoce_str</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">createNoncestr();
</span><span style="color: #800080">$data</span>["appid"] = <span style="color: #800080">$this</span>-><span style="color: #000000">appId;
</span><span style="color: #800080">$data</span>["body"] = <span style="color: #800080">$body</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["mch_id"] = <span style="color: #800080">$this</span>-><span style="color: #000000">mch_id;
</span><span style="color: #800080">$data</span>["nonce_str"] = <span style="color: #800080">$onoce_str</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["notify_url"] = <span style="color: #800080">$notify_url</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["out_trade_no"] = <span style="color: #800080">$out_trade_no</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["spbill_create_ip"] = <span style="color: #800080">$this</span>-><span style="color: #000000">get_client_ip();
</span><span style="color: #800080">$data</span>["total_fee"] = <span style="color: #800080">$total_fee</span><span style="color: #000000">;
</span><span style="color: #800080">$data</span>["trade_type"] = "MWEB"<span style="color: #000000">;
</span><span style="color: #800080">$data</span>["scene_info"] = "{'h5_info': {'type':'Wap','wap_url': <span style="color: #800080">$notify_url</span>,'wap_name': '测试充值'}}"<span style="color: #000000">;
</span><span style="color: #800080">$sign</span> = <span style="color: #800080">$this</span>->getSign(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span>["sign"] = <span style="color: #800080">$sign</span><span style="color: #000000">;
</span><span style="color: #800080">$xml</span> = <span style="color: #800080">$this</span>->arrayToXml(<span style="color: #800080">$data</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000"> return $data;</span>
<span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->postXmlCurl(<span style="color: #800080">$xml</span>,<span style="color: #800080">$url</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">将微信返回的结果xml转成数组</span>
<span style="color: #800080">$response</span> = <span style="color: #800080">$this</span>->xmlToArray(<span style="color: #800080">$response</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">请求数据,统一下单 </span>
<span style="color: #0000ff">return</span> <span style="color: #800080">$response</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">function</span> getNonceStr(<span style="color: #800080">$length</span> = 32<span style="color: #000000">)
{
</span><span style="color: #800080">$chars</span> = "abcdefghijklmnopqrstuvwxyz0123456789"<span style="color: #000000">;
</span><span style="color: #800080">$str</span> =""<span style="color: #000000">;
</span><span style="color: #0000ff">for</span> ( <span style="color: #800080">$i</span> = 0; <span style="color: #800080">$i</span> < <span style="color: #800080">$length</span>; <span style="color: #800080">$i</span>++<span style="color: #000000"> ) {
</span><span style="color: #800080">$str</span> .= <span style="color: #008080">substr</span>(<span style="color: #800080">$chars</span>,<span style="color: #008080">mt_rand</span>(0,<span style="color: #008080">strlen</span>(<span style="color: #800080">$chars</span>)-1),1<span style="color: #000000">);
}
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$str</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000"> /*<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>签名*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> getSign(<span style="color: #800080">$Obj</span><span style="color: #000000">){
</span><span style="color: #0000ff">foreach</span> (<span style="color: #800080">$Obj</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$k</span> => <span style="color: #800080">$v</span><span style="color: #000000">){
</span><span style="color: #800080">$Parameters</span>[<span style="color: #800080">$k</span>] = <span style="color: #800080">$v</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">签名步骤一:按字典序排序参数</span>
<span style="color: #008080">ksort</span>(<span style="color: #800080">$Parameters</span><span style="color: #000000">);
</span><span style="color: #800080">$String</span> = <span style="color: #800080">$this</span>->formatBizQueryParaMap(<span style="color: #800080">$Parameters</span>,<span style="color: #0000ff">false</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">echo '【string1】'.$String.'</br>';
//签名步骤二:在string后加入KEY</span>
<span style="color: #800080">$String</span> = <span style="color: #800080">$String</span>."&key=".<span style="color: #800080">$this</span>-><span style="color: #008080">key</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">echo "【string2】".$String."</br>";
//签名步骤三:MD5加密</span>
<span style="color: #800080">$String</span> = <span style="color: #008080">md5</span>(<span style="color: #800080">$String</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写</span>
<span style="color: #800080">$result_</span> = <span style="color: #008080">strtoupper</span>(<span style="color: #800080">$String</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">echo "【result】 ".$result_."</br>";</span>
<span style="color: #0000ff">return</span> <span style="color: #800080">$result_</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* 作用:产生<a href="/tag/suiji/" target="_blank" class="keywords">随机</a>字符串,不长于32位
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> createNoncestr( <span style="color: #800080">$length</span> = 32<span style="color: #000000"> ){
</span><span style="color: #800080">$chars</span> = "abcdefghijklmnopqrstuvwxyz0123456789"<span style="color: #000000">;
</span><span style="color: #800080">$str</span> =""<span style="color: #000000">;
</span><span style="color: #0000ff">for</span> ( <span style="color: #800080">$i</span> = 0; <span style="color: #800080">$i</span> < <span style="color: #800080">$length</span>; <span style="color: #800080">$i</span>++<span style="color: #000000"> ) {
</span><span style="color: #800080">$str</span>.= <span style="color: #008080">substr</span>(<span style="color: #800080">$chars</span>,1<span style="color: #000000">);
}
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$str</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">数组转xml</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> arrayToXml(<span style="color: #800080">$arr</span><span style="color: #000000">){
</span><span style="color: #800080">$xml</span> = "<xml>"<span style="color: #000000">;
</span><span style="color: #0000ff">foreach</span> (<span style="color: #800080">$arr</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$key</span>=><span style="color: #800080">$val</span><span style="color: #000000">){
</span><span style="color: #0000ff">if</span> (<span style="color: #008080">is_numeric</span>(<span style="color: #800080">$val</span><span style="color: #000000">)){
</span><span style="color: #800080">$xml</span>.="<".<span style="color: #800080">$key</span>.">".<span style="color: #800080">$val</span>."</".<span style="color: #800080">$key</span>.">"<span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #800080">$xml</span>.="<".<span style="color: #800080">$key</span>."><![CDATA[".<span style="color: #800080">$val</span>."]]></".<span style="color: #800080">$key</span>.">"<span style="color: #000000">;
}
}
</span><span style="color: #800080">$xml</span>.="</xml>"<span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$xml</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* 作用:将xml转为array
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> xmlToArray(<span style="color: #800080">$xml</span><span style="color: #000000">){
</span><span style="color: #008000">//</span><span style="color: #008000">将XML转为array </span>
<span style="color: #800080">$array_data</span> = json_decode(json_encode(<span style="color: #008080">simplexml_load_string</span>(<span style="color: #800080">$xml</span>,'SimpleXMLElement',LIBXML_NOCDATA)),<span style="color: #0000ff">true</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$array_data</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* 作用:以post方式提交xml到对应的接口url
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> postXmlCurl(<span style="color: #800080">$xml</span>,<span style="color: #800080">$url</span>,<span style="color: #800080">$second</span>=30<span style="color: #000000">){
</span><span style="color: #008000">//</span><span style="color: #008000">初始化curl </span>
<span style="color: #800080">$ch</span> =<span style="color: #000000"> curl_init();
</span><span style="color: #008000">//</span><span style="color: #008000">设置超时</span>
curl_setopt(<span style="color: #800080">$ch</span>,CURLOPT_TIMEOUT,<span style="color: #800080">$second</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY,'8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT,8080);</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #0000ff">FALSE</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">设置header</span>
curl_setopt(<span style="color: #800080">$ch</span>,CURLOPT_HEADER,<span style="color: #0000ff">FALSE</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">要求结果为字符串且<a href="/tag/shuchu/" target="_blank" class="keywords">输出</a>到屏幕上</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #0000ff">TRUE</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">post提交方式</span>
curl_setopt(<span style="color: #800080">$ch</span>,CURLOPT_POST,<span style="color: #0000ff">TRUE</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080">$xml</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">运行curl</span>
<span style="color: #800080">$data</span> = curl_exec(<span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">返回结果</span>
<span style="color: #0000ff">if</span>(<span style="color: #800080">$data</span><span style="color: #000000">){
curl_close(</span><span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #800080">$error</span> = curl_errno(<span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> "curl出错,<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>码:<span style="color: #800080">$error</span>"."<br>"<span style="color: #000000">;
curl_close(</span><span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
}
}
</span><span style="color: #008000">/*</span><span style="color: #008000">
<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>当前服务器的IP
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> get_client_ip(){
</span><span style="color: #0000ff">if</span>(<span style="color: #008080">getenv</span>('HTTP_CLIENT_IP') && <span style="color: #008080">strcasecmp</span>(<span style="color: #008080">getenv</span>('HTTP_CLIENT_IP'),'unknown'<span style="color: #000000">)) {
</span><span style="color: #800080">$ip</span> = <span style="color: #008080">getenv</span>('HTTP_CLIENT_IP'<span style="color: #000000">);
} </span><span style="color: #0000ff">elseif</span>(<span style="color: #008080">getenv</span>('HTTP_X_FORWARDED_FOR') && <span style="color: #008080">strcasecmp</span>(<span style="color: #008080">getenv</span>('HTTP_X_FORWARDED_FOR'),'unknown'<span style="color: #000000">)) {
</span><span style="color: #800080">$ip</span> = <span style="color: #008080">getenv</span>('HTTP_X_FORWARDED_FOR'<span style="color: #000000">);
} </span><span style="color: #0000ff">elseif</span>(<span style="color: #008080">getenv</span>('REMOTE_ADDR') && <span style="color: #008080">strcasecmp</span>(<span style="color: #008080">getenv</span>('REMOTE_ADDR'),'unknown'<span style="color: #000000">)) {
</span><span style="color: #800080">$ip</span> = <span style="color: #008080">getenv</span>('REMOTE_ADDR'<span style="color: #000000">);
} </span><span style="color: #0000ff">elseif</span>(<span style="color: #0000ff">isset</span>(<span style="color: #800080">$_SERVER</span>['REMOTE_ADDR']) && <span style="color: #800080">$_SERVER</span>['REMOTE_ADDR'] && <span style="color: #008080">strcasecmp</span>(<span style="color: #800080">$_SERVER</span>['REMOTE_ADDR'],'unknown'<span style="color: #000000">)) {
</span><span style="color: #800080">$ip</span> = <span style="color: #800080">$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000">];
}
</span><span style="color: #0000ff">return</span> <span style="color: #008080">preg_match</span> ( '/[\d\.]{7,15}/',<span style="color: #800080">$ip</span>,<span style="color: #800080">$matches</span> ) ? <span style="color: #800080">$matches</span> [0] : ''<span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* 作用:格式化参数,签名过程需要使用
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> formatBizQueryParaMap(<span style="color: #800080">$paraMap</span>,<span style="color: #800080">$urlencode</span><span style="color: #000000">){
</span><span style="color: #800080">$buff</span> = ""<span style="color: #000000">;
</span><span style="color: #008080">ksort</span>(<span style="color: #800080">$paraMap</span><span style="color: #000000">);
</span><span style="color: #0000ff">foreach</span> (<span style="color: #800080">$paraMap</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$k</span> => <span style="color: #800080">$v</span><span style="color: #000000">){
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$urlencode</span><span style="color: #000000">){
</span><span style="color: #800080">$v</span> = <span style="color: #008080">urlencode</span>(<span style="color: #800080">$v</span><span style="color: #000000">);
}
</span><span style="color: #800080">$buff</span> .= <span style="color: #800080">$k</span> . "=" . <span style="color: #800080">$v</span> . "&"<span style="color: #000000">;
}
</span><span style="color: #800080">$reqPar</span><span style="color: #000000">;
</span><span style="color: #0000ff">if</span> (<span style="color: #008080">strlen</span>(<span style="color: #800080">$buff</span>) > 0<span style="color: #000000">){
</span><span style="color: #800080">$reqPar</span> = <span style="color: #008080">substr</span>(<span style="color: #800080">$buff</span>,<span style="color: #008080">strlen</span>(<span style="color: #800080">$buff</span>)-1<span style="color: #000000">);
}
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$reqPar</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> MakeSign(<span style="color: #800080">$unifiedorder</span><span style="color: #000000">)
{
</span><span style="color: #800080">$this</span>->values=<span style="color: #800080">$unifiedorder</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">签名步骤一:按字典序排序参数
// ksort($this->values);</span>
<span style="color: #800080">$string</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">ToUrlParams();
</span><span style="color: #008000">//</span><span style="color: #008000">halt($string);
//签名步骤二:在string后加入KEY</span>
<span style="color: #800080">$string</span> = <span style="color: #800080">$string</span> . "&key=".<span style="color: #800080">$this</span>-><span style="color: #008080">key</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">签名步骤三:MD5加密</span>
<span style="color: #800080">$string</span> = <span style="color: #008080">md5</span>(<span style="color: #800080">$string</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">签名步骤四:所有字符转为大写</span>
<span style="color: #800080">$result</span> = <span style="color: #008080">strtoupper</span>(<span style="color: #800080">$string</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$result</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> ToUrlParams()
{
</span><span style="color: #800080">$buff</span> = ""<span style="color: #000000">;
</span><span style="color: #0000ff">foreach</span> (<span style="color: #800080">$this</span>->values <span style="color: #0000ff">as</span> <span style="color: #800080">$k</span> => <span style="color: #800080">$v</span><span style="color: #000000">)
{
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$k</span> != "sign" && <span style="color: #800080">$v</span> != "" && !<span style="color: #008080">is_array</span>(<span style="color: #800080">$v</span><span style="color: #000000">)){
</span><span style="color: #800080">$buff</span> .= <span style="color: #800080">$k</span> . "=" . <span style="color: #800080">$v</span> . "&"<span style="color: #000000">;
}
}
</span><span style="color: #800080">$buff</span> = <span style="color: #008080">trim</span>(<span style="color: #800080">$buff</span>,"&"<span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$buff</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">function</span> array2xml(<span style="color: #800080">$array</span><span style="color: #000000">)
{
</span><span style="color: #800080">$xml</span>='<xml>'<span style="color: #000000">;
</span><span style="color: #0000ff">foreach</span>(<span style="color: #800080">$array</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$key</span>=><span style="color: #800080">$val</span><span style="color: #000000">){
</span><span style="color: #0000ff">if</span>(<span style="color: #008080">is_numeric</span>(<span style="color: #800080">$key</span><span style="color: #000000">)){
</span><span style="color: #800080">$key</span>="item id=\"<span style="color: #800080">$key</span>\""<span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #008000">//</span><span style="color: #008000">去掉空格,只取空格之前<a href="/tag/wenzi/" target="_blank" class="keywords">文字</a>为key</span>
<span style="color: #0000ff">list</span>(<span style="color: #800080">$key</span>,)=<span style="color: #008080">explode</span>(' ',<span style="color: #800080">$key</span><span style="color: #000000">);
}
</span><span style="color: #800080">$xml</span>.="<<span style="color: #800080">$key</span>>"<span style="color: #000000">;
</span><span style="color: #800080">$xml</span>.=<span style="color: #008080">is_array</span>(<span style="color: #800080">$val</span>)?<span style="color: #800080">$this</span>->_array2xml(<span style="color: #800080">$val</span>):<span style="color: #800080">$val</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">去掉空格,只取空格之前<a href="/tag/wenzi/" target="_blank" class="keywords">文字</a>为key</span>
<span style="color: #0000ff">list</span>(<span style="color: #800080">$key</span>,<span style="color: #800080">$key</span><span style="color: #000000">);
</span><span style="color: #800080">$xml</span>.="</<span style="color: #800080">$key</span>>"<span style="color: #000000">;
}
</span><span style="color: #800080">$xml</span>.="</xml>"<span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$xml</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">function</span> xml2array(<span style="color: #800080">$xml</span><span style="color: #000000">)
{
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="/tag/jinzhi/" target="_blank" class="keywords">禁止</a>引用外部xml实体</span>
libxml_disable_entity_loader(<span style="color: #0000ff">true</span><span style="color: #000000">);
</span><span style="color: #800080">$values</span> = json_decode(json_encode(<span style="color: #008080">simplexml_load_string</span>(<span style="color: #800080">$xml</span>,<span style="color: #0000ff">true</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$values</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> request_post(<span style="color: #800080">$url</span> = '',<span style="color: #800080">$param</span> = ''<span style="color: #000000">)
{
</span><span style="color: #0000ff">if</span> (<span style="color: #0000ff">empty</span>(<span style="color: #800080">$url</span>) || <span style="color: #0000ff">empty</span>(<span style="color: #800080">$param</span><span style="color: #000000">)) {
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
}
</span><span style="color: #800080">$postUrl</span> = <span style="color: #800080">$url</span><span style="color: #000000">;
</span><span style="color: #800080">$curlPost</span> = <span style="color: #800080">$param</span><span style="color: #000000">;
</span><span style="color: #800080">$ch</span> = curl_init(); <span style="color: #008000">//</span><span style="color: #008000">初始化curl</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #800080">$postUrl</span>); <span style="color: #008000">//</span><span style="color: #008000">抓取指定网页</span>
curl_setopt(<span style="color: #800080">$ch</span>,0); <span style="color: #008000">//</span><span style="color: #008000">设置header</span>
curl_setopt(<span style="color: #800080">$ch</span>,1); <span style="color: #008000">//</span><span style="color: #008000">要求结果为字符串且<a href="/tag/shuchu/" target="_blank" class="keywords">输出</a>到屏幕上</span>
curl_setopt(<span style="color: #800080">$ch</span>,1); <span style="color: #008000">//</span><span style="color: #008000">post提交方式</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #800080">$curlPost</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span> = curl_exec(<span style="color: #800080">$ch</span>); <span style="color: #008000">//</span><span style="color: #008000">运行curl</span>
curl_close(<span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">function</span> curl_post_ssl(<span style="color: #800080">$url</span>,<span style="color: #800080">$vars</span>,<span style="color: #800080">$second</span>=30,<span style="color: #800080">$aHeader</span>=<span style="color: #0000ff">array</span><span style="color: #000000">())
{
</span><span style="color: #800080">$ch</span> =<span style="color: #000000"> curl_init();
</span><span style="color: #008000">//</span><span style="color: #008000">curl_setopt($ch,CURLOPT_VERBOSE,'1');</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #800080">$second</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,1<span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,<span style="color: #0000ff">false</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSLCERT,'/data/cert/<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>.pem'<span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSLCERTPASSWD,'1234'<span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSLKEYTYPE,CURLOPT_SSLKEY,'/data/cert/<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>_private.pem'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span>( <span style="color: #008080">count</span>(<span style="color: #800080">$aHeader</span>) >= 1<span style="color: #000000"> ){
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_HTTPHEADER,<span style="color: #800080">$aHeader</span><span style="color: #000000">);
}
curl_setopt(</span><span style="color: #800080">$ch</span>,<span style="color: #800080">$vars</span><span style="color: #000000">);
</span><span style="color: #800080">$data</span> = curl_exec(<span style="color: #800080">$ch</span><span style="color: #000000">);
curl_close(</span><span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$data</span><span style="color: #000000">){
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$data</span><span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
}
}
}
Alipay.PHP
<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">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$partner</span> = "",<span style="color: #800080">$md5_key</span> = ""<span style="color: #000000">)
{
</span><span style="color: #800080">$this</span>->partner = <span style="color: #800080">$partner</span><span style="color: #000000">;
</span><span style="color: #800080">$this</span>->md5_key = <span style="color: #800080">$md5_key</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #800080">$alipay_config</span> =<span style="color: #000000"> [
</span><span style="color: #008000">//</span><span style="color: #008000"> '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">//</span><span style="color: #008000">https://docs.open.alipay.com/api_1 看你需要哪个API接口</span>
'anti_phishing_key' => '','exter_invoke_ip' => '','app_pay' => 'Y',<span style="color: #000000">
];
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> alipay(<span style="color: #800080">$data</span> =<span style="color: #000000"> [])
{</span><span style="color: #008000">//</span><span style="color: #008000">发起支付宝支付</span>
<span style="color: #800080">$validate</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Validate([
[</span>'out_trade_no','require|alphaNum','订单编号输入<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>1|订单编号输入<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>2'],<span style="color: #000000">
[</span>'total_fee','require|number|gt:0','金额输入<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>|金额输入<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>|金额输入<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>'],<span style="color: #000000">
[</span>'subject','require','请输入<a href="/tag/biaoti/" target="_blank" class="keywords">标题</a>'],<span style="color: #000000">
[</span>'body','请输入描述'],<span style="color: #000000">
[</span>'notify_url','异步<a href="/tag/tongzhi/" target="_blank" class="keywords">通知</a>地址不为空'],<span style="color: #000000">
]);
</span><span style="color: #0000ff">if</span> (!<span style="color: #800080">$validate</span>->check(<span style="color: #800080">$data</span><span style="color: #000000">)) {
</span><span style="color: #0000ff">return</span> ['code' => 0,'msg' => <span style="color: #800080">$validate</span>-><span style="color: #000000">getError()];
}
</span><span style="color: #800080">$config</span> = self::<span style="color: #800080">$alipay_config</span><span style="color: #000000">;
</span><span style="color: #800080">$config</span>['partner'] = <span style="color: #800080">$this</span>-><span style="color: #000000">partner;
</span><span style="color: #800080">$config</span>['seller_id'] = <span style="color: #800080">$this</span>-><span style="color: #000000">partner;
</span><span style="color: #800080">$config</span>['key'] = <span style="color: #800080">$this</span>-><span style="color: #000000">md5_key;
vendor(</span>'alipay.alipay'<span style="color: #000000">);
</span><span style="color: #800080">$parameter</span> =<span style="color: #000000"> [
</span>"service" => <span style="color: #800080">$config</span>['service'],"partner" => <span style="color: #800080">$config</span>['partner'],"seller_id" => <span style="color: #800080">$config</span>['seller_id'],"payment_type" => <span style="color: #800080">$config</span>['payment_type'],"notify_url" => <span style="color: #800080">$data</span>['notify_url'],"return_url" => <span style="color: #800080">$data</span>['return_url'],"anti_phishing_key" => <span style="color: #800080">$config</span>['anti_phishing_key'],"exter_invoke_ip" => <span style="color: #800080">$config</span>['exter_invoke_ip'],"out_trade_no" => <span style="color: #800080">$data</span>['out_trade_no'],"subject" => <span style="color: #800080">$data</span>['subject'],"total_fee" => <span style="color: #800080">$data</span>['total_fee'],"body" => <span style="color: #800080">$data</span>['body'],"_input_charset" => <span style="color: #800080">$config</span>['input_charset'],<span style="color: #000000">
];
</span><span style="color: #800080">$alipaySubmit</span> = <span style="color: #0000ff">new</span> AlipaySubmit(<span style="color: #800080">$config</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> ['code' => 1,'msg' => <span style="color: #800080">$alipaySubmit</span>->buildRequestForm(<span style="color: #800080">$parameter</span>,"get","确认"<span style="color: #000000">)];
}
}
?>
wx.html在使用微信客户端支付时需要的页面
Email:361169632@qq.com注明出处: