PayNoticeController.php 11 KB

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