1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Modules\Trade\Pay;
- use App\Jobs\QappTikTokUserCharge;
- use App\Modules\Book\Models\BookConfig;
- use App\Modules\Subscribe\Models\Order;
- use App\Modules\User\Services\ReadRecordService;
- use Exception;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- *
- * @property \App\Modules\Subscribe\Models\Order $order
- */
- abstract class PaySuccessAbstract
- {
- protected $order;
- /**
- * 处理支付成功的逻辑
- */
- abstract protected function handlePayProcess();
- public function __construct(Order $order)
- {
- $this->order = $order;
- }
- /**
- * 支付成功入口
- */
- public function success()
- {
- try {
- DB::beginTransaction();
- $this->setOrderSuccess();
- $this->handlePayProcess();
- DB::commit();
- $this->addQueue();
- return true;
- } catch (Exception $e) {
- DB::rollback();
- Log::error('pay.success: ' . $e->getTraceAsString());
- }
- }
- /**
- * 设置order成功
- */
- protected function setOrderSuccess()
- {
- $this->order->pay_type = $this->getChargeTimes();
- $this->order->status = 'PAID';
- $this->order->pay_end_at = date('Y-m-d H:i:s');
- $this->order->save();
- }
- /**
- * 获取用户第几次充值
- * @param $uid
- * @return int
- */
- private function getChargeTimes()
- {
- $count = Order::where('uid', $this->order->uid)->where('status', 'PAID')->count('id');
- return $count + 1;
- }
- protected function addQueue()
- {
- $bid = ReadRecordService::getSimpleFirstReadRecord($this->order->uid);
- $book_id = book_hash_encode($bid);
- $book = BookConfig::where('bid', $bid)->select('book_name')->first();
- $book_name = $book ? $book->book_name : '';
- $job = new QappTikTokUserCharge($this->order->uid, $this->order->price, $this->order->created_at, QappTikTokUserCharge::CURRENT_DAY_REGISTER, $book_id, $book_name);
- dispatch($job->onConnection('rabbitmq')->onQueue('qapp_tiktok_user_charge_queue'));
- }
- }
|