PaySuccessAbstract.php 1.2 KB

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