Notify.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use kjpay\kjpay;
  5. use think\Request;
  6. use think\Db;
  7. use addons\epay\library\Service;
  8. /**
  9. * 支付回调
  10. */
  11. class Notify extends Api
  12. {
  13. protected $noNeedLogin = ['wechatnotify', 'wechatkjnotify'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 支付回调
  17. */
  18. public function wechatnotify()
  19. {
  20. $paytype = "wechat";
  21. $pay = Service::checkNotify($paytype);
  22. if (!$pay) {
  23. echo '签名错误';
  24. return;
  25. }
  26. $data = $pay->verify();
  27. try {
  28. $payamount = $data['total_fee'] / 100;
  29. $out_trade_no = $data['out_trade_no'];
  30. Db::startTrans();
  31. try {
  32. $orderModel = new \app\common\model\RecharOrder();
  33. $userModel = new \app\common\model\User();
  34. $jewellogModel = new \app\common\model\UserJewelLog();
  35. $rechargelogModel = new \app\common\model\UserRechargeLog();
  36. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  37. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  38. // 修改订单状态
  39. $res1 = $orderModel->update(["status" => 1], ["order_no" => $out_trade_no]);
  40. // 修改用户钻石余额
  41. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  42. // 添加钻石流水记录
  43. $detail = "砖石充值";
  44. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  45. // 添加充值记录
  46. $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], $orderInfo["platform"], 1);
  47. if ($res1 && $res2 && $res3 && $res4) {
  48. // 添加统计
  49. $userInfo->chargecount = $userInfo->chargecount + $payamount;
  50. $userInfo->chargetime = time();
  51. $userInfo->save();
  52. Db::commit();
  53. }
  54. } catch (ValidateException $e) {
  55. Db::rollback();
  56. $this->error($e->getMessage());
  57. } catch (PDOException $e) {
  58. Db::rollback();
  59. $this->error($e->getMessage());
  60. } catch (Exception $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. }
  64. //你可以在此编写订单逻辑
  65. } catch (Exception $e) {
  66. }
  67. echo $pay->success();
  68. }
  69. /**
  70. * 支付回调
  71. */
  72. public function wechatkjnotify()
  73. {
  74. $input = file_get_contents("php://input"); // 主题信息
  75. $dataArr = explode("&", urldecode($input));
  76. $data = [];
  77. if ($dataArr) foreach ($dataArr as $k => $v) {
  78. $vv = explode("=", $v);
  79. $data[$vv[0]] = $vv[1];
  80. }
  81. // 实例化支付类
  82. $kjpay = new kjpay();
  83. // 获取公共参数
  84. $config = config("kjPay");
  85. //密钥
  86. $key = $config["key"];
  87. $sign = $data['sign'];
  88. unset($data['sign']);
  89. if ($kjpay->local_sign($data, $key) == $sign) {
  90. //验签成功后,开始业务逻辑
  91. try {
  92. $payamount = $data['amount'];
  93. $out_trade_no = $data['merchant_order_no'];
  94. $pay_channel = $data['pay_channel'];
  95. Db::startTrans();
  96. try {
  97. $orderModel = new \app\common\model\RecharOrder();
  98. $userModel = new \app\common\model\User();
  99. $jewellogModel = new \app\common\model\UserJewelLog();
  100. $rechargelogModel = new \app\common\model\UserRechargeLog();
  101. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  102. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  103. // 修改订单状态
  104. $res1 = $orderModel->update(["status" => 1], ["order_no" => $out_trade_no]);
  105. // 修改用户钻石余额
  106. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  107. // 添加钻石流水记录
  108. $detail = "砖石充值";
  109. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  110. // 添加充值记录
  111. $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
  112. $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
  113. $platformArr = ["wx_app_pay" => 1, "ali_wap_pay" => 2, "wx_public_pay" => 3];
  114. $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"]);
  115. if ($res1 && $res2 && $res3 && $res4) {
  116. // 添加统计
  117. $time = time();
  118. (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
  119. // \app\common\model\Test::update(["content"=>"update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = ".$userInfo["id"]],["id"=>1]);
  120. // 首充赠送
  121. $userInfo = \app\common\model\User::get($orderInfo["user_id"]);
  122. if ($userInfo->is_recharge == 0) { // 首充
  123. Db::startTrans();
  124. try {
  125. // 赠送消息尾灯
  126. $res1 = \app\common\model\AttireBack::addToMyBack(15, $orderInfo["user_id"]);
  127. $userInfo->is_recharge = 1;
  128. $res2 = $userInfo->save();
  129. if ($res1 && $res2) Db::commit();
  130. } catch (ValidateException $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. $this->rebate($orderInfo["user_id"], $data['amount']);
  136. // +EXP
  137. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "5EMwg7la", 1);
  138. // 查询今日充值记录
  139. $today = strtotime(date("Y-m-d 00:00:00"));
  140. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $orderInfo["user_id"], "createtime" => ["gt", $today]])->sum("money");
  141. if ($rechargeMoney >= 100) {
  142. // +EXP
  143. \app\common\model\TaskLog::tofinish($orderInfo["user_id"], "DaCVgOLu", 1);
  144. }
  145. // 查询新人礼包
  146. $this->addBagGift($orderInfo["user_id"], $data['amount']);
  147. Db::commit();
  148. echo "success";
  149. exit;
  150. }
  151. } catch (ValidateException $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. } catch (PDOException $e) {
  155. Db::rollback();
  156. $this->error($e->getMessage());
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. //你可以在此编写订单逻辑
  162. } catch (Exception $e) {
  163. }
  164. }
  165. }
  166. /**
  167. * 银联提现回调
  168. */
  169. public function cashoutNotify()
  170. {
  171. }
  172. /**
  173. * 更新新人礼包
  174. */
  175. private function addBagGift($user_id, $amount)
  176. {
  177. if (!$user_id || !$amount) return false;
  178. // 先看该用户是否有新人礼包特权
  179. $have = \app\common\model\NewBagHave::where(["user_id" => $user_id])->find();
  180. // print_r($have);exit;
  181. if ($have && time() - $have->createtime <= 7 * 86400) {
  182. $where = [];
  183. $where["value"] = ["elt", $amount];
  184. $bagInfo = \app\common\model\NewBag::where($where)->order("value", "desc")->select();
  185. if (!$bagInfo) return false;
  186. $res1 = false;
  187. foreach ($bagInfo as $k => $v) {
  188. // 查询完成情况
  189. $bagfinish = \app\common\model\NewBagFinish::where(["user_id" => $user_id, "bag_id" => $v["id"]])->find();
  190. if (!$bagfinish || $bagfinish->status <= 0) {
  191. if ($bagfinish) {
  192. $bagfinish->status = 1;
  193. $bagfinish->updatetime = time();
  194. $res1 = $bagfinish->save();
  195. } else {
  196. $res1 = \app\common\model\NewBagFinish::insert(["user_id" => $user_id, "bag_id" => $v["id"], "status" => 1, "updatetime" => time()]);
  197. }
  198. break;
  199. }
  200. }
  201. return $res1;
  202. }
  203. }
  204. /**
  205. * 充值返利
  206. */
  207. private function rebate($user_id, $money)
  208. {
  209. // // 获取配置信息
  210. // $config = \app\common\model\RebateConfig::where(["recharge"=>$money])->find();
  211. // if(!$config) return true;
  212. // 找到当前用户的上级和上上级
  213. $userInfo = \app\common\model\User::get($user_id);
  214. if ($userInfo->pre_userid > 0) {
  215. $preUserInfo = \app\common\model\User::where(["id" => $userInfo->pre_userid])->find();// 上一级用户
  216. $this->addRebateMoney($preUserInfo->id, $user_id, $money, 1);
  217. if ($preUserInfo->pre_userid > 0) {
  218. $this->addRebateMoney($preUserInfo->pre_userid, $user_id, $money, 2);
  219. } else {
  220. return true;
  221. }
  222. } else {
  223. return true;
  224. }
  225. }
  226. /**
  227. * 添加返利金额
  228. */
  229. private function addRebateMoney($preUserId, $user_id, $money, $type)
  230. {
  231. Db::startTrans();
  232. try {
  233. // 添加返利记录表
  234. $data = [];
  235. $data["user_id"] = $preUserId;
  236. $data["by_user_id"] = $user_id;
  237. $data["money"] = $money;
  238. $data["rebate_money"] = 0.00;
  239. $data["rebate_type"] = $type;
  240. $data["createtime"] = time();
  241. $res1 = \app\common\model\RebateLog::insertGetId($data);
  242. // $res2 = (new \app\common\model\RebateLog)->execute("update hx_rebate_log set rebate_money = $rebatemoney where id = $res1");
  243. if ($res1) {
  244. Db::commit();
  245. }
  246. } catch (ValidateException $e) {
  247. Db::rollback();
  248. $this->error($e->getMessage());
  249. } catch (PDOException $e) {
  250. Db::rollback();
  251. $this->error($e->getMessage());
  252. } catch (Exception $e) {
  253. Db::rollback();
  254. $this->error($e->getMessage());
  255. }
  256. }
  257. }