OrderController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Admin;
  3. use Illuminate\Support\Facades\Request;
  4. class OrderController extends AdminBaseController
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->setWebsite(__NAMESPACE__);
  10. }
  11. ##################################################################################################################
  12. ### 订单退款 ################################################################################################
  13. ##################################################################################################################
  14. /**
  15. * 确认退款
  16. * 注意:
  17. * 1、交易时间超过一年的订单无法提交退款
  18. * 2、微信支付退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号
  19. *
  20. * 退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  21. *
  22. * @return String
  23. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  24. */
  25. public function ensureRefund()
  26. {
  27. $requestData = Request::all();
  28. $md5_id = $requestData['mid'] ?? '';
  29. $price = $requestData['price'] ?? '';
  30. $order_type = $requestData['order_type'] ?? '';
  31. if (empty($md5_id) || empty($price) || empty($order_type)) {
  32. return responseMessage(2001, '非法提交!');
  33. }
  34. if ($order_type == 1) { // 电商
  35. $orderAlias = $this->setAliasCommerceOrder;
  36. $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
  37. } elseif ($order_type == 2) { // 预约
  38. $orderAlias = $this->setAliasTostoreOrder;
  39. $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
  40. } else {
  41. return responseMessage(2003, '该产品不支持!');
  42. }
  43. // 获取订单信息 order_id
  44. $orderService = new MaterialService(new EavSetService($orderAlias));
  45. $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
  46. if (empty($orderInfo)) {
  47. return responseMessage(2005, '该订单不存在,请重试!');
  48. }
  49. if ($price > $orderInfo['order_amount_total']) {
  50. return responseMessage(2006, '修改的价格,必须小于或等于原价');
  51. }
  52. $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
  53. if (empty($payConfig)) {
  54. return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
  55. }
  56. //pay_type 1 微信小程序 2 微信公众号 3支付宝
  57. if ($orderInfo['pay_channel'] == 1) {
  58. $app = $this->wxGateway($payConfig, $this->storeId);
  59. } else {
  60. $app = $this->wechatGateway($payConfig, $this->storeId);
  61. }
  62. $pay_info = json_decode($orderInfo['pay_info'], true);
  63. $result = $app->refund->byTransactionId($pay_info['transaction_id'], $refund_no, $orderInfo['order_amount_total'] * 100, $price * 100, [
  64. 'refund_desc' => $refundInfo['refund_reason']
  65. ]);
  66. if ($result['return_code'] == 'SUCCESS') {
  67. if ($result['result_code'] == 'SUCCESS') {
  68. // 更新退款状态和信息
  69. $refundOrderService->update($refundInfo['id'], [
  70. 'status' => 5, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  71. 'refund_true_price' => $price,
  72. 'refund_info' => json_encode($result)
  73. ]);
  74. // 更新退款产品的状态
  75. $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
  76. $refundInfo['product_id'] = intval($refundInfo['product_id']);
  77. if ($refundInfo['product_id']) { // 拼团的订单,该字段为空值
  78. $orderProductService->update($refundInfo['product_id'], [
  79. 'status' => 5 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  80. ]);
  81. }
  82. // 消息通知
  83. // dispatch(new SendMessage($this->storeId, 'REFUND_SUCCESS', ['orderNo' => $orderInfo['order_no']], $orderInfo['member_id']));
  84. return responseMessage(1001, '退款操作成功!');
  85. } else {
  86. return responseMessage(2011, '操作失败:' . $result['err_code'] . '-' . $result['err_code_des']);
  87. }
  88. } else {
  89. return responseMessage(2010, '操作失败:' . $result['return_msg']);
  90. }
  91. }
  92. /**
  93. * 通过微信接口,检测该订单的退款状态
  94. */
  95. public function checkRefundOrder()
  96. {
  97. $requestData = Request::all();
  98. $refund_no = $requestData['refund_no'] ?? '';
  99. $md5_id = $requestData['mid'] ?? '';
  100. $order_type = $requestData['order_type'] ?? '';
  101. if (empty($md5_id) || empty($refund_no) || empty($order_type)) {
  102. return $this->responseMessage(2001, '非法提交!');
  103. }
  104. if ($order_type == 1) { // 电商
  105. $refundAlias = $this->setAliasCommerceRefund;
  106. $orderAlias = $this->setAliasCommerceOrder;
  107. $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
  108. } elseif ($order_type == 2) { // 预约
  109. $refundAlias = $this->setAliasTostoreRefund;
  110. $orderAlias = $this->setAliasTostoreOrder;
  111. $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
  112. } elseif ($order_type == 3) { // 餐饮
  113. $refundAlias = $this->setAliasTakeoutRefund;
  114. $orderAlias = $this->setAliasTakeoutOrder;
  115. $aliasOrderProduct = $this->setAliasTakeoutOrderProduct;
  116. } else {
  117. return $this->responseMessage(2003, '该产品不支持!');
  118. }
  119. // 获取原订单信息 order_id
  120. $orderService = new MaterialService(new EavSetService($orderAlias));
  121. $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
  122. if (empty($orderInfo)) {
  123. return $this->responseMessage(2005, '该订单不存在,请重试!');
  124. }
  125. if ($orderInfo['status'] == 3) {
  126. return $this->responseMessage(2014, '退款完成');
  127. }
  128. $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
  129. if (empty($payConfig)) {
  130. return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
  131. }
  132. //pay_type 1 微信小程序 2 微信公众号 3支付宝
  133. if ($orderInfo['pay_channel'] == 1) {
  134. $app = $this->wxGateway($payConfig, $this->storeId);
  135. } else {
  136. $app = $this->wechatGateway($payConfig, $this->storeId);
  137. }
  138. $pay_info = json_decode($orderInfo['pay_info'], true);
  139. $result = $app->refund->queryByTransactionId($pay_info['transaction_id']);
  140. if ($result['return_code'] == 'SUCCESS') {
  141. if ($result['result_code'] == 'SUCCESS') {
  142. // 更新退款状态和信息
  143. $refundOrderService->update($refundInfo['id'], [
  144. 'status' => 3, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  145. ]);
  146. //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款
  147. $orderService->update($orderInfo['id'], ['order_status' => 8]);
  148. // 更新退款产品的状态
  149. $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
  150. $orderProductService->update($refundInfo['product_id'], [
  151. 'status' => 3 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  152. ]);
  153. return $this->responseMessage(1001, '已退款!');
  154. } else {
  155. return $this->responseMessage(2011, '');
  156. }
  157. } else {
  158. return $this->responseMessage(2010, '');
  159. }
  160. }
  161. }