~~~
<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Cache;
use think\Loader;
use qrcode\QRcode;
class Gupay extends Controller
{
public function index(){
header("Content-Type:text/html; charset=utf-8");
$ip=$_SERVER["REMOTE_ADDR"];
$n_rl="http://www.muzuinky.cn/api/Gupay/notify";
$key="0180437CED486F857078875D3039CC59";
$trade_type="NATIVE";
$out_trade_no='longtai'.time();
//充值表
Db::table('wp_balance')->where('bpid',$_GET[ 'bpid'])->setField('balance_sn',$out_trade_no);
$mch_id="100004383076";
$money1=$_GET[ 'money']*100;
//$money1=1;
//$ip="127.0.0.1";
$str="attach=ATTACH&body=测试支付&detail=DETAIL&mch_id=".$mch_id."&nonce_str=1409196838¬ify_url=".$n_rl."&out_trade_no=".$out_trade_no."&spbill_create_ip=".$ip."&total_fee=".$money1."&trade_type=".$trade_type."&key=".$key;
$str1= strtoupper(md5($str));
//echo $str1;exit();
//构造xml
$xmldata = <<<EOF
<?xml version='1.0' encoding='UTF-8'?>
<xml>
<mch_id><![CDATA[$mch_id]]></mch_id>
<total_fee><![CDATA[$money1]]></total_fee>
<out_trade_no><![CDATA[$out_trade_no]]></out_trade_no>
<body><![CDATA[测试支付]]></body>
<attach><![CDATA[ATTACH]]></attach>
<detail><![CDATA[DETAIL]]></detail>
<spbill_create_ip><![CDATA[$ip]]></spbill_create_ip>
<notify_url><![CDATA[$n_rl]]></notify_url>
<nonce_str><![CDATA[1409196838]]></nonce_str>
<trade_type><![CDATA[$trade_type]]></trade_type>
<sign><![CDATA[$str1]]></sign>
</xml>
EOF;
//初始化curl会话
$ch = curl_init();
//设置url
curl_setopt($ch, CURLOPT_URL, 'http://mapi.bosc.uline.cc/wechat/orders');
//设置发送方式
curl_setopt($ch, CURLOPT_POST, true);
//设置发送的数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//禁止curl资源直接输出
// 3.抓取url并把它传递给服务器
$opt=curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);
//转换为simplexml对象
$objectxml = simplexml_load_string($opt);//将文件转换成 对象
$xmljson= json_encode($objectxml );//将对象转换个JSON
$xmlarray=json_decode($xmljson,true);//将json转换成数组
$this->assign('data',$xmlarray);
$this->assign('money',$_GET[ 'money']);
$this->assign('out_trade_no',$out_trade_no);
return $this->fetch('index');
}
public function query(){
$res = $this->request->param();
//获取请求订单参数
$sn=$res['out_trade_no'];
$ispay=Db::table('wp_balance')->where('balance_sn',$sn)->value('bptype');
if($ispay==1){
return ['status'=>1, 'message'=>'支付成功'];
}else{
return ['status'=>0, 'message'=>'支付失败'];
}
}
public function refurn(){
return $this->fetch('refurn');
}
public function notify(){
$postStr = file_get_contents("php://input"); //接收post数据(xml数据)
$param="";
if (isset($postStr)) {
$objectxml = simplexml_load_string($postStr); //将文件转换成 对象
$xmljson= json_encode($objectxml );//将对象转换个JSON
$param=json_decode($xmljson,true);//将json转换成数组
if($param['return_code']=="SUCCESS"&& $param['result_code']=="SUCCESS"){
$notify_fee =$param["total_fee"]/100; //实际支付金额
$times =time(); //支付时间
$sdorderno=$param["out_trade_no"]; //用户自己的订单号
$ispay=Db::table('wp_balance')->where('balance_sn',$sdorderno)->value('bptype');
if($ispay==1){
exit("SUCCESS");
}else{
$uid=Db::table('wp_balance')->where('balance_sn',$sdorderno)->value('uid');
$money=Db::table('wp_userinfo')->where('uid',$uid)->value('usermoney');
$money1=$money+$notify_fee;
//用户表
$res1= Db::table('wp_userinfo')->where('uid',$uid)->setField('usermoney',$money1);
if($res1!=0){
//充值表
$aaaa=['isverified'=>1,'cltime'=>$times,'bptype'=>1,'bptime'=>$times,'bpprice'=>$notify_fee,'remarks'=>'会员充值','bpbalance'=>$money1,'btime'=>$times,'reg_par'=>0];
$res2= Db::table('wp_balance')->where('balance_sn',$sdorderno)->update($aaaa);
if($res2!=0){
exit("SUCCESS");
//echo "SUCCESS";
}
}
}
}
}
$this->writelog('--post='.json_encode($param),'guma11');
}
public function writelog($text, $aType='')
{
$text = $this->characet1($text);
file_put_contents (dirname ( __FILE__ )."/fb1log_".$aType._. date( "Y-m-d" ).".txt", date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
}
function characet1($data)
{
if (! empty ( $data ))
{
$fileType = mb_detect_encoding ( $data, array (
'UTF-8',
'GBK',
'GB2312',
'LATIN1',
'BIG5'
) );
if ($fileType != 'UTF-8')
{
$data = mb_convert_encoding ( $data, 'UTF-8', $fileType );
}
}
return $data;
}
}
~~~