Notify.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\UserService;
  5. use kjpay\kjpay;
  6. use think\Request;
  7. use think\Db;
  8. use addons\epay\library\Service;
  9. /**
  10. * 支付回调
  11. */
  12. class Notify extends Api
  13. {
  14. protected $noNeedLogin = ['wechatnotify', 'alipaynotify', 'wechatkjnotify'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 支付回调
  18. */
  19. public function wechatnotify()
  20. {
  21. $paytype = "wechat";
  22. $pay = Service::checkNotify($paytype);
  23. if (!$pay) {
  24. echo '签名错误';
  25. return;
  26. }
  27. $data = $pay->verify();
  28. Db::startTrans();
  29. try {
  30. $this->payhandel($data);
  31. Db::commit();
  32. } catch (ValidateException $e) {
  33. Db::rollback();
  34. $this->error($e->getMessage());
  35. } catch (PDOException $e) {
  36. Db::rollback();
  37. $this->error($e->getMessage());
  38. } catch (Exception $e) {
  39. Db::rollback();
  40. $this->error($e->getMessage());
  41. }
  42. echo $pay->success();
  43. }
  44. /**
  45. * 支付回调
  46. */
  47. public function alipaynotify()
  48. {
  49. $paytype = "alipay";
  50. $pay = Service::checkNotify($paytype);
  51. if (!$pay) {
  52. echo '签名错误';
  53. return;
  54. }
  55. $data = $pay->verify();
  56. /*$dataStr = '{
  57. "s": "\/api\/notify\/alipaynotify",
  58. "gmt_create": "2023-07-12 09:37:14",
  59. "charset": "utf-8",
  60. "seller_email": "ixiaoqiuqiu@qq.com",
  61. "subject": "充值钻石",
  62. "sign": "SzP5OgA9Z+qD8mSzMbAbKFsYOyB6fiH\/8itILn6YqX0Aloa5FnOxUHXEKCFlv5k3ya+OORjZPDq5mt7eZ3FvwJUasSWnMgDlSa6zWLGhlkKE3HXVAe777tkM5UgnVw53uJEr\/Ur2UsLpI+qEcrZAXhNAhA2nNNWhwkvrA1Fyc3iZERnbtJ3XNT\/8nnxGMBjhS3TWlr3VDZ\/bb26fvC1qHQ9Y6GGz8gvj3z30Fo7\/DlLFvH2HFSi6wky+33vWEtgug+klsqh6ziOWaVJhJKvnt2H+ltINiKLCshYVhu8ZBc856sV8noEwMmxPoFTW4CoG7ut\/3PKLjglygSOZblyf4Q==",
  63. "buyer_id": "2088702050034935",
  64. "invoice_amount": "0.01",
  65. "notify_id": "2023071201222093714034931493558779",
  66. "fund_bill_list": "[{&quot;amount&quot;:&quot;0.01&quot;,&quot;fundChannel&quot;:&quot;ALIPAYACCOUNT&quot;}]",
  67. "notify_type": "trade_status_sync",
  68. "trade_status": "TRADE_SUCCESS",
  69. "receipt_amount": "0.01",
  70. "app_id": "2021004103620776",
  71. "buyer_pay_amount": "0.01",
  72. "sign_type": "RSA2",
  73. "seller_id": "2088641467220712",
  74. "gmt_payment": "2023-07-12 09:37:14",
  75. "notify_time": "2023-07-12 09:37:14",
  76. "version": "1.0",
  77. "out_trade_no": "20230712093707239053",
  78. "total_amount": "0.01",
  79. "trade_no": "2023071222001434931415906799",
  80. "auth_app_id": "2021004103620776",
  81. "buyer_logon_id": "136****5934",
  82. "point_amount": "0.00"
  83. }';
  84. $data = json_decode($dataStr,true);*/
  85. Db::startTrans();
  86. try {
  87. $this->payhandel($data);
  88. Db::commit();
  89. } catch (ValidateException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (PDOException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. echo $pay->success();
  100. }
  101. /**
  102. * 支付处理方法
  103. * @return void
  104. */
  105. private function payhandel($params = [])
  106. {
  107. $payamount = $params['total_amount'];
  108. $out_trade_no = $params['out_trade_no'];
  109. $trade_no = $params['trade_no'];
  110. $orderModel = new \app\common\model\RecharOrder();
  111. $userModel = new \app\common\model\User();
  112. $jewellogModel = new \app\common\model\UserJewelLog();
  113. $rechargelogModel = new \app\common\model\UserRechargeLog();
  114. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  115. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  116. $time = time();
  117. // 修改订单状态
  118. $res1 = $orderModel->update(["status" => 1,'transaction_id' => $trade_no,'paytime'=>$time], ["order_no" => $out_trade_no]);
  119. // 修改用户钻石余额
  120. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  121. //判断是否首充
  122. $jewellogWhere['user_id'] = $orderInfo["user_id"];
  123. $jewellogWhere['type'] = 1;
  124. $userJewelLog = $jewellogModel->where($jewellogWhere)->find();
  125. $isFirst = 1;
  126. if (!empty($userJewelLog)) {
  127. $isFirst = 0;
  128. }
  129. $preUserId = $userInfo['pre_userid'];
  130. // 添加钻石流水记录
  131. $detail = "钻石充值";
  132. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  133. // 添加充值记录
  134. $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], $orderInfo["platform"], 2,$isFirst,$preUserId);
  135. if ($res1 && $res2 && $res3 && $res4) {
  136. // 添加统计
  137. $userInfo->chargecount = $userInfo->chargecount + $payamount;
  138. $userInfo->chargetime = time();
  139. $userInfo->save();
  140. $userService = new UserService();//邀请赠送奖励
  141. $userService->inviteMoney(['user_id'=>$orderInfo["user_id"]]);
  142. // +EXP 充值任意金额
  143. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "5EMwg7la", 1);
  144. // 查询今日充值记录
  145. $today = strtotime(date("Y-m-d 00:00:00"));
  146. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $orderInfo["user_id"], "createtime" => ["gt", $today]])->sum("money");
  147. if ($rechargeMoney >= 100) {
  148. // +EXP 充值超过100
  149. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "DaCVgOLu", 1);
  150. }
  151. } else {
  152. $res1 = $orderModel->update(["status" => -1], ["order_no" => $out_trade_no]);
  153. if (!$res1) {
  154. throw new Exception('更新充值状态失败:'.$out_trade_no);
  155. }
  156. }
  157. }
  158. /**
  159. * 支付回调
  160. */
  161. public function wechatkjnotify()
  162. {
  163. $input = file_get_contents("php://input"); // 主题信息
  164. $dataArr = explode("&", urldecode($input));
  165. $data = [];
  166. if ($dataArr) foreach ($dataArr as $k => $v) {
  167. $vv = explode("=", $v);
  168. $data[$vv[0]] = $vv[1];
  169. }
  170. // 实例化支付类
  171. $kjpay = new kjpay();
  172. // 获取公共参数
  173. $config = config("kjPay");
  174. //密钥
  175. $key = $config["key"];
  176. $sign = $data['sign'];
  177. unset($data['sign']);
  178. if ($kjpay->local_sign($data, $key) == $sign) {
  179. //验签成功后,开始业务逻辑
  180. try {
  181. $payamount = $data['amount'];
  182. $out_trade_no = $data['merchant_order_no'];
  183. $pay_channel = $data['pay_channel'];
  184. Db::startTrans();
  185. try {
  186. $orderModel = new \app\common\model\RecharOrder();
  187. $userModel = new \app\common\model\User();
  188. $jewellogModel = new \app\common\model\UserJewelLog();
  189. $rechargelogModel = new \app\common\model\UserRechargeLog();
  190. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  191. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  192. // 修改订单状态
  193. $res1 = $orderModel->update(["status" => 1], ["order_no" => $out_trade_no]);
  194. // 修改用户钻石余额
  195. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  196. // 添加钻石流水记录
  197. $detail = "砖石充值";
  198. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  199. // 添加充值记录
  200. $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
  201. $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
  202. $platformArr = ["wx_app_pay" => 1, "ali_wap_pay" => 2, "wx_public_pay" => 3];
  203. $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"]);
  204. if ($res1 && $res2 && $res3 && $res4) {
  205. // 添加统计
  206. $time = time();
  207. (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
  208. // \app\common\model\Test::update(["content"=>"update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = ".$userInfo["id"]],["id"=>1]);
  209. // 首充赠送
  210. $userInfo = \app\common\model\User::get($orderInfo["user_id"]);
  211. if ($userInfo->is_recharge == 0) { // 首充
  212. Db::startTrans();
  213. try {
  214. // 赠送消息尾灯
  215. $res1 = \app\common\model\AttireBack::addToMyBack(15, $orderInfo["user_id"]);
  216. $userInfo->is_recharge = 1;
  217. $res2 = $userInfo->save();
  218. if ($res1 && $res2) Db::commit();
  219. } catch (ValidateException $e) {
  220. Db::rollback();
  221. $this->error($e->getMessage());
  222. }
  223. }
  224. $this->rebate($orderInfo["user_id"], $data['amount']);
  225. // +EXP
  226. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "5EMwg7la", 1);
  227. // 查询今日充值记录
  228. $today = strtotime(date("Y-m-d 00:00:00"));
  229. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $orderInfo["user_id"], "createtime" => ["gt", $today]])->sum("money");
  230. if ($rechargeMoney >= 100) {
  231. // +EXP
  232. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "DaCVgOLu", 1);
  233. }
  234. // 查询新人礼包
  235. $this->addBagGift($orderInfo["user_id"], $data['amount']);
  236. Db::commit();
  237. echo "success";
  238. exit;
  239. }
  240. } catch (ValidateException $e) {
  241. Db::rollback();
  242. $this->error($e->getMessage());
  243. } catch (PDOException $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. } catch (Exception $e) {
  247. Db::rollback();
  248. $this->error($e->getMessage());
  249. }
  250. //你可以在此编写订单逻辑
  251. } catch (Exception $e) {
  252. }
  253. }
  254. }
  255. /**
  256. * 银联提现回调
  257. */
  258. public function cashoutNotify()
  259. {
  260. }
  261. /**
  262. * 更新新人礼包
  263. */
  264. private function addBagGift($user_id, $amount)
  265. {
  266. if (!$user_id || !$amount) return false;
  267. // 先看该用户是否有新人礼包特权
  268. $have = \app\common\model\NewBagHave::where(["user_id" => $user_id])->find();
  269. // print_r($have);exit;
  270. if ($have && time() - $have->createtime <= 7 * 86400) {
  271. $where = [];
  272. $where["value"] = ["elt", $amount];
  273. $bagInfo = \app\common\model\NewBag::where($where)->order("value", "desc")->select();
  274. if (!$bagInfo) return false;
  275. $res1 = false;
  276. foreach ($bagInfo as $k => $v) {
  277. // 查询完成情况
  278. $bagfinish = \app\common\model\NewBagFinish::where(["user_id" => $user_id, "bag_id" => $v["id"]])->find();
  279. if (!$bagfinish || $bagfinish->status <= 0) {
  280. if ($bagfinish) {
  281. $bagfinish->status = 1;
  282. $bagfinish->updatetime = time();
  283. $res1 = $bagfinish->save();
  284. } else {
  285. $res1 = \app\common\model\NewBagFinish::insert(["user_id" => $user_id, "bag_id" => $v["id"], "status" => 1, "updatetime" => time()]);
  286. }
  287. break;
  288. }
  289. }
  290. return $res1;
  291. }
  292. }
  293. /**
  294. * 充值返利
  295. */
  296. private function rebate($user_id, $money)
  297. {
  298. // // 获取配置信息
  299. // $config = \app\common\model\RebateConfig::where(["recharge"=>$money])->find();
  300. // if(!$config) return true;
  301. // 找到当前用户的上级和上上级
  302. $userInfo = \app\common\model\User::get($user_id);
  303. if ($userInfo->pre_userid > 0) {
  304. $preUserInfo = \app\common\model\User::where(["id" => $userInfo->pre_userid])->find();// 上一级用户
  305. $this->addRebateMoney($preUserInfo->id, $user_id, $money, 1);
  306. if ($preUserInfo->pre_userid > 0) {
  307. $this->addRebateMoney($preUserInfo->pre_userid, $user_id, $money, 2);
  308. } else {
  309. return true;
  310. }
  311. } else {
  312. return true;
  313. }
  314. }
  315. /**
  316. * 添加返利金额
  317. */
  318. private function addRebateMoney($preUserId, $user_id, $money, $type)
  319. {
  320. Db::startTrans();
  321. try {
  322. // 添加返利记录表
  323. $data = [];
  324. $data["user_id"] = $preUserId;
  325. $data["by_user_id"] = $user_id;
  326. $data["money"] = $money;
  327. $data["rebate_money"] = 0.00;
  328. $data["rebate_type"] = $type;
  329. $data["createtime"] = time();
  330. $res1 = \app\common\model\RebateLog::insertGetId($data);
  331. // $res2 = (new \app\common\model\RebateLog)->execute("update hx_rebate_log set rebate_money = $rebatemoney where id = $res1");
  332. if ($res1) {
  333. Db::commit();
  334. }
  335. } catch (ValidateException $e) {
  336. Db::rollback();
  337. $this->error($e->getMessage());
  338. } catch (PDOException $e) {
  339. Db::rollback();
  340. $this->error($e->getMessage());
  341. } catch (Exception $e) {
  342. Db::rollback();
  343. $this->error($e->getMessage());
  344. }
  345. }
  346. }