PaySuccessAbstract.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Modules\Trade\Pay;
  3. use App\Modules\Subscribe\Models\Order;
  4. use Exception;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. *
  9. * @property \App\Modules\Subscribe\Models\Order $order
  10. */
  11. abstract class PaySuccessAbstract
  12. {
  13. protected $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. return true;
  33. } catch (Exception $e) {
  34. DB::rollback();
  35. Log::error('pay.success: ' . $e->getTraceAsString());
  36. }
  37. }
  38. /**
  39. * 设置order成功
  40. */
  41. protected function setOrderSuccess()
  42. {
  43. $this->order->pay_type = $this->getChargeTimes();
  44. $this->order->status = 'PAID';
  45. $this->order->pay_end_at = date('Y-m-d H:i:s');
  46. $this->order->save();
  47. }
  48. /**
  49. * 获取用户第几次充值
  50. * @param $uid
  51. * @return int
  52. */
  53. private function getChargeTimes()
  54. {
  55. $count = Order::where('uid', $this->order->uid)->where('status', 'PAID')->count('id');
  56. return $count + 1;
  57. }
  58. }