yzms/show/wxpay_recv.php

150 lines
5.7 KiB
PHP

<?
if(!$wxpay_recvxml) {
require_once(dirname(__FILE__)."/common.php");
$xmls = file_get_contents("php://input");
} else {
$xmls = $wxpay_recvxml;
}
/*$xmls = "
<xml><appid><![CDATA[wx426b3015555a46be]]></appid>
<bank_type><![CDATA[CFT]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[1900009851]]></mch_id>
<nonce_str><![CDATA[e3f706fd27cd18b1bbf35ba694338ad5]]></nonce_str>
<openid><![CDATA[oHZx6uBiKW-vf-RPqr3UYJslWgQQ]]></openid>
<out_trade_no><![CDATA[572197987712]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[25F58F2EE0A837F5EC7B075D1D80AD11]]></sign>
<time_end><![CDATA[20170321103822]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[NATIVE]]></trade_type>
<transaction_id><![CDATA[4004242001201703214106194791]]></transaction_id>
</xml>
";*/
//if(!$xmls) exit;
file_put_contents("wxpay.log", date("Y-m-d H:i:s").": \n".$xmls."\n\n", FILE_APPEND);
$xml = simplexml_load_string($xmls);
if(!$xml) exit;
$data = array();
foreach($xml->children() as $name => $node) {
$data[$name.""] = $node."";
}
if(!$data) exit;
if(!$wxpay_key) {
$wxpay_key = 'a8555efc5395420ae8c62483f8732f84';
$out_trade_no = $data['out_trade_no'];
if($out_trade_no) {
$ddInfo = $db->get_one("select a.*, c.company_name, c.wxpay_key from tb_recharge_pay_weixin a, tb_user b, tb_company c where a.uid=b.id and b.company_id=c.id and a.code = '{$out_trade_no}'");
if(!$ddInfo) exit;
$wxpay_key = $ddInfo['wxpay_key'];
}
}
$db->query("insert into tb_recharge_pay_weixin_recv set bank_type='".$data['bank_type']."',cash_fee='".$data['cash_fee']."',fee_type='".$data['fee_type']."',is_subscribe='".$data['is_subscribe']."',openid='".$data['openid']."',out_trade_no='".$data['out_trade_no']."',result_code='".$data['result_code']."',return_code='".$data['return_code']."',sign='".$data['sign']."',time_end='".$data['time_end']."',total_fee='".$data['total_fee']."',trade_type='".$data['trade_type']."',transaction_id='".$data['transaction_id']."',trade_state='".$data['trade_state']."',trade_state_desc='".addslashes($data['trade_state_desc'])."',err_code='".$data['err_code']."',err_code_des='".$data['err_code_des']."',addtime=now()");
$sign = $data['sign'];
if(!$sign) exit;
unset($data['sign']);
ksort($data);
$s1 = '';
foreach($data as $key => $item) {
if($item != '') {
if($s1 != '') $s1 .= "&";
$s1 .= $key."=".$item;
}
}
$s = $s1. "&key=".$wxpay_key;
$sign2 = strtoupper(md5($s));
if($sign != $sign2) exit;
$code = $data['out_trade_no'];
$orderInfo = $db->get_one("select * from tb_recharge_pay_weixin where code = '{$code}'");
if(!$orderInfo) exit;
if($orderInfo['state'] == 1) {
if(!$wxrecv_isinclude) {
echo "
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>
";
}
exit;
}
$total_fee = round($orderInfo['total_fee']*100);
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS' && $data['transaction_id'] && (!$data['trade_state'] || $data['trade_state'] == 'SUCCESS') && $total_fee == $data['total_fee']) {
$db->query("insert into tb_recharge_pay_suc set ORDERSEQ='{$code}', AMOUNT='".$orderInfo['total_fee']."', addtime=now()", 'SILENT');
if($db->affected_rows() == 1) {
require_once("include/pay.inc.php");
//gpay_add_user_account_uid($orderInfo['uid'], $orderInfo['total_fee'], 0, $orderInfo['id'], 3, 1, 2, 0);
$uInfo = $db->get_one("select * from tb_user where id = ".$orderInfo['uid']);
$company_id = $uInfo['company_id'];
$dining_hall_id = $uInfo['dining_hall_id'];
$payInfo = array(
'oid' => $orderInfo['id'],
'rtype' => 1,
'ac_type' => 2,
'recharge_fund_type' => 3,
);
if($company_id == 58) { //韶关小岛考虑supplier_id
$supplier_id = 0;
$dInfo = $db->get_one("select * from tb_dining_hall where id = ".intval($dining_hall_id));
if($dInfo) $supplier_id = intval($dInfo['supplier_id']);
$payInfo['supplier_id'] = $supplier_id;
}
$rs = gpay_add_user_account_uid($orderInfo['uid'], $orderInfo['total_fee'], $payInfo);
if($rs == 'suc'){
// 查询充值是否有赠送金额
$giveUrl = "https://yzms.fsecity.com/api/recharge/get_give_fee";
// 获取用户token
$userSession = $db->get_one("select * from tb_session where uid = ".$orderInfo['uid'] . " order by expire_time desc");
$giveApiFee = $data['total_fee']/100;
$giveInfo = gquery($giveUrl, array("token"=>$userSession['token'],"fee"=>$giveApiFee));
$giveInfo = json_decode($giveInfo, 1);
if($giveInfo['status'] == 1){
$giveFee = $giveInfo['data']['gift_fee'];
if($giveFee > 0){
$rechargePayInfo = array(
'admin_id' => 1,
'rtype' => 1,
'ac_type' => 1,
'notes' => '充值赠送',
);
if(isset($supplier_id) && !empty($supplier_id)){
$rechargePayInfo['supplier_id'] = $supplier_id;
}
$r = gpay_add_user_account_uid($orderInfo['uid'], floatval($giveFee), $rechargePayInfo);
}
}
}
$db->query("update tb_recharge_pay_weixin set state=1,suctime=now(),transaction_id='".$data['transaction_id']."' where id=".$orderInfo['id']);
//file_put_contents("1.txt", $_SERVER["HTTP_USER_AGENT"].": ".$UPTRANSEQ."\n\n\n", FILE_APPEND);
if(!$wxrecv_isinclude) {
echo "
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>
";
}
}
} else {
$db->query("update tb_recharge_pay_weixin set state=2 where id=".$orderInfo['id']);
}