PresellOrder.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. /**
  26. * 处理支付请求
  27. *
  28. * 本函数旨在处理用户的支付操作,包括校验支付方式的有效性、检查订单状态和支付状态,
  29. * 以及根据不同的支付方式引导用户完成支付。如果订单无需支付或已支付,则直接标记支付成功。
  30. *
  31. * @param int $id 订单ID
  32. * @return mixed 返回支付结果,可能是支付成功的提示,也可能是支付网关的跳转指令
  33. */
  34. public function pay($id)
  35. {
  36. // 获取用户选择的支付方式
  37. $type = $this->request->param('type');
  38. // 校验支付方式是否有效
  39. if (!in_array($type, StoreOrderRepository::PAY_TYPE))
  40. return app('json')->fail('请选择正确的支付方式');
  41. // 根据用户ID和订单ID获取订单信息
  42. $order = $this->repository->userOrder($this->request->uid(), intval($id));
  43. // 检查订单是否存在
  44. if (!$order)
  45. throw new ValidateException('尾款订单不存在');
  46. // 检查订单是否已支付
  47. if ($order->paid)
  48. throw new ValidateException('已支付');
  49. // 检查订单是否失效
  50. if (!$order->status)
  51. throw new ValidateException('尾款订单以失效');
  52. // 检查支付时间是否有效
  53. if (strtotime($order->final_start_time) > time())
  54. throw new ValidateException('未到尾款支付时间');
  55. if (strtotime($order->final_end_time) < time())
  56. throw new ValidateException('已过尾款支付时间');
  57. // 更新订单支付方式
  58. $order->pay_type = array_search($type, StoreOrderRepository::PAY_TYPE);
  59. if ($type == 'offline') {
  60. if (!systemConfig('offline_switch')) {
  61. return app('json')->fail('未开启线下支付功能');
  62. }
  63. if (!(($order->merchant['offline_switch']) ?? '')) {
  64. return app('json')->fail('该店铺未开启线下支付');
  65. }
  66. return app('json')->status('success', '线下支付,请告知收银员', ['order_id' => $order->presell_order_id]);
  67. }
  68. $order->save();
  69. // 如果订单金额为0,直接标记支付成功并返回
  70. if ($order['pay_price'] == 0) {
  71. $this->repository->paySuccess($order);
  72. return app('json')->status('success', '支付成功', ['order_id' => $order['presell_order_id']]);
  73. }
  74. // 尝试执行支付操作,可能会根据支付方式跳转到不同的支付网关
  75. try {
  76. return $this->repository->pay($type, $this->request->userInfo(), $order, $this->request->param('return_url'), $this->request->isApp());
  77. } catch (\Exception $e) {
  78. // 支付过程中发生异常,返回错误信息
  79. return app('json')->status('error', $e->getMessage(), ['order_id' => $order->presell_order_id]);
  80. }
  81. }
  82. }