Callback.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\Goods;
  4. use App\Http\Models\GoodsLabel;
  5. use App\Http\Models\Order;
  6. use App\Http\Models\Payment;
  7. use App\Http\Models\SsNode;
  8. use App\Http\Models\SsNodeLabel;
  9. use App\Http\Models\User;
  10. use App\Http\Models\UserLabel;
  11. use App\Mail\sendUserInfo;
  12. use DB;
  13. use Exception;
  14. use Hash;
  15. use Log;
  16. use Mail;
  17. trait Callback
  18. {
  19. protected static $systemConfig;
  20. function __construct()
  21. {
  22. self::$systemConfig = Helpers::systemConfig();
  23. }
  24. public function show()
  25. {
  26. exit('show');
  27. }
  28. // 交易支付
  29. private function tradePaid($msg, $pay_type)
  30. {
  31. $pay_type_name = $pay_type == 1? '余额支付' : ($pay_type == 4? '支付宝国际' : ($pay_type == 5? '支付宝当面付' : ''));
  32. if($pay_type != 1){
  33. Log::info('【'.$pay_type_name.'】支付成功,开始处理回调订单');
  34. // 获取未完成状态的订单防止重复增加时间
  35. $payment = Payment::query()->with(['order', 'order.goods'])->where('status', 0)->where('order_sn', $msg['out_trade_no'])->first();
  36. if(!$payment){
  37. Log::info('【'.$pay_type_name.'】回调订单【'.$msg['out_trade_no'].'】不存在');
  38. return FALSE;
  39. }
  40. }else{
  41. Log::info('【'.$pay_type_name.'】订单处理');
  42. }
  43. // 处理订单
  44. DB::beginTransaction();
  45. try{
  46. if($pay_type != 1){
  47. // 如果支付单中没有用户信息则创建一个用户
  48. if(!$payment->user_id){
  49. $uid = Helpers::addUser('自动生成-'.$payment->order->email, Hash::make(makeRandStr()), 1, $payment->order->goods->days);
  50. if($uid){
  51. Order::query()->where('oid', $payment->oid)->update(['user_id' => $uid]);
  52. }
  53. }
  54. // 更新支付单
  55. $payment->pay_way = $pay_type == 4 || $pay_type == 5? 2 : 1; // 1-微信、2-支付宝
  56. $payment->status = 1;
  57. $payment->save();
  58. }
  59. // 更新订单
  60. $order = Order::query()->where('order_sn', $msg['out_trade_no'])->first();
  61. // 提取商品信息
  62. $goods = Goods::query()->where('id', $order->goods_id)->first();
  63. // 取出用户信息
  64. $user = User::query()->where('id', $order->user_id)->first();
  65. // 商品为流量或者套餐
  66. switch($goods->type){
  67. case 1:
  68. $order->status = 2;
  69. $order->save();
  70. Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, $user->transfer_enable+$goods->traffic*1048576, '[在线支付]加上用户购买的套餐流量');
  71. User::query()->where('id', $order->user_id)->increment('transfer_enable', $goods->traffic*1048576);
  72. break;
  73. case 2:
  74. $activePlan = Order::query()
  75. ->where('user_id', $order->user_id)
  76. ->with(['goods'])
  77. ->whereHas('goods', function($q){
  78. $q->where('type', 2);
  79. })
  80. ->where('is_expire', 0)
  81. ->where('status', 2)
  82. ->exists();
  83. // 2为开始生效,3为预支付
  84. $order->status = $activePlan? 3 : 2;
  85. $order->save();
  86. // 预支付不执行
  87. if(!$activePlan){
  88. // 如果买的是套餐,则先将之前购买的套餐都无效化,重置用户已用、可用流量为0
  89. Order::query()
  90. ->where('user_id', $order->user_id)
  91. ->with(['goods'])
  92. ->whereHas('goods', function($q){
  93. $q->where('type', '<=', 2);
  94. })
  95. ->where('is_expire', 0)
  96. ->where('status', 2)
  97. ->where('oid', '<>', $order->oid)
  98. ->update(['expire_at' => date('Y-m-d H:i:s'), 'is_expire' => 1]);
  99. Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, 0, '[在线支付]用户购买新套餐,先清空流量');
  100. User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'transfer_enable' => 0]);
  101. $userTraffic = $goods->traffic*1048576;
  102. // 添加账号有效期
  103. $expireTime = date('Y-m-d', strtotime("+".$goods->days." days"));
  104. //账号下一个重置时间
  105. $nextResetTime = date('Y-m-d', strtotime("+".$goods->period." days"));
  106. if($nextResetTime >= $expireTime){
  107. $nextResetTime = NULL;
  108. }
  109. // 写入用户标签
  110. if($goods->label){
  111. // 删除用户所有标签
  112. UserLabel::query()->where('user_id', $order->user_id)->delete();
  113. //取出 商品默认标签 & 系统默认标签 去重
  114. $newUserLabels = array_values(array_unique(array_merge(GoodsLabel::query()->where('goods_id', $order->goods_id)->pluck('label_id')->toArray(), self::$systemConfig['initial_labels_for_user']? explode(',', self::$systemConfig['initial_labels_for_user']) : [])));
  115. // 生成标签
  116. foreach($newUserLabels as $vo){
  117. $obj = new UserLabel();
  118. $obj->user_id = $order->user_id;
  119. $obj->label_id = $vo;
  120. $obj->save();
  121. }
  122. }
  123. // 写入返利日志
  124. if($order->user->referral_uid){
  125. $this->addReferralLog($order->user_id, $order->user->referral_uid, $order->oid, $order->amount, $order->amount*self::$systemConfig['referral_percent']);
  126. // 邀请注册功能开启时,每成功邀请一名付费用户,返还邀请者邀请名额
  127. if(self::$systemConfig['is_invite_register']){
  128. User::query()->where('id', $order->user->referral_uid)->increment('invite_num', 1);
  129. }
  130. }
  131. Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, $userTraffic, '[在线支付]加上用户购买的套餐流量');
  132. User::query()->where('id', $order->user_id)->increment('invite_num', $goods->invite_num? : 0, ['transfer_enable' => $userTraffic, 'reset_time' => $nextResetTime, 'expire_time' => $expireTime, 'enable' => 1]);
  133. }else{
  134. //预支付订单先给上账号时间用于流量重置判断
  135. User::query()->where('id', $order->user_id)->update(['expire_time' => date('Y-m-d', strtotime("+".$goods->days." days", strtotime($user->expire_time)))]);
  136. }
  137. break;
  138. case 3:
  139. $order->status = 2;
  140. $order->save();
  141. User::query()->where('id', $order->user_id)->increment('balance', $goods->price*100);
  142. // 余额变动记录日志
  143. $this->addUserBalanceLog($order->user_id, $order->oid, $order->user->balance, $order->user->balance+$goods->price, $goods->price, '用户在线充值');
  144. break;
  145. default:
  146. Log::info('【处理订单】出现错误-未知套餐类型');
  147. }
  148. // 自动提号机:如果order的email值不为空
  149. if($order->email){
  150. $title = '自动发送账号信息';
  151. $content = [
  152. 'order_sn' => $order->order_sn,
  153. 'goods_name' => $order->goods->name,
  154. 'goods_traffic' => flowAutoShow($order->goods->traffic*1048576),
  155. 'port' => $order->user->port,
  156. 'passwd' => $order->user->passwd,
  157. 'method' => $order->user->method,
  158. //'protocol' => $order->user->protocol,
  159. //'protocol_param' => $order->user->protocol_param,
  160. //'obfs' => $order->user->obfs,
  161. //'obfs_param' => $order->user->obfs_param,
  162. 'created_at' => $order->created_at->toDateTimeString(),
  163. 'expire_at' => $order->expire_at
  164. ];
  165. // 获取可用节点列表
  166. $labels = UserLabel::query()->where('user_id', $order->user_id)->get()->pluck('label_id');
  167. $nodeIds = SsNodeLabel::query()->whereIn('label_id', $labels)->get()->pluck('node_id');
  168. $nodeList = SsNode::query()->whereIn('id', $nodeIds)->orderBy('sort', 'desc')->orderBy('id', 'desc')->get()->toArray();
  169. $content['serverList'] = $nodeList;
  170. $logId = Helpers::addEmailLog($order->email, $title, json_encode($content));
  171. Mail::to($order->email)->send(new sendUserInfo($logId, $content));
  172. }
  173. DB::commit();
  174. Log::info('【'.$pay_type_name.'】处理成功');
  175. } catch(Exception $e){
  176. DB::rollBack();
  177. Log::info('【'.$pay_type_name.'】回调更新支付单和订单异常:'.$e->getMessage());
  178. }
  179. return FALSE;
  180. }
  181. private function activePrepaidOrder($oid)
  182. {
  183. // 取出预支付订单
  184. $prepaidOrder = Order::query()->where('oid', $oid)->first();
  185. //去除使用中的套餐和 流量包
  186. Order::query()->where('user_id', $prepaidOrder->user_id)->where('status', 2)->where('is_expire', 0)->update(['expire_at' => date('Y-m-d H:i:s'), 'is_expire' => 1]);
  187. //取出对应套餐信息
  188. $prepaidGood = Goods::query()->where('id', $prepaidOrder->goods_id)->first();
  189. //激活预支付套餐
  190. Order::query()->where('oid', $prepaidOrder->oid)->update(['expire_at' => date("Y-m-d H:i:s", strtotime("+".$prepaidGood->days." days")), 'status' => 2]);
  191. //取出用户信息
  192. $user = User::query()->where('id', $prepaidOrder->user_id)->first();
  193. $userTraffic = $prepaidGood->traffic*1048576;
  194. //拿出可能存在的其余套餐, 推算 最新的到期时间
  195. $expire_time = date('Y-m-d', strtotime("+".$prepaidGood->days." days"));
  196. $prepaidOrders = Order::query()->where('user_id', $prepaidOrder->user_id)->where('status', 3)->get();
  197. foreach($prepaidOrders as $paidOrder){
  198. //取出对应套餐信息
  199. $goods = Goods::query()->where('id', $paidOrder->goods_id)->first();
  200. $expire_time = date('Y-m-d', strtotime("+".$goods->days." days", strtotime($expire_time)));
  201. }
  202. //计算账号下一个重置时间
  203. $nextResetTime = date('Y-m-d', strtotime("+".$prepaidGood->period." days"));
  204. if($nextResetTime >= $expire_time){
  205. $nextResetTime = NULL;
  206. }
  207. // 用户默认标签
  208. $defaultLabels = self::$systemConfig['initial_labels_for_user']? explode(',', self::$systemConfig['initial_labels_for_user']) : [];
  209. //取出 商品默认标签 & 系统默认标签 去重
  210. $newUserLabels = array_values(array_unique(array_merge(GoodsLabel::query()->where('goods_id', $prepaidOrder->goods_id)->pluck('label_id')->toArray(), $defaultLabels)));
  211. // 生成标签
  212. foreach($newUserLabels as $vo){
  213. $obj = new UserLabel();
  214. $obj->user_id = $prepaidOrder->user_id;
  215. $obj->label_id = $vo;
  216. $obj->save();
  217. }
  218. Helpers::addUserTrafficModifyLog($prepaidOrder->user_id, $prepaidOrder->oid, $user->transfer_enable, $userTraffic, '[预支付订单激活]加上用户购买的套餐流量');
  219. User::query()->where('id', $prepaidOrder->user_id)->increment('invite_num', $prepaidOrder->invite_num? : 0, ['u' => 0, 'd' => 0, 'transfer_enable' => $userTraffic, 'expire_time' => $expire_time, 'reset_time' => $nextResetTime]);
  220. }
  221. }