PayNoticeController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
  4. use App\Helper\UniPlatform\Kuaishou\Payment as KuaishouPayment;
  5. use App\Models\Config;
  6. use App\Models\Giveaway;
  7. use App\Models\Order;
  8. use App\Models\Pay;
  9. use App\Models\Share;
  10. use App\libs\helpers\Helper;
  11. use App\Models\PaymentConfig;
  12. use App\Models\ShareConfig;
  13. use App\Models\User;
  14. use App\Models\UserConsumeRecord;
  15. use App\Models\UserInfo;
  16. use App\Models\UserRechargeRecord;
  17. use App\Models\UserShare;
  18. use App\Models\UserVipRecord;
  19. use Carbon\Carbon;
  20. use Illuminate\Http\JsonResponse;
  21. use Illuminate\Support\Facades\Log;
  22. class PayNoticeController extends Controller
  23. {
  24. public function bytedance(): JsonResponse
  25. {
  26. $factory = $this->getUniFactory();
  27. $app = new BytedancePayment($factory);
  28. return $app->payNotify(function ($data, $fail) use ($factory) {
  29. // 处理支付订单
  30. try {
  31. \DB::beginTransaction();
  32. $payId = $data['cp_orderno'];
  33. $pay = Pay::find($payId);
  34. if (!$pay) { // 如果订单不存在
  35. return true;
  36. }
  37. $pay->pay_type = $data['way'];
  38. $pay->serial_number = $data['channel_no'];
  39. $pay->status = 'SUCCESS' == $data['status'];
  40. $pay->pay_dt = Carbon::now()->toDateTimeString();
  41. $pay->save();
  42. // 处理
  43. if (Pay::SOURCE_RECHARGE == $pay->source) {
  44. $goods = $this->recharge($payId);
  45. } elseif (Pay::SOURCE_BUY_VIP == $pay->source) {
  46. $goods = $this->buyVip($payId);
  47. }
  48. if (isset($goods)) {
  49. /* @var Pay $payInfo */
  50. $payInfo = Pay::with(['user'])->where('pay_id', $payId)->first();
  51. $factory->pushOrder($payInfo->user->open_id, $payId, $goods, '已支付');
  52. }
  53. \DB::commit();
  54. // 成为分销商
  55. (new User())->beComeShare($pay->user_id);
  56. } catch (\Exception $e) {
  57. \DB::rollBack();
  58. \Log::error('抖音支付回调错误==>' . $e->getMessage());
  59. return $fail('通信失败,请稍后再通知我');
  60. }
  61. return true;
  62. });
  63. }
  64. public function kuaishou(): JsonResponse
  65. {
  66. $factory = $this->getUniFactory(2);
  67. $app = new KuaishouPayment($factory);
  68. return $app->payNotify(function ($data, $fail) use ($factory) {
  69. // 处理支付订单
  70. try {
  71. \DB::beginTransaction();
  72. $payId = $data['out_order_no'];
  73. $pay = Pay::find($payId);
  74. if (!$pay) { // 如果订单不存在
  75. return true;
  76. }
  77. $payType = 11;
  78. if ('WECHAT' == $data['channel']) {
  79. $payType = 1;
  80. }
  81. if ('ALIPAY' == $data['channel']) {
  82. $payType = 2;
  83. }
  84. $pay->pay_type = $payType;
  85. $pay->serial_number = $data['trade_no'];
  86. $pay->status = 'SUCCESS' == $data['status'];
  87. $pay->pay_dt = Carbon::now()->toDateTimeString();
  88. $pay->save();
  89. // 处理
  90. if (Pay::SOURCE_RECHARGE == $pay->source) {
  91. $goods = $this->recharge($payId);
  92. } elseif (Pay::SOURCE_BUY_VIP == $pay->source) {
  93. $goods = $this->buyVip($payId);
  94. }
  95. if (isset($goods)) {
  96. /* @var Pay $payInfo */
  97. $payInfo = Pay::with(['user'])->where('pay_id', $payId)->first();
  98. $factory->pushOrder($payInfo->user->open_id, $payId, $payInfo->created_at);
  99. }
  100. \DB::commit();
  101. // 成为分销商
  102. (new User())->beComeShare($pay->user_id);
  103. } catch (\Exception $e) {
  104. \DB::rollBack();
  105. \Log::error('快手支付回调错误==>' . $e->getMessage());
  106. return $fail('通信失败,请稍后再通知我');
  107. }
  108. return true;
  109. });
  110. }
  111. public function kuaishouSettle()
  112. {
  113. $app = new KuaishouPayment($this->getUniFactory(2));
  114. return $app->payNotify(function ($data, $fail) {
  115. return true;
  116. });
  117. }
  118. /**
  119. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  120. */
  121. public function wechat()
  122. {
  123. $param = file_get_contents('php://input');
  124. $params = Helper::xmlToArray($param);
  125. Log::info('微信支付回调==>' . json_encode($params, JSON_UNESCAPED_SLASHES));
  126. // 处理支付订单
  127. try {
  128. $payId = $params['out_trade_no'];
  129. $pay = Order::query()->where('order', $payId)->where('state', 0)->first();
  130. if (!$pay) { // 如果订单不存在
  131. return true;
  132. }
  133. if ('SUCCESS' === $params['return_code']) {
  134. if ('SUCCESS' === $params['result_code']) {
  135. $pay->state = 1;
  136. $pay->wx_order = $params['transaction_id'];
  137. $pay->amount = $params['total_fee'];
  138. User::query()->where('id', $pay->user_id)->increment('diamond', $pay->diamond);
  139. $user = User::query()->find($pay->user_id);
  140. if (!empty($user->share_pid)) {
  141. $userShare = User::query()->find($user->share_pid);
  142. $payConfig = PaymentConfig::query()->where('id', $pay->config_id)->first();
  143. (float) $commission = Config::query()->where('key', 'commission')->value('value');
  144. if ($userShare->is_share) {
  145. $number = $pay->amount * ($commission / 100);
  146. if ($number < 0.01) {
  147. $number = 0.01;
  148. }
  149. $income = $number * 100;
  150. User::query()->where('id', $user->share_pid)->increment('income', $income);
  151. Giveaway::query()->create([
  152. 'user_id' => $user->share_pid,
  153. 'purchaser_user_id' => $pay->user_id,
  154. 'amount' => $income,
  155. 'title' => "购买了{$payConfig->amount}的{$payConfig->title},您获得了佣金{$number}元",
  156. 'order_id' => $pay->id,
  157. ]);
  158. } else {
  159. if ($payConfig->award_count > 0) {
  160. Share::query()->create([
  161. 'user_id' => $user->share_pid,
  162. 'pid' => $user->id,
  163. 'diamond' => $payConfig->award_count,
  164. 'desc' => '通过购买套餐' . $payConfig->amount . '的' . $payConfig->title . '获得' . $payConfig->award_count . '次数',
  165. ]);
  166. User::query()->where('id', $user->share_pid)->increment('diamond', $payConfig->award_count);
  167. }
  168. }
  169. }
  170. } elseif ('FAIL' === $params['result_code']) {
  171. $pay->state = 2;
  172. }
  173. } else {
  174. return '通信失败,请稍后再通知我';
  175. }
  176. $pay->pay_at = date('Y-m-d H:i:s'); // 更新支付时间为当前时间
  177. $pay->save();
  178. } catch (\Exception $e) {
  179. Log::error('微信支付回调错误==>' . $e->getMessage());
  180. return '通信失败,请稍后再通知我';
  181. }
  182. }
  183. private function recharge($payId)
  184. {
  185. /* @var UserRechargeRecord $record */
  186. $record = UserRechargeRecord::with(['combo'])->where('pay_id', $payId)->first();
  187. if (!$record) {
  188. return true;
  189. }
  190. $user = UserInfo::find($record->user_id);
  191. // 记录日志
  192. $gold = $record->gold + $record->gift;
  193. app(UserConsumeRecord::class)->record(
  194. $user->user_id,
  195. UserConsumeRecord::TYPE_RECHARGE,
  196. $gold,
  197. "购买金币,combo_id:{$record->combo_id}"
  198. );
  199. // 发放奖励
  200. $user->integral = $user->integral + $gold;
  201. $user->total_integral = $user->total_integral + $gold;
  202. $user->save();
  203. $record->status = 1;
  204. $record->save();
  205. // 分销
  206. $this->share($record, 2);
  207. return [
  208. 'id' => $record->combo_id,
  209. 'title' => $record->combo->name,
  210. 'price' => $record->combo->price,
  211. 'img' => '',
  212. ];
  213. }
  214. private function buyVip($payId)
  215. {
  216. /* @var UserVipRecord $record */
  217. $record = UserVipRecord::with(['combo'])->where('pay_id', $payId)->first();
  218. if (!$record) {
  219. return true;
  220. }
  221. $user = UserInfo::find($record->user_id);
  222. if (empty($user->is_vip)) {
  223. $user->is_vip = 1;
  224. $user->start_at = Carbon::now()->toDateString();
  225. $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
  226. } else {
  227. list($year, $month, $day) = explode('-', $user->end_at);
  228. $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
  229. }
  230. $user->save();
  231. $record->status = 1;
  232. $record->save();
  233. // 分销
  234. $this->share($record, 1);
  235. return [
  236. 'id' => $record->combo_id,
  237. 'title' => $record->combo->name,
  238. 'price' => $record->combo->price,
  239. 'img' => '',
  240. ];
  241. }
  242. /**
  243. * @param UserVipRecord|UserRechargeRecord $record
  244. *
  245. * @return void
  246. */
  247. private function share($record, $type)
  248. {
  249. $user = User::find($record->user_id);
  250. // 一级分销
  251. if ($user->parent_id) {
  252. $config = ShareConfig::first();
  253. $pay = Pay::find($record->pay_id);
  254. if (1 == $config->share_type) { // 百分比
  255. $income = $pay->order_fee * $config->share_number / 100;
  256. } else { // 固定
  257. $income = $config->share_number;
  258. }
  259. $share = new UserShare();
  260. $share->user_id = $user->parent_id;
  261. $share->child_id = $user->id;
  262. $share->order_type = $type;
  263. $share->order_id = $record->pay_id;
  264. $share->income = $income;
  265. $share->save();
  266. $parent = User::find($user->parent_id);
  267. $parent->income = $user->income + $income;
  268. $parent->total_income = $user->total_income + $income;
  269. $parent->save();
  270. }
  271. }
  272. }