PayNoticeController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
  4. use App\Models\Pay;
  5. use App\Models\UserConsumeRecord;
  6. use App\Models\UserInfo;
  7. use App\Models\UserRechargeRecord;
  8. use App\Models\UserVipRecord;
  9. use Carbon\Carbon;
  10. class PayNoticeController extends Controller
  11. {
  12. public function bytedance()
  13. {
  14. $app = new BytedancePayment($this->getUniFactory());
  15. return $app->payNotify(function ($data, $fail) {
  16. //处理支付订单
  17. try {
  18. \DB::beginTransaction();
  19. $payId = $data['cp_orderno'];
  20. $pay = Pay::find($payId);
  21. if (!$pay) { // 如果订单不存在
  22. return true;
  23. }
  24. $pay->pay_type = $data['way'];
  25. $pay->serial_number = $data['channel_no'];
  26. $pay->status = $data['status'] == 'SUCCESS';
  27. $pay->pay_dt = Carbon::now()->toDateString();
  28. $pay->save();
  29. // 处理
  30. if ($pay->source == Pay::SOURCE_RECHARGE) {
  31. $this->recharge($payId);
  32. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  33. $this->buyVip($payId);
  34. }
  35. \DB::commit();
  36. } catch (\Exception $e) {
  37. \DB::rollBack();
  38. return $fail('通信失败,请稍后再通知我');
  39. }
  40. return true;
  41. });
  42. }
  43. public function kuaishou()
  44. {
  45. }
  46. private function recharge($payId)
  47. {
  48. /* @var UserRechargeRecord $record */
  49. $record = UserRechargeRecord::where('pay_id', $payId)->first();
  50. if (!$record) {
  51. return true;
  52. }
  53. $user = UserInfo::find($record->user_id);
  54. // 记录日志
  55. $gold = $record->gold + $record->gift;
  56. app(UserConsumeRecord::class)->record(
  57. $user->user_id,
  58. UserConsumeRecord::TYPE_RECHARGE,
  59. $gold,
  60. "购买金币,combo_id:{$record->combo_id}"
  61. );
  62. // 发放奖励
  63. $user->integral = $user->integral + $gold;
  64. $user->total_integral = $user->total_integral + $gold;
  65. $user->save();
  66. $record->status = 1;
  67. $record->save();
  68. }
  69. private function buyVip($payId)
  70. {
  71. /* @var UserVipRecord $record */
  72. $record = UserVipRecord::where('pay_id', $payId)->first();
  73. if (!$record) {
  74. return true;
  75. }
  76. $user = UserInfo::find($record->user_id);
  77. if (empty($user->is_vip)) {
  78. $user->is_vip = 1;
  79. $user->start_at = Carbon::now()->toDateString();
  80. $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
  81. } else {
  82. list($year, $month, $day) = explode('-', $user->end_at);
  83. $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
  84. }
  85. $user->save();
  86. $record->status = 1;
  87. $record->save();
  88. }
  89. }