PaySuccessAbstract.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Modules\Trade\Pay;
  3. use App\Jobs\QappTikTokUserCharge;
  4. use App\Modules\Book\Models\BookConfig;
  5. use App\Modules\Subscribe\Models\Order;
  6. use App\Modules\User\Services\ReadRecordService;
  7. use Exception;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. *
  12. * @property \App\Modules\Subscribe\Models\Order $order
  13. */
  14. abstract class PaySuccessAbstract
  15. {
  16. protected $order;
  17. /**
  18. * 处理支付成功的逻辑
  19. */
  20. abstract protected function handlePayProcess();
  21. public function __construct(Order $order)
  22. {
  23. $this->order = $order;
  24. }
  25. /**
  26. * 支付成功入口
  27. */
  28. public function success()
  29. {
  30. try {
  31. DB::beginTransaction();
  32. $this->setOrderSuccess();
  33. $this->handlePayProcess();
  34. DB::commit();
  35. $this->addQueue();
  36. return true;
  37. } catch (Exception $e) {
  38. DB::rollback();
  39. Log::error('pay.success: ' . $e->getTraceAsString());
  40. }
  41. }
  42. /**
  43. * 设置order成功
  44. */
  45. protected function setOrderSuccess()
  46. {
  47. $this->order->pay_type = $this->getChargeTimes();
  48. $this->order->status = 'PAID';
  49. $this->order->pay_end_at = date('Y-m-d H:i:s');
  50. $this->order->save();
  51. }
  52. /**
  53. * 获取用户第几次充值
  54. * @param $uid
  55. * @return int
  56. */
  57. private function getChargeTimes()
  58. {
  59. $count = Order::where('uid', $this->order->uid)->where('status', 'PAID')->count('id');
  60. return $count + 1;
  61. }
  62. protected function addQueue()
  63. {
  64. $bid = ReadRecordService::getSimpleFirstReadRecord($this->order->uid);
  65. $book_id = book_hash_encode($bid);
  66. $book = BookConfig::where('bid', $bid)->select('book_name')->first();
  67. $book_name = $book ? $book->book_name : '';
  68. $job = new QappTikTokUserCharge($this->order->uid, $this->order->price, $this->order->created_at, QappTikTokUserCharge::CURRENT_DAY_REGISTER, $book_id, $book_name);
  69. dispatch($job->onConnection('rabbitmq')->onQueue('qapp_tiktok_user_charge_queue'));
  70. }
  71. }