123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\service\UserService;
- use kjpay\kjpay;
- use think\Request;
- use think\Db;
- use addons\epay\library\Service;
- /**
- * 支付回调
- */
- class Notify extends Api
- {
- protected $noNeedLogin = ['wechatnotify', 'alipaynotify', 'wechatkjnotify'];
- protected $noNeedRight = ['*'];
- /**
- * 支付回调
- */
- public function wechatnotify()
- {
- $paytype = "wechat";
- $pay = Service::checkNotify($paytype);
- if (!$pay) {
- echo '签名错误';
- return;
- }
- $data = $pay->verify();
- Db::startTrans();
- try {
- /*$orderModel = new \app\common\model\RecharOrder();
- $out_trade_no = $data['out_trade_no'];
- $orderinfo = $orderModel->where(["order_no" => $out_trade_no])->find();
- if(empty($orderinfo)){
- Db::rollback();
- echo $pay->success();
- exit;
- }
- if($orderinfo['status'] != 0)
- {
- Db::rollback();
- echo $pay->success();
- exit;
- }*/
- $this->payhandel($data);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- echo $pay->success();
- }
- /**
- * 支付回调
- */
- public function alipaynotify()
- {
- $paytype = "alipay";
- $notify_file = $this->notify_log_start($paytype);
- $pay = Service::checkNotify($paytype);
- if (!$pay) {
- echo '签名错误';
- return;
- }
- $data = $pay->verify();
- /*$dataStr = '{
- "s": "\/api\/notify\/alipaynotify",
- "gmt_create": "2023-07-12 09:37:14",
- "charset": "utf-8",
- "seller_email": "ixiaoqiuqiu@qq.com",
- "subject": "充值钻石",
- "sign": "SzP5OgA9Z+qD8mSzMbAbKFsYOyB6fiH\/8itILn6YqX0Aloa5FnOxUHXEKCFlv5k3ya+OORjZPDq5mt7eZ3FvwJUasSWnMgDlSa6zWLGhlkKE3HXVAe777tkM5UgnVw53uJEr\/Ur2UsLpI+qEcrZAXhNAhA2nNNWhwkvrA1Fyc3iZERnbtJ3XNT\/8nnxGMBjhS3TWlr3VDZ\/bb26fvC1qHQ9Y6GGz8gvj3z30Fo7\/DlLFvH2HFSi6wky+33vWEtgug+klsqh6ziOWaVJhJKvnt2H+ltINiKLCshYVhu8ZBc856sV8noEwMmxPoFTW4CoG7ut\/3PKLjglygSOZblyf4Q==",
- "buyer_id": "2088702050034935",
- "invoice_amount": "0.01",
- "notify_id": "2023071201222093714034931493558779",
- "fund_bill_list": "[{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]",
- "notify_type": "trade_status_sync",
- "trade_status": "TRADE_SUCCESS",
- "receipt_amount": "0.01",
- "app_id": "2021004103620776",
- "buyer_pay_amount": "0.01",
- "sign_type": "RSA2",
- "seller_id": "2088641467220712",
- "gmt_payment": "2023-07-12 09:37:14",
- "notify_time": "2023-07-12 09:37:14",
- "version": "1.0",
- "out_trade_no": "20230712093707239053",
- "total_amount": "0.01",
- "trade_no": "2023071222001434931415906799",
- "auth_app_id": "2021004103620776",
- "buyer_logon_id": "136****5934",
- "point_amount": "0.00"
- }';
- $data = json_decode($dataStr,true);*/
- Db::startTrans();
- try {
- $orderModel = new \app\common\model\RecharOrder();
- $out_trade_no = $data['out_trade_no'];
- $orderinfo = $orderModel->where(["order_no" => $out_trade_no])->find();
- if(empty($orderinfo)){
- Db::rollback();
- echo $pay->success();
- exit;
- }
- if($orderinfo['status'] != 0)
- {
- Db::rollback();
- echo $pay->success();
- exit;
- }
- $this->payhandel($data);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- echo $pay->success();
- }
- /**
- * 支付处理方法
- * @return void
- */
- private function payhandel($params = [])
- {
- $payamount = $params['total_amount'];
- $out_trade_no = $params['out_trade_no'];
- $trade_no = $params['trade_no'];
- $orderModel = new \app\common\model\RecharOrder();
- $userModel = new \app\common\model\User();
- $jewellogModel = new \app\common\model\UserJewelLog();
- $rechargelogModel = new \app\common\model\UserRechargeLog();
- $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
- $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
- $time = time();
- // 修改订单状态
- $res1 = $orderModel->update(["status" => 1,'transaction_id' => $trade_no,'paytime'=>$time], ["order_no" => $out_trade_no]);
- // 修改用户钻石余额
- $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
- //判断是否首充
- $jewellogWhere['user_id'] = $orderInfo["user_id"];
- $jewellogWhere['type'] = 1;
- $userJewelLog = $jewellogModel->where($jewellogWhere)->find();
- $isFirst = 1;
- if (!empty($userJewelLog)) {
- $isFirst = 0;
- }
- $preUserId = $userInfo['pre_userid'];
- // 添加钻石流水记录
- $detail = "钻石充值";
- $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
- // 添加充值记录
- $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], $orderInfo["platform"], 2,$isFirst,$preUserId);
- if ($res1 && $res2 && $res3 && $res4) {
- // 添加统计
- $userInfo->chargecount = $userInfo->chargecount + $payamount;
- $userInfo->chargetime = time();
- $userInfo->save();
- $userService = new UserService();//邀请赠送奖励
- $userService->inviteMoney(['user_id'=>$orderInfo["user_id"]]);
- // +EXP 充值任意金额
- \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "5EMwg7la", 1);
- // 查询今日充值记录
- $today = strtotime(date("Y-m-d 00:00:00"));
- $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $orderInfo["user_id"], "createtime" => ["gt", $today]])->sum("money");
- if ($rechargeMoney >= 100) {
- // +EXP 充值超过100
- \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "DaCVgOLu", 1);
- }
- } else {
- $res1 = $orderModel->update(["status" => -1], ["order_no" => $out_trade_no]);
- if (!$res1) {
- throw new Exception('更新充值状态失败:'.$out_trade_no);
- }
- }
- }
- /**
- * 支付回调
- */
- public function wechatkjnotify()
- {
- $input = file_get_contents("php://input"); // 主题信息
- $dataArr = explode("&", urldecode($input));
- $data = [];
- if ($dataArr) foreach ($dataArr as $k => $v) {
- $vv = explode("=", $v);
- $data[$vv[0]] = $vv[1];
- }
- // 实例化支付类
- $kjpay = new kjpay();
- // 获取公共参数
- $config = config("kjPay");
- //密钥
- $key = $config["key"];
- $sign = $data['sign'];
- unset($data['sign']);
- if ($kjpay->local_sign($data, $key) == $sign) {
- //验签成功后,开始业务逻辑
- try {
- $payamount = $data['amount'];
- $out_trade_no = $data['merchant_order_no'];
- $pay_channel = $data['pay_channel'];
- Db::startTrans();
- try {
- $orderModel = new \app\common\model\RecharOrder();
- $userModel = new \app\common\model\User();
- $jewellogModel = new \app\common\model\UserJewelLog();
- $rechargelogModel = new \app\common\model\UserRechargeLog();
- $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
- $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
- // 修改订单状态
- $res1 = $orderModel->update(["status" => 1], ["order_no" => $out_trade_no]);
- // 修改用户钻石余额
- $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
- // 添加钻石流水记录
- $detail = "砖石充值";
- $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
- // 添加充值记录
- $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
- $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
- $platformArr = ["wx_app_pay" => 1, "ali_wap_pay" => 2, "wx_public_pay" => 3];
- $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], $orderInfo["platform"], $platformArr[$pay_channel], $isFirstRecharge, $userInfo["pre_userid"]);
- if ($res1 && $res2 && $res3 && $res4) {
- // 添加统计
- $time = time();
- (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
- // \app\common\model\Test::update(["content"=>"update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = ".$userInfo["id"]],["id"=>1]);
- // 首充赠送
- $userInfo = \app\common\model\User::get($orderInfo["user_id"]);
- if ($userInfo->is_recharge == 0) { // 首充
- Db::startTrans();
- try {
- // 赠送消息尾灯
- $res1 = \app\common\model\AttireBack::addToMyBack(15, $orderInfo["user_id"]);
- $userInfo->is_recharge = 1;
- $res2 = $userInfo->save();
- if ($res1 && $res2) Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- $this->rebate($orderInfo["user_id"], $data['amount']);
- // +EXP
- \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "5EMwg7la", 1);
- // 查询今日充值记录
- $today = strtotime(date("Y-m-d 00:00:00"));
- $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $orderInfo["user_id"], "createtime" => ["gt", $today]])->sum("money");
- if ($rechargeMoney >= 100) {
- // +EXP
- \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "DaCVgOLu", 1);
- }
- // 查询新人礼包
- $this->addBagGift($orderInfo["user_id"], $data['amount']);
- Db::commit();
- echo "success";
- exit;
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- //你可以在此编写订单逻辑
- } catch (Exception $e) {
- }
- }
- }
- /**
- * 银联提现回调
- */
- public function cashoutNotify()
- {
- }
- /**
- * 更新新人礼包
- */
- private function addBagGift($user_id, $amount)
- {
- if (!$user_id || !$amount) return false;
- // 先看该用户是否有新人礼包特权
- $have = \app\common\model\NewBagHave::where(["user_id" => $user_id])->find();
- // print_r($have);exit;
- if ($have && time() - $have->createtime <= 7 * 86400) {
- $where = [];
- $where["value"] = ["elt", $amount];
- $bagInfo = \app\common\model\NewBag::where($where)->order("value", "desc")->select();
- if (!$bagInfo) return false;
- $res1 = false;
- foreach ($bagInfo as $k => $v) {
- // 查询完成情况
- $bagfinish = \app\common\model\NewBagFinish::where(["user_id" => $user_id, "bag_id" => $v["id"]])->find();
- if (!$bagfinish || $bagfinish->status <= 0) {
- if ($bagfinish) {
- $bagfinish->status = 1;
- $bagfinish->updatetime = time();
- $res1 = $bagfinish->save();
- } else {
- $res1 = \app\common\model\NewBagFinish::insert(["user_id" => $user_id, "bag_id" => $v["id"], "status" => 1, "updatetime" => time()]);
- }
- break;
- }
- }
- return $res1;
- }
- }
- /**
- * 充值返利
- */
- private function rebate($user_id, $money)
- {
- // // 获取配置信息
- // $config = \app\common\model\RebateConfig::where(["recharge"=>$money])->find();
- // if(!$config) return true;
- // 找到当前用户的上级和上上级
- $userInfo = \app\common\model\User::get($user_id);
- if ($userInfo->pre_userid > 0) {
- $preUserInfo = \app\common\model\User::where(["id" => $userInfo->pre_userid])->find();// 上一级用户
- $this->addRebateMoney($preUserInfo->id, $user_id, $money, 1);
- if ($preUserInfo->pre_userid > 0) {
- $this->addRebateMoney($preUserInfo->pre_userid, $user_id, $money, 2);
- } else {
- return true;
- }
- } else {
- return true;
- }
- }
- /**
- * 添加返利金额
- */
- private function addRebateMoney($preUserId, $user_id, $money, $type)
- {
- Db::startTrans();
- try {
- // 添加返利记录表
- $data = [];
- $data["user_id"] = $preUserId;
- $data["by_user_id"] = $user_id;
- $data["money"] = $money;
- $data["rebate_money"] = 0.00;
- $data["rebate_type"] = $type;
- $data["createtime"] = time();
- $res1 = \app\common\model\RebateLog::insertGetId($data);
- // $res2 = (new \app\common\model\RebateLog)->execute("update hx_rebate_log set rebate_money = $rebatemoney where id = $res1");
- if ($res1) {
- Db::commit();
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- //异步日志
- private function notify_log_start($paytype = 'wechat'){
- //记录支付回调数据
- ignore_user_abort(); // run script in background
- set_time_limit(30);
- // 日志文件 start
- $log_base_dir = '../paylog/'.$paytype.'/';
- if (!is_dir($log_base_dir))
- {
- mkdir($log_base_dir, 0770, true);
- @chmod($log_base_dir, 0770);
- }
- $notify_file = $log_base_dir.'notify.txt';
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- if(filesize($notify_file)>5242880)//大于5M自动切换
- {
- rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
- }
- if(!file_exists($notify_file)) {
- @touch($notify_file);
- @chmod($notify_file, 0770);
- }
- // 日志文件 end
- //开始写入
- $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
- if($_REQUEST && $paytype == 'alipay') {
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
- } else {
- $xml = file_get_contents("php://input");
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
- $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
- }
- ini_set('display_errors','On');
- return $notify_file;
- }
- }
|