PresellOrder.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\order;
  12. use app\common\repositories\store\order\PresellOrderRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. use think\exception\ValidateException;
  17. class PresellOrder extends BaseController
  18. {
  19. protected $repository;
  20. public function __construct(App $app, PresellOrderRepository $repository)
  21. {
  22. parent::__construct($app);
  23. $this->repository = $repository;
  24. }
  25. public function pay($id)
  26. {
  27. $type = $this->request->param('type');
  28. if (!in_array($type, StoreOrderRepository::PAY_TYPE))
  29. return app('json')->fail('请选择正确的支付方式');
  30. $order = $this->repository->userOrder($this->request->uid(), intval($id));
  31. if (!$order)
  32. throw new ValidateException('尾款订单不存在');
  33. if ($order->paid)
  34. throw new ValidateException('已支付');
  35. if (!$order->status)
  36. throw new ValidateException('尾款订单以失效');
  37. if (strtotime($order->final_start_time) > time())
  38. throw new ValidateException('未到尾款支付时间');
  39. if (strtotime($order->final_end_time) < time())
  40. throw new ValidateException('已过尾款支付时间');
  41. $order->pay_type = array_search($type, StoreOrderRepository::PAY_TYPE);
  42. $order->save();
  43. if ($order['pay_price'] == 0) {
  44. $this->repository->paySuccess($order);
  45. return app('json')->status('success', '支付成功', ['order_id' => $order['presell_order_id']]);
  46. }
  47. try {
  48. return $this->repository->pay($type, $this->request->userInfo(), $order, $this->request->param('return_url'), $this->request->isApp());
  49. } catch (\Exception $e) {
  50. return app('json')->status('error', $e->getMessage(), ['order_id' => $order->presell_order_id]);
  51. }
  52. }
  53. }