PayNoticeController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Helper\ByteDance;
  4. use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
  5. use App\Helper\UniPlatform\Kuaishou\Payment as KuaishouPayment;
  6. use App\Models\Pay;
  7. use App\Models\UserConsumeRecord;
  8. use App\Models\UserInfo;
  9. use App\Models\UserRechargeRecord;
  10. use App\Models\UserVipRecord;
  11. use Carbon\Carbon;
  12. use Illuminate\Support\Facades\Log;
  13. class PayNoticeController extends Controller
  14. {
  15. public function bytedance()
  16. {
  17. $factory = $this->getUniFactory();
  18. $app = new BytedancePayment($factory);
  19. return $app->payNotify(function ($data, $fail) use ($factory) {
  20. //处理支付订单
  21. try {
  22. \DB::beginTransaction();
  23. $payId = $data['cp_orderno'];
  24. $pay = Pay::find($payId);
  25. if (!$pay) { // 如果订单不存在
  26. return true;
  27. }
  28. $pay->pay_type = $data['way'];
  29. $pay->serial_number = $data['channel_no'];
  30. $pay->status = $data['status'] == 'SUCCESS';
  31. $pay->pay_dt = Carbon::now()->toDateTimeString();
  32. $pay->save();
  33. // 处理
  34. if ($pay->source == Pay::SOURCE_RECHARGE) {
  35. $goods = $this->recharge($payId);
  36. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  37. $goods = $this->buyVip($payId);
  38. }
  39. if(isset($goods)){
  40. /* @var Pay $payInfo*/
  41. $payInfo = Pay::with(['user'])->where('id', $payId)->first($payId);
  42. $factory->pushOrder($payInfo->user->open_id, $payId, $goods ,'已支付');
  43. }
  44. \DB::commit();
  45. } catch (\Exception $e) {
  46. \DB::rollBack();
  47. \Log::error('抖音支付回调错误==>'.$e->getMessage());
  48. return $fail('通信失败,请稍后再通知我');
  49. }
  50. return true;
  51. });
  52. }
  53. public function kuaishou()
  54. {
  55. $app = new KuaishouPayment($this->getUniFactory(2));
  56. return $app->payNotify(function ($data, $fail) {
  57. //处理支付订单
  58. try {
  59. \DB::beginTransaction();
  60. $payId = $data['out_order_no'];
  61. $pay = Pay::find($payId);
  62. if (!$pay) { // 如果订单不存在
  63. return true;
  64. }
  65. $payType = 11;
  66. if($data['channel'] == 'WECHAT'){
  67. $payType = 1;
  68. }
  69. if($data['channel'] == 'ALIPAY'){
  70. $payType = 2;
  71. }
  72. $pay->pay_type = $payType;
  73. $pay->serial_number = $data['trade_no'];
  74. $pay->status = $data['status'] == 'SUCCESS';
  75. $pay->pay_dt = Carbon::now()->toDateTimeString();
  76. $pay->save();
  77. // 处理
  78. if ($pay->source == Pay::SOURCE_RECHARGE) {
  79. $this->recharge($payId);
  80. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  81. $this->buyVip($payId);
  82. }
  83. \DB::commit();
  84. } catch (\Exception $e) {
  85. \DB::rollBack();
  86. \Log::error('快手支付回调错误==>'.$e->getMessage());
  87. return $fail('通信失败,请稍后再通知我');
  88. }
  89. return true;
  90. });
  91. }
  92. private function recharge($payId)
  93. {
  94. /* @var UserRechargeRecord $record */
  95. $record = UserRechargeRecord::with(['combo'])->where('pay_id', $payId)->first();
  96. if (!$record) {
  97. return true;
  98. }
  99. $user = UserInfo::find($record->user_id);
  100. // 记录日志
  101. $gold = $record->gold + $record->gift;
  102. app(UserConsumeRecord::class)->record(
  103. $user->user_id,
  104. UserConsumeRecord::TYPE_RECHARGE,
  105. $gold,
  106. "购买金币,combo_id:{$record->combo_id}"
  107. );
  108. // 发放奖励
  109. $user->integral = $user->integral + $gold;
  110. $user->total_integral = $user->total_integral + $gold;
  111. $user->save();
  112. $record->status = 1;
  113. $record->save();
  114. return [
  115. 'id' => $record->combo_id,
  116. 'title' => $record->combo->name,
  117. 'price' => $record->combo->price
  118. ];
  119. }
  120. private function buyVip($payId)
  121. {
  122. /* @var UserVipRecord $record */
  123. $record = UserVipRecord::with(['combo'])->where('pay_id', $payId)->first();
  124. if (!$record) {
  125. return true;
  126. }
  127. $user = UserInfo::find($record->user_id);
  128. if (empty($user->is_vip)) {
  129. $user->is_vip = 1;
  130. $user->start_at = Carbon::now()->toDateString();
  131. $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
  132. } else {
  133. list($year, $month, $day) = explode('-', $user->end_at);
  134. $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
  135. }
  136. $user->save();
  137. $record->status = 1;
  138. $record->save();
  139. return [
  140. 'id' => $record->combo_id,
  141. 'title' => $record->combo->name,
  142. 'price' => $record->combo->price
  143. ];
  144. }
  145. }