Notify.php 17 KB

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