Commit 9b3c96ab by 冷斌

fix bug

parent 613d1de2
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace Appapi\Controller; namespace Appapi\Controller;
use Common\Controller\HomebaseController; use Common\Controller\HomebaseController;
/** /**
* 支付回调 * 支付回调
*/ */
class PayController extends HomebaseController { class PayController extends HomebaseController {
private $wxDate = null; private $wxDate = null;
//支付宝 回调 //支付宝 回调
public function notify_ali() { public function notify_ali() {
...@@ -34,36 +34,36 @@ class PayController extends HomebaseController { ...@@ -34,36 +34,36 @@ class PayController extends HomebaseController {
$trade_no = $_POST['trade_no']; $trade_no = $_POST['trade_no'];
//交易状态 //交易状态
$trade_status = $_POST['trade_status']; $trade_status = $_POST['trade_status'];
//交易金额 //交易金额
$total_fee = $_POST['total_fee']; $total_fee = $_POST['total_fee'];
if($_POST['trade_status'] == 'TRADE_FINISHED') { if($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理 //判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序 //如果有做过处理,不执行商户的业务程序
//注意: //注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//调试用,写文本函数记录程序运行情况是否正常 //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
}else if ($_POST['trade_status'] == 'TRADE_SUCCESS') { }else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理 //判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序 //如果有做过处理,不执行商户的业务程序
//注意: //注意:
//付款完成后,支付宝系统发送该交易状态通知 //付款完成后,支付宝系统发送该交易状态通知
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//调试用,写文本函数记录程序运行情况是否正常 //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
$orderinfo=M("users_charge")->where("orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='1'")->find(); $orderinfo=M("users_charge")->where("orderno='{$out_trade_no}' and money='{$total_fee}' and status='0' and type='1'")->find();
$this->logali("orderinfo:".json_encode($orderinfo)); $this->logali("orderinfo:".json_encode($orderinfo));
if($orderinfo){ if($orderinfo){
/* 更新会员虚拟币 */ /* 更新会员虚拟币 */
$coin=$orderinfo['coin']+$orderinfo['coin_give']; $coin=$orderinfo['coin']+$orderinfo['coin_give'];
...@@ -71,37 +71,37 @@ class PayController extends HomebaseController { ...@@ -71,37 +71,37 @@ class PayController extends HomebaseController {
/* 更新 订单状态 */ /* 更新 订单状态 */
M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$trade_no)); M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$trade_no));
$this->logali("成功"); $this->logali("成功");
echo "success"; //请不要修改或删除 echo "success"; //请不要修改或删除
exit; exit;
}else{ }else{
$this->logali("orderno:".$out_trade_no.' 订单信息不存在'); $this->logali("orderno:".$out_trade_no.' 订单信息不存在');
} }
} }
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
echo "fail"; //请不要修改或删除 echo "fail"; //请不要修改或删除
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}else { }else {
$this->logali("验证失败"); $this->logali("验证失败");
//验证失败 //验证失败
echo "fail"; echo "fail";
//调试用,写文本函数记录程序运行情况是否正常 //调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
} }
} }
/* 支付宝支付 */ /* 支付宝支付 */
/* 微信支付 */ /* 微信支付 */
public function notify_wx(){ public function notify_wx(){
$config=getConfigPri(); $config=getConfigPri();
//$xmlInfo = $GLOBALS['HTTP_RAW_POST_DATA']; //$xmlInfo = $GLOBALS['HTTP_RAW_POST_DATA'];
$xmlInfo=file_get_contents("php://input"); $xmlInfo=file_get_contents("php://input");
//解析xml //解析xml
$arrayInfo = $this -> xmlToArray($xmlInfo); $arrayInfo = $this -> xmlToArray($xmlInfo);
...@@ -136,23 +136,23 @@ class PayController extends HomebaseController { ...@@ -136,23 +136,23 @@ class PayController extends HomebaseController {
echo $this -> returnInfo("FAIL","签名失败"); echo $this -> returnInfo("FAIL","签名失败");
$this -> logwx($arrayInfo['return_code']);//log打印保存 $this -> logwx($arrayInfo['return_code']);//log打印保存
exit; exit;
} }
} }
private function returnInfo($type,$msg){ private function returnInfo($type,$msg){
if($type == "SUCCESS"){ if($type == "SUCCESS"){
return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code></xml>"; return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code></xml>";
}else{ }else{
return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code><return_msg><![CDATA[{$msg}]]></return_msg></xml>"; return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code><return_msg><![CDATA[{$msg}]]></return_msg></xml>";
} }
} }
//签名验证 //签名验证
private function checkSign($sign1,$sign2){ private function checkSign($sign1,$sign2){
return trim($sign1) == trim($sign2); return trim($sign1) == trim($sign2);
} }
/* 订单查询加值业务处理 /* 订单查询加值业务处理
* @param orderNum 订单号 * @param orderNum 订单号
*/ */
private function orderServer(){ private function orderServer(){
$info = $this -> wxDate; $info = $this -> wxDate;
...@@ -168,59 +168,59 @@ class PayController extends HomebaseController { ...@@ -168,59 +168,59 @@ class PayController extends HomebaseController {
M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$info['transaction_id'])); M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$info['transaction_id']));
$this->logwx("orderno:".$out_trade_no.' 支付成功'); $this->logwx("orderno:".$out_trade_no.' 支付成功');
}else{ }else{
$this->logwx("orderno:".$out_trade_no.' 订单信息不存在'); $this->logwx("orderno:".$out_trade_no.' 订单信息不存在');
return false; return false;
} }
} }
/** /**
* sign拼装获取 * sign拼装获取
*/ */
private function sign($param,$key){ private function sign($param,$key){
$sign = ""; $sign = "";
foreach($param as $k => $v){ foreach($param as $k => $v){
$sign .= $k."=".$v."&"; $sign .= $k."=".$v."&";
} }
$sign .= "key=".$key; $sign .= "key=".$key;
$sign = strtoupper(md5($sign)); $sign = strtoupper(md5($sign));
return $sign; return $sign;
} }
/** /**
* xml转为数组 * xml转为数组
*/ */
private function xmlToArray($xmlStr){ private function xmlToArray($xmlStr){
$msg = array(); $msg = array();
$postStr = $xmlStr; $postStr = $xmlStr;
$msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
return $msg; return $msg;
} }
/* 微信支付 */ /* 微信支付 */
/* 苹果支付 */ /* 苹果支付 */
public function notify_ios(){ public function notify_ios(){
$content=file_get_contents("php://input"); $content=file_get_contents("php://input");
$data = json_decode($content,true); $data = json_decode($content,true);
$this->logios("data:".json_encode($data)); $this->logios("data:".json_encode($data));
$receipt = $data["receipt-data"]; $receipt = $data["receipt-data"];
$isSandbox = $data["sandbox"]; $isSandbox = $data["sandbox"];
$out_trade_no = $data["out_trade_no"]; $out_trade_no = $data["out_trade_no"];
$info = $this->getReceiptData($receipt, $isSandbox); $info = $this->getReceiptData($receipt, $isSandbox);
$this->logios("info:".json_encode($info)); $this->logios("info:".json_encode($info));
$iforderinfo=M("users_charge")->where("trade_no='{$info['transaction_id']}' and type='3'")->find(); $iforderinfo=M("users_charge")->where("trade_no='{$info['transaction_id']}' and type='3'")->find();
if($iforderinfo){ if($iforderinfo){
echo '{"status":"fail","info":"非法提交-001"}';exit; echo '{"status":"fail","info":"非法提交-001"}';exit;
} }
$chargeinfo=M("charge_rules")->where("product_id='{$info['product_id']}'")->find(); $chargeinfo=M("charge_rules")->where("product_id='{$info['product_id']}'")->find();
if(!$chargeinfo){ if(!$chargeinfo){
echo '{"status":"fail","info":"非法提交-002"}';exit; echo '{"status":"fail","info":"非法提交-002"}';exit;
...@@ -228,97 +228,109 @@ class PayController extends HomebaseController { ...@@ -228,97 +228,109 @@ class PayController extends HomebaseController {
//判断订单是否存在 //判断订单是否存在
$orderinfo=M("users_charge")->where("orderno='{$out_trade_no}' and coin='{$chargeinfo['coin']}' and status='0' and type='3'")->find(); $orderinfo=M("users_charge")->where("orderno='{$out_trade_no}' and coin='{$chargeinfo['coin']}' and status='0' and type='3'")->find();
if($orderinfo){ if($orderinfo){
/* 更新会员虚拟币 */ /* 更新会员虚拟币 */
$coin=$orderinfo['coin']+$orderinfo['coin_give']; $coin=$orderinfo['coin']+$orderinfo['coin_give'];
M("users")->where("id='{$orderinfo['touid']}'")->setInc("coin",$coin); M("users")->where("id='{$orderinfo['touid']}'")->setInc("coin",$coin);
/* 更新 订单状态 */ /* 更新 订单状态 */
M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$info['transaction_id'],"ambient"=>$info['ambient'])); M("users_charge")->where("id='{$orderinfo['id']}'")->save(array("status"=>1,"trade_no"=>$info['transaction_id'],"ambient"=>$info['ambient']));
// 邀请返利
$user = M("users")->where("id='{$orderinfo['touid']}'")->find();
if ($user['pid'] > 0) {
$config = getConfigPri();
$invite_reward = !empty($config['invite_reward']) ? $config['invite_reward'] : 0;
$coin2 = ($coin * $invite_reward) / 100;
$c = (int)$coin2;
M("users")->where("id='{$user['pid']}'")->setInc("i_votes",$c);
}
$this->logios("orderno:".$out_trade_no.' 支付成功'); $this->logios("orderno:".$out_trade_no.' 支付成功');
}else{ }else{
$this->logios("orderno:".$out_trade_no.' 订单信息不存在'); $this->logios("orderno:".$out_trade_no.' 订单信息不存在');
echo '{"status":"fail","info":"订单信息不存在-003"}'; echo '{"status":"fail","info":"订单信息不存在-003"}';
exit(); exit();
} }
echo '{"status":"success","info":"充值支付成功"}'; echo '{"status":"success","info":"充值支付成功"}';
exit; exit;
} }
public function getReceiptData($receipt, $isSandbox){
public function getReceiptData($receipt, $isSandbox){
$config=getConfigPri(); $config=getConfigPri();
$this->logios("isSandbox:".$isSandbox); $this->logios("isSandbox:".$isSandbox);
$this->logios("isSandboxc:".$config['ios_sandbox']); $this->logios("isSandboxc:".$config['ios_sandbox']);
$ambient=0; $ambient=0;
if ($isSandbox == $config['ios_sandbox']) { if ($isSandbox == $config['ios_sandbox']) {
//沙盒 //沙盒
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt'; $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
$ambient=0; $ambient=0;
}else { }else {
//生产 //生产
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt'; $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
$ambient=1; $ambient=1;
} }
$postData = json_encode( $postData = json_encode(
array('receipt-data' => $receipt) array('receipt-data' => $receipt)
); );
$ch = curl_init($endpoint); $ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //关闭安全验证 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //关闭安全验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //关闭安全验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //关闭安全验证
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch); $response = curl_exec($ch);
$errno = curl_errno($ch); $errno = curl_errno($ch);
$errmsg = curl_error($ch); $errmsg = curl_error($ch);
curl_close($ch); curl_close($ch);
$this->logios("getReceiptData response:".json_encode($response)); $this->logios("getReceiptData response:".json_encode($response));
$this->logios("getReceiptData errno:".json_encode($errno)); $this->logios("getReceiptData errno:".json_encode($errno));
$this->logios("getReceiptData errmsg:".json_encode($errmsg)); $this->logios("getReceiptData errmsg:".json_encode($errmsg));
if($errno != 0) { if($errno != 0) {
echo '{"status":"fail","info":"服务器出错,请联系管理员"}'; echo '{"status":"fail","info":"服务器出错,请联系管理员"}';
exit; exit;
} }
$data = json_decode($response,1); $data = json_decode($response,1);
if (!is_array($data)) { if (!is_array($data)) {
echo '{"status":"fail","info":"验证失败,如有疑问请联系管理"}'; echo '{"status":"fail","info":"验证失败,如有疑问请联系管理"}';
exit; exit;
} }
if (!isset($data['status']) || $data['status'] != 0) { if (!isset($data['status']) || $data['status'] != 0) {
echo '{"status":"fail","info":"验证失败,如有疑问请联系管理"}'; echo '{"status":"fail","info":"验证失败,如有疑问请联系管理"}';
exit; exit;
} }
$newdata=end($data['receipt']['in_app']); $newdata=end($data['receipt']['in_app']);
return array( return array(
'product_id' => $newdata['product_id'], 'product_id' => $newdata['product_id'],
'transaction_id' => $newdata['transaction_id'], 'transaction_id' => $newdata['transaction_id'],
'ambient' => $ambient, 'ambient' => $ambient,
); );
} }
/* 苹果支付 */ /* 苹果支付 */
/* 打印log */ /* 打印log */
public function logali($msg){ public function logali($msg){
file_put_contents(SITE_PATH.'data/paylog/logali_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND); file_put_contents(SITE_PATH.'data/paylog/logali_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND);
} }
/* 打印log */ /* 打印log */
public function logwx($msg){ public function logwx($msg){
file_put_contents(SITE_PATH.'data/paylog/logwx_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND); file_put_contents(SITE_PATH.'data/paylog/logwx_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND);
} }
/* 打印log */ /* 打印log */
public function logios($msg){ public function logios($msg){
file_put_contents(SITE_PATH.'data/paylog/logios_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND); file_put_contents(SITE_PATH.'data/paylog/logios_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment