PaySuccessAbstract.php 1.6 KB

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