OrderController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Services\OrderService\ComboOrderService;
  4. use App\Services\OrderService\GoodsOrderService;
  5. use EasyWeChat\Factory;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Support\Facades\Request;
  8. use Txj\Elastic\Facades\ES;
  9. class OrderController extends HttpBaseController
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->setWebsite(__NAMESPACE__);
  15. $this->isLoginJson();
  16. }
  17. /**
  18. * 创建订单
  19. */
  20. public function createOrder()
  21. {
  22. $requestData = Request::all();
  23. $product_type = Request::post('product_type'); // 1 服务商品 2 服务套餐
  24. if (!in_array($product_type, [1, 2])) {
  25. return responseMessage(2002, '非法提交,type参数错误!');
  26. }
  27. try {
  28. if ($product_type == 1) { // 服务商品
  29. $goodsOrder = new GoodsOrderService($this->userId);
  30. return $goodsOrder->create($requestData);
  31. } else { // 服务套餐
  32. $comboOrder = new ComboOrderService($this->userId);
  33. return $comboOrder->create($requestData);
  34. }
  35. } catch (\Exception $e) {
  36. return responseMessage(2003, '订单创建失败,请重试!');
  37. }
  38. }
  39. /**
  40. * 不停的检测,微信是否支付成功
  41. */
  42. public function checkIsWXPay()
  43. {
  44. $requestData = Request::all();
  45. $order_no = $requestData['order_no'] ?? '';
  46. if (empty($order_no)) {
  47. return responseMessage(2001, '参数错误!');
  48. }
  49. $materialService = $this->getOrderService($order_no);
  50. $orderInfo = $materialService->getOrderInfo($order_no);
  51. if ($orderInfo && $orderInfo['order_status'] == 1) {
  52. return responseMessage(2004, '支付中!');
  53. } elseif ($orderInfo['order_status'] == 2) {
  54. return responseMessage(1001, '恭喜你,支付成功!');
  55. }
  56. }
  57. ###################################################################################################################
  58. ##### 订单退款 ##########################################################################################################
  59. ###################################################################################################################
  60. /**
  61. * 申请退款
  62. */
  63. public function applyRefund()
  64. {
  65. $requestData = Request::all();
  66. $order_no = $requestData['order_no'] ?? '';
  67. $refund_reason = $requestData['refund_reason'] ?? ''; //退款原因
  68. $images = $requestData['images'] ?? ''; //退款图片
  69. if (empty($order_no)) {
  70. return responseMessage(2001, '参数错误!');
  71. }
  72. if (empty($refund_reason)) {
  73. return responseMessage(2003, '请输入退款原因!');
  74. }
  75. $refund_no = 'TG' . createNewOrderNo(); // 退款单号
  76. //判断该订单是否存在
  77. $orderInfo = ES::table('order_goods')->where(['order_no' => $order_no, 'user_id' => $this->userId])->first();
  78. if (empty($orderInfo)) {
  79. return responseMessage(2004, '该订单不存在,或已删除!');
  80. }
  81. if ($orderInfo['order_status'] != 2) {
  82. return responseMessage(2005, '该订单状态不正确,不允许退款!');
  83. }
  84. // 判断是否已经申请过了
  85. $is_exist = ES::table('order_goods_refund')->where(['order_id' => $orderInfo['id']])->count();
  86. if ($is_exist) {
  87. return responseMessage(2006, '你已经申请过了,请不要重复提交!');
  88. }
  89. // 保存售后信息
  90. // status 1 退款申请中 2 拒绝退款 3 退款完成 4 取消退款 5 退款中
  91. $is_success = ES::table('order_goods_refund')->insertGetId([
  92. 'refund_order_no' => $refund_no, // 退款单号
  93. 'order_id' => $orderInfo['id'],
  94. 'order_status' => $orderInfo['order_status'],
  95. 'user_id' => $this->userId,
  96. 'refund_reason' => $refund_reason,
  97. 'refund_images' => $images,
  98. 'refund_price' => $orderInfo['order_amount_total'], // 优惠平台之后的价格
  99. 'refund_status' => 1,
  100. ]);
  101. if ($is_success) {
  102. // 更新总订单的状态
  103. $res = ES::table('order_goods')->toRefresh()->updateEntityById($orderInfo['id'], ['order_status' => 7]);
  104. // // 消息通知
  105. // dispatch(new SendMessage($this->storeId, 'REFUND_APPLY', [
  106. // 'orderNo' => $orderInfo['order_no']
  107. // ], $this->userId));
  108. return responseMessage(1001, '操作成功', $res);
  109. } else {
  110. return responseMessage(2007, '操作失败,请重试!');
  111. }
  112. }
  113. /**
  114. * 取消退款
  115. */
  116. public function cancelRefund(): JsonResponse
  117. {
  118. $requestData = Request::all();
  119. $refund_mid = $requestData['mid'] ?? '';
  120. if (empty($refund_mid)) {
  121. return responseMessage(2001, '参数错误!');
  122. }
  123. $refundInfo = ES::table('order_goods_refund')->where('mid', $refund_mid)->where('user_id', $this->userId)->first();
  124. if (empty($refundInfo)) {
  125. return responseMessage(2005, '退款订单不存在,请重试!');
  126. }
  127. // status 1 退款申请中 2 拒绝退款 3 退款完成 4 取消退款
  128. if ($refundInfo['refund_status'] == 1) {
  129. $is_success = ES::table('order_goods_refund')->updateEntityById($refundInfo['id'], ['refund_status' => 4]);
  130. if ($is_success) {
  131. //更新总订单
  132. ES::table('order_goods')->updateEntityById($refundInfo['order_id'], ['order_status' => $refundInfo['order_status']]);
  133. return responseMessage(1001, '操作成功!');
  134. } else {
  135. return responseMessage(2007, '操作失败,请重试!');
  136. }
  137. } else {
  138. return responseMessage(2006, '非法操作!');
  139. }
  140. }
  141. /**
  142. * 通过微信接口,检测该订单的退款状态
  143. */
  144. public function checkRefundOrder()
  145. {
  146. $requestData = Request::all();
  147. $refund_no = $requestData['refund_no'] ?? '';
  148. $refund_mid = $requestData['mid'] ?? '';
  149. if (empty($refund_mid) || empty($refund_no)) {
  150. return responseMessage(2001, '非法提交!');
  151. }
  152. // 获取退款订单信息
  153. $refundInfo = ES::table('order_goods_refund')->where('mid', $refund_mid)->where('user_id', $this->userId)->first();
  154. if (empty($refundInfo)) {
  155. return responseMessage(2005, '退款订单不存在,请重试!');
  156. }
  157. if ($refundInfo['status'] == 3) {
  158. return responseMessage(2014, '退款完成');
  159. }
  160. // 获取原订单信息 order_id
  161. $orderInfo = ES::table('order_goods')->find($refundInfo['order_id']);
  162. if (empty($orderInfo)) {
  163. return responseMessage(2005, '该订单不存在,请重试!');
  164. }
  165. $config = config('wechat.payment.default');
  166. $app = Factory::payment($config);
  167. $pay_info = json_decode($orderInfo['pay_info'], true);
  168. $result = $app->refund->queryByTransactionId($pay_info['transaction_id']);
  169. if ($result['return_code'] == 'SUCCESS') {
  170. if ($result['result_code'] == 'SUCCESS') {
  171. // 更新退款状态和信息 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  172. ES::table('order_goods_refund')->updateEntityById($refundInfo['id'], ['status' => 3]);
  173. //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款
  174. ES::table('order_goods')->updateEntityById($orderInfo['id'], ['order_status' => 6]);
  175. return responseMessage(1001, '已退款!');
  176. } else {
  177. return responseMessage(2011, '');
  178. }
  179. } else {
  180. return responseMessage(2010, '');
  181. }
  182. }
  183. }