PresellOrder.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace app\controller\api\store\order;
  3. use app\common\repositories\store\order\PresellOrderRepository;
  4. use app\common\repositories\store\order\StoreOrderRepository;
  5. use ln\basic\BaseController;
  6. use think\App;
  7. use think\exception\ValidateException;
  8. class PresellOrder extends BaseController
  9. {
  10. protected $repository;
  11. public function __construct(App $app, PresellOrderRepository $repository)
  12. {
  13. parent::__construct($app);
  14. $this->repository = $repository;
  15. }
  16. public function pay($id)
  17. {
  18. $type = $this->request->param('type');
  19. if (!in_array($type, StoreOrderRepository::PAY_TYPE))
  20. return app('json')->fail('请选择正确的支付方式');
  21. $order = $this->repository->userOrder($this->request->uid(), intval($id));
  22. if (!$order)
  23. throw new ValidateException('尾款订单不存在');
  24. if ($order->paid)
  25. throw new ValidateException('已支付');
  26. if (!$order->status)
  27. throw new ValidateException('尾款订单以失效');
  28. if (strtotime($order->final_start_time) > time())
  29. throw new ValidateException('未到尾款支付时间');
  30. if (strtotime($order->final_end_time) < time())
  31. throw new ValidateException('已过尾款支付时间');
  32. $order->pay_type = array_search($type, StoreOrderRepository::PAY_TYPE);
  33. $order->save();
  34. if ($order['pay_price'] == 0) {
  35. $this->repository->paySuccess($order);
  36. return app('json')->status('success', '支付成功', ['order_id' => $order['presell_order_id']]);
  37. }
  38. try {
  39. return $this->repository->pay($type, $this->request->userInfo(), $order, $this->request->param('return_url'), $this->request->isApp());
  40. } catch (\Exception $e) {
  41. return app('json')->status('error', $e->getMessage(), ['order_id' => $order->presell_order_id]);
  42. }
  43. }
  44. }