相对于之前随笔写的来说,PHP对接app微信支付功能就相对简单的多了,最近有加我的朋友问到app微信支付,所以我把app微信支付的demo展示出来供大家参考一下,希望对做这个功能的人员给予参考和帮助,
首先还是到支付页面:
微信支付的步骤大致和支付宝支付的步骤差不多,这里我讲的详细一点。
第一步:建立一个支付控制器pay.PHP,是由上个页面选择好支付方式之后确认支付后需要经过的控制器(这里选择的是微信支付),代码 如下:
PHP
namespace app\mobile\controller;
<span style="color: #0000ff">class Pay <span style="color: #0000ff">extends<span style="color: #000000"> Controller
{
<span style="color: #0000ff">public <span style="color: #0000ff">function<span style="color: #000000"> pay_order()
{
<span style="color: #800080">$res = <span style="color: #0000ff">new<span style="color: #000000"> OrderGoods();
<span style="color: #008000">//<span style="color: #008000">获取订单号
<span style="color: #800080">$where['id'] = input('post.order_sn'<span style="color: #000000">);
<span style="color: #800080">$reoderSn = input('post.order_sn'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">查询订单信息
<span style="color: #800080">$order_info = <span style="color: #800080">$res->where(<span style="color: #800080">$where)-><span style="color: #000000">find();
<span style="color: #008000">//<span style="color: #008000">获取支付方式
<span style="color: #800080">$pay_type = input('post.pay_type');<span style="color: #008000">//<span style="color: #008000">微信支付 或者支付宝支付
//获取支付金额
<span style="color: #800080">$money = input('post.totle_sum'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">判断支付方式
<span style="color: #0000ff">switch (<span style="color: #800080">$pay_type<span style="color: #000000">) {
<span style="color: #0000ff">case 'ali';<span style="color: #008000">//<span style="color: #008000">如果支付方式为支付宝支付
{
<span style="color: #0000ff">public <span style="color: #0000ff">function<span style="color: #000000"> pay_order()
{
<span style="color: #800080">$res = <span style="color: #0000ff">new<span style="color: #000000"> OrderGoods();
<span style="color: #008000">//<span style="color: #008000">获取订单号
<span style="color: #800080">$where['id'] = input('post.order_sn'<span style="color: #000000">);
<span style="color: #800080">$reoderSn = input('post.order_sn'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">查询订单信息
<span style="color: #800080">$order_info = <span style="color: #800080">$res->where(<span style="color: #800080">$where)-><span style="color: #000000">find();
<span style="color: #008000">//<span style="color: #008000">获取支付方式
<span style="color: #800080">$pay_type = input('post.pay_type');<span style="color: #008000">//<span style="color: #008000">微信支付 或者支付宝支付
//获取支付金额
<span style="color: #800080">$money = input('post.totle_sum'<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">判断支付方式
<span style="color: #0000ff">switch (<span style="color: #800080">$pay_type<span style="color: #000000">) {
<span style="color: #0000ff">case 'ali';<span style="color: #008000">//<span style="color: #008000">如果支付方式为支付宝支付
<span style="color: #0000ff">break</span><span style="color: #000000">;
</span><span style="color: #0000ff">case</span> 'wx'<span style="color: #000000">;
</span><span style="color: #800080">$type</span>['pay_type'] = 'wx';<span style="color: #008000">//</span><span style="color: #008000">更新支付方式为微信</span>
<span style="color: #800080">$res</span>->where(<span style="color: #800080">$where</span>)->update(<span style="color: #800080">$type</span><span style="color: #000000">);
</span><span style="color: #800080">$wx</span> = <span style="color: #0000ff">new</span> Wxpay();<span style="color: #008000">//</span><span style="color: #008000">实例化微信支付控制器</span>
<span style="color: #800080">$body</span> = '订单号' . <span style="color: #800080">$order_info</span>;<span style="color: #008000">//</span><span style="color: #008000">支付说明</span>
<span style="color: #800080">$out_trade_no</span> = <span style="color: #800080">$reoderSn</span>;<span style="color: #008000">//</span><span style="color: #008000">订单号</span>
<span style="color: #800080">$total_fee</span> = <span style="color: #800080">$money</span> * 100;<span style="color: #008000">//</span><span style="color: #008000">支付金额(乘以100)</span>
<span style="color: #800080">$notify_url</span> = '';<span style="color: #008000">//</span><span style="color: #008000">回调地址</span>
<span style="color: #800080">$order</span> = <span style="color: #800080">$wx</span>->getPrePayOrder(<span style="color: #800080">$body</span>,<span style="color: #800080">$out_trade_no</span>,<span style="color: #800080">$total_fee</span>,<span style="color: #800080">$notify_url</span>);<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>微信支付的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff">if</span> (<span style="color: #800080">$order</span>['prepay_id']){<span style="color: #008000">//</span><span style="color: #008000">判断返回参数中是否有prepay_id</span>
<span style="color: #800080">$order1</span> = <span style="color: #800080">$wx</span>->getOrder(<span style="color: #800080">$order</span>['prepay_id']);<span style="color: #008000">//</span><span style="color: #008000">执行二次签名返回参数</span>
<span style="color: #0000ff">echo</span> json_encode(<span style="color: #0000ff">array</span>('status' => 1,'prepay_order' => no_null(<span style="color: #800080">$order1</span><span style="color: #000000">)));
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #0000ff">echo</span> json_encode(<span style="color: #0000ff">array</span>('status' => 0,'msg' => <span style="color: #800080">$order</span>['err_code_des'<span style="color: #000000">]));
}
</span><span style="color: #0000ff">break</span><span style="color: #000000">;
}
}
}
然后创建微信支付控制器了。命名为wx.PHP,用于执行微信支付,代码如下:
PHP
namespace app\mobile\controller;
WxPay
= 'appid' => "",
'mch_id' => "",40493402",/*微信申请成功之后邮件中的商户id*/
'api_key' => ""
<span style="color: #0000ff">return xmlToArray(<span style="color: #800080">$response<span style="color: #000000">);
}
";
//签名步骤三:MD5加密
<span style="color: #800080">$result = <span style="color: #008080">strtoupper(<span style="color: #008080">md5(<span style="color: #800080">$String<span style="color: #000000">));
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
// return $this->domnode_to_array($doc->documentElement);
// }
<span style="color: #0000ff">function domnode_to_array(<span style="color: #800080">$node<span style="color: #000000">) {
<span style="color: #800080">$output = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">switch (<span style="color: #800080">$node-><span style="color: #000000">nodeType) {
<span style="color: #0000ff">case XML_CDATA_SECTION_NODE:
<span style="color: #0000ff">case XML_TEXT_NODE:
<span style="color: #800080">$output = <span style="color: #008080">trim(<span style="color: #800080">$node-><span style="color: #000000">textContent);
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case XML_ELEMENT_NODE:
<span style="color: #0000ff">for (<span style="color: #800080">$i=0,<span style="color: #800080">$m=<span style="color: #800080">$node->childNodes->length; <span style="color: #800080">$i<<span style="color: #800080">$m; <span style="color: #800080">$i++<span style="color: #000000">) {
<span style="color: #800080">$child = <span style="color: #800080">$node->childNodes->item(<span style="color: #800080">$i<span style="color: #000000">);
<span style="color: #800080">$v = <span style="color: #800080">$this->domnode_to_array(<span style="color: #800080">$child<span style="color: #000000">);
<span style="color: #0000ff">if(<span style="color: #0000ff">isset(<span style="color: #800080">$child-><span style="color: #000000">tagName)) {
<span style="color: #800080">$t = <span style="color: #800080">$child-><span style="color: #000000">tagName;
<span style="color: #0000ff">if(!<span style="color: #0000ff">isset(<span style="color: #800080">$output[<span style="color: #800080">$t<span style="color: #000000">])) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #0000ff">array<span style="color: #000000">();
}
<span style="color: #800080">$output[<span style="color: #800080">$t][] = <span style="color: #800080">$v<span style="color: #000000">;
}
<span style="color: #0000ff">elseif(<span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #800080">$output = (<span style="color: #0000ff">string) <span style="color: #800080">$v<span style="color: #000000">;
}
}
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$output<span style="color: #000000">)) {
<span style="color: #0000ff">if(<span style="color: #800080">$node->attributes-><span style="color: #000000">length) {
<span style="color: #800080">$a = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">foreach(<span style="color: #800080">$node->attributes <span style="color: #0000ff">as <span style="color: #800080">$attrName => <span style="color: #800080">$attrNode<span style="color: #000000">) {
<span style="color: #800080">$a[<span style="color: #800080">$attrName] = (<span style="color: #0000ff">string) <span style="color: #800080">$attrNode-><span style="color: #000000">value;
}
<span style="color: #800080">$output['@attributes'] = <span style="color: #800080">$a<span style="color: #000000">;
}
<span style="color: #0000ff">foreach (<span style="color: #800080">$output <span style="color: #0000ff">as <span style="color: #800080">$t => <span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$v) && <span style="color: #008080">count(<span style="color: #800080">$v)==1 && <span style="color: #800080">$t!='@attributes'<span style="color: #000000">) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #800080">$v[0<span style="color: #000000">];
}
}
}
<span style="color: #0000ff">break<span style="color: #000000">;
}
<span style="color: #0000ff">return <span style="color: #800080">$output<span style="color: #000000">;
}
}
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>预支付订单</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> getPrePayOrder(<span style="color: #800080">$body</span>,<span style="color: #800080">$notify_url</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">$onoce_str</span> = <span style="color: #800080">$this</span>->getRandChar(32<span style="color: #000000">);
</span><span style="color: #800080">$data</span>["appid"] = <span style="color: #800080">$this</span>->config["appid"<span style="color: #000000">];
</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>->config['mch_id'<span style="color: #000000">];
</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">$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: #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转成数组
// return $this->xmlstr_to_array($response);
<span style="color: #0000ff">return xmlToArray(<span style="color: #800080">$response<span style="color: #000000">);
}
</span><span style="color: #008000">//</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>->config["appid"<span style="color: #000000">];
</span><span style="color: #800080">$data</span>["noncestr"] = <span style="color: #800080">$this</span>->getRandChar(32<span style="color: #000000">);;
</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>->config['mch_id'<span style="color: #000000">];
</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="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>签名
</span><span style="color: #008000">*/</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: #008080">strtolower</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 "【string】 =".$String."@H_<a href="https://www.jb51.cc/tag/502/" target="_blank" class="keywords">502</a>_263@";
//签名步骤二:在string后加入KEY</span>
<span style="color: #800080">$String</span> = <span style="color: #800080">$String</span>."&key=".<span style="color: #800080">$this</span>->config['api_key'<span style="color: #000000">];
<span style="color: #008000">//<span style="color: #008000"> echo " ";
//签名步骤三:MD5加密
<span style="color: #800080">$result = <span style="color: #008080">strtoupper(<span style="color: #008080">md5(<span style="color: #800080">$String<span style="color: #000000">));
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>指定长度的<a href="https://www.jb51.cc/tag/suiji/" target="_blank" class="keywords">随机</a>字符串</span>
<span style="color: #0000ff">function</span> getRandChar(<span style="color: #800080">$length</span><span style="color: #000000">){
</span><span style="color: #800080">$str</span> = <span style="color: #0000ff">null</span><span style="color: #000000">;
</span><span style="color: #800080">$strPol</span> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"<span style="color: #000000">;
</span><span style="color: #800080">$max</span> = <span style="color: #008080">strlen</span>(<span style="color: #800080">$strPol</span>)-1<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: #800080">$strPol</span>[<span style="color: #008080">rand</span>(0,<span style="color: #800080">$max</span>)];<span style="color: #008000">//</span><span style="color: #008000">rand($min,$max)<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>介于min和max两个数之间的一个<a href="https://www.jb51.cc/tag/suiji/" target="_blank" class="keywords">随机</a>整数</span>
<span style="color: #000000"> }
</span><span style="color: #0000ff">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">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: #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">post https请求,CURLOPT_POSTFIELDS xml格式</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>,CURLOPT_URL,<span style="color: #800080">$url</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSL_VERIFYPEER,<span style="color: #0000ff">FALSE</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_SSL_VERIFYHOST,<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="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>到屏幕上</span>
curl_setopt(<span style="color: #800080">$ch</span>,CURLOPT_RETURNTRANSFER,<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="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>码:<span style="color: #800080">$error</span>"."<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a>@H_<a href="https://www.jb51.cc/tag/502/" target="_blank" class="keywords">502</a>_263@"<span style="color: #000000">;
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="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>当前服务器的IP
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> get_client_ip()
{
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000">]) {
</span><span style="color: #800080">$cip</span> = <span style="color: #800080">$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000">];
} </span><span style="color: #0000ff">elseif</span> (<span style="color: #008080">getenv</span>("REMOTE_ADDR"<span style="color: #000000">)) {
</span><span style="color: #800080">$cip</span> = <span style="color: #008080">getenv</span>("REMOTE_ADDR"<span style="color: #000000">);
} </span><span style="color: #0000ff">elseif</span> (<span style="color: #008080">getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000">)) {
</span><span style="color: #800080">$cip</span> = <span style="color: #008080">getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000">);
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #800080">$cip</span> = "unknown"<span style="color: #000000">;
}
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$cip</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">将数组转成uri字符串</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: #008080">strtolower</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: #008000">/*</span><span style="color: #008000">*
xml转成数组
</span><span style="color: #008000">*/</span>
<span style="color: #008000">//
@H639502@loadXML($xmlstr);// return $this->domnode_to_array($doc->documentElement);
// }
<span style="color: #0000ff">function domnode_to_array(<span style="color: #800080">$node<span style="color: #000000">) {
<span style="color: #800080">$output = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">switch (<span style="color: #800080">$node-><span style="color: #000000">nodeType) {
<span style="color: #0000ff">case XML_CDATA_SECTION_NODE:
<span style="color: #0000ff">case XML_TEXT_NODE:
<span style="color: #800080">$output = <span style="color: #008080">trim(<span style="color: #800080">$node-><span style="color: #000000">textContent);
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case XML_ELEMENT_NODE:
<span style="color: #0000ff">for (<span style="color: #800080">$i=0,<span style="color: #800080">$m=<span style="color: #800080">$node->childNodes->length; <span style="color: #800080">$i<<span style="color: #800080">$m; <span style="color: #800080">$i++<span style="color: #000000">) {
<span style="color: #800080">$child = <span style="color: #800080">$node->childNodes->item(<span style="color: #800080">$i<span style="color: #000000">);
<span style="color: #800080">$v = <span style="color: #800080">$this->domnode_to_array(<span style="color: #800080">$child<span style="color: #000000">);
<span style="color: #0000ff">if(<span style="color: #0000ff">isset(<span style="color: #800080">$child-><span style="color: #000000">tagName)) {
<span style="color: #800080">$t = <span style="color: #800080">$child-><span style="color: #000000">tagName;
<span style="color: #0000ff">if(!<span style="color: #0000ff">isset(<span style="color: #800080">$output[<span style="color: #800080">$t<span style="color: #000000">])) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #0000ff">array<span style="color: #000000">();
}
<span style="color: #800080">$output[<span style="color: #800080">$t][] = <span style="color: #800080">$v<span style="color: #000000">;
}
<span style="color: #0000ff">elseif(<span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #800080">$output = (<span style="color: #0000ff">string) <span style="color: #800080">$v<span style="color: #000000">;
}
}
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$output<span style="color: #000000">)) {
<span style="color: #0000ff">if(<span style="color: #800080">$node->attributes-><span style="color: #000000">length) {
<span style="color: #800080">$a = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #0000ff">foreach(<span style="color: #800080">$node->attributes <span style="color: #0000ff">as <span style="color: #800080">$attrName => <span style="color: #800080">$attrNode<span style="color: #000000">) {
<span style="color: #800080">$a[<span style="color: #800080">$attrName] = (<span style="color: #0000ff">string) <span style="color: #800080">$attrNode-><span style="color: #000000">value;
}
<span style="color: #800080">$output['@attributes'] = <span style="color: #800080">$a<span style="color: #000000">;
}
<span style="color: #0000ff">foreach (<span style="color: #800080">$output <span style="color: #0000ff">as <span style="color: #800080">$t => <span style="color: #800080">$v<span style="color: #000000">) {
<span style="color: #0000ff">if(<span style="color: #008080">is_array(<span style="color: #800080">$v) && <span style="color: #008080">count(<span style="color: #800080">$v)==1 && <span style="color: #800080">$t!='@attributes'<span style="color: #000000">) {
<span style="color: #800080">$output[<span style="color: #800080">$t] = <span style="color: #800080">$v[0<span style="color: #000000">];
}
}
}
<span style="color: #0000ff">break<span style="color: #000000">;
}
<span style="color: #0000ff">return <span style="color: #800080">$output<span style="color: #000000">;
}
}
微信支付控制器里面基本不用动,只需要改几个参数就行了。很简单。
回调控制器和方法我这里就不写了,很简单的,不懂得可以私信。
流程已经没了,执行一下程序,看一下返回给前端的参数示例:
把这些参数通过和前端交互给到前端就行了,接下来就是前端拿着参数去进行支付,支付成功系统会自动调用你写的回调地址改写你所需要的代码逻辑。
至于微信控制器wx.PHP存放的位置参考之前的那个app支付宝支付文章就行了,一样的。希望以上说明对需要做这个的有所帮助。
内容,为了尊重他人劳动,转载请注明本文地址:
原文链接:https://www.f2er.com/thinkphp/71862.html