RefundLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam-段誉
  15. // +----------------------------------------------------------------------
  16. namespace app\common\logic;
  17. use app\common\service\pay\MixedPayService;
  18. use app\common\enum\AfterSaleEnum;
  19. use app\common\enum\AfterSaleLogEnum;
  20. use app\common\enum\DeliveryEnum;
  21. use app\common\enum\OrderEnum;
  22. use app\common\enum\PayEnum;
  23. use app\common\model\AfterSale;
  24. use app\common\model\Order;
  25. use app\common\model\OrderGoods;
  26. use app\common\model\Refund;
  27. use app\common\service\after_sale\AfterSaleService;
  28. use app\common\service\pay\AliPayService;
  29. use app\common\service\pay\BalancePayService;
  30. use app\common\service\pay\ToutiaoPayService;
  31. use app\common\service\pay\WeChatPayService;
  32. use app\common\service\WeChatConfigService;
  33. use think\db\exception\DataNotFoundException;
  34. use think\db\exception\DbException;
  35. use think\db\exception\ModelNotFoundException;
  36. /**
  37. * 订单退款逻辑
  38. * Class OrderRefundLogic
  39. * @package app\common\logic
  40. */
  41. class RefundLogic extends BaseLogic
  42. {
  43. protected static $refund;
  44. /**
  45. * @notes 发起退款
  46. * @param $refundWay //退款类型;(原路退,退回到余额)
  47. * @param $order //订单信息
  48. * @param $afterSaleId //售后退款id
  49. * @param $refundAmount //退款金额
  50. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  51. * @author 段誉
  52. * @date 2021/8/9 17:31
  53. */
  54. public static function refund($refundWay, $order, $afterSaleId, $refundAmount)
  55. {
  56. if ($refundAmount < 0) {
  57. return false;
  58. }
  59. self::log($order, $afterSaleId, $refundAmount);
  60. if ($refundAmount == 0) {
  61. //更新退款日志记录
  62. Refund::update([
  63. 'refund_status' => 1
  64. ], ['id' => self::$refund['id']]);
  65. //更新售后状态
  66. $afterSale = AfterSale::findOrEmpty($afterSaleId);
  67. // 整单退款
  68. if($afterSale['refund_type'] == AfterSaleEnum::REFUND_TYPE_ORDER) {
  69. $order = Order::findOrEmpty($afterSale['order_id'])->toArray();
  70. $refundStauts = ($afterSale['refund_total_amount'] == $order['order_amount']) ? AfterSaleEnum::FULL_REFUND : AfterSaleEnum::PARTIAL_REFUND;
  71. }
  72. // 商品售后
  73. if($afterSale['refund_type'] == AfterSaleEnum::REFUND_TYPE_GOODS) {
  74. $orderGoods = OrderGoods::findOrEmpty($afterSale['order_goods_id'])->toArray();
  75. $refundStauts = ($afterSale['refund_total_amount'] == $orderGoods['total_pay_price']) ? AfterSaleEnum::FULL_REFUND : AfterSaleEnum::PARTIAL_REFUND;
  76. }
  77. $afterSale->status = AfterSaleEnum::STATUS_SUCCESS;
  78. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_SUCCESS;
  79. $afterSale->refund_status = $refundStauts ?? AfterSaleEnum::FULL_REFUND;
  80. $afterSale->save();
  81. AfterSaleService::createAfterLog($afterSale['id'], '系统已完成退款', 0, AfterSaleLogEnum::ROLE_SYS);
  82. return true;
  83. }
  84. //区分原路退 还是退回到余额
  85. if ($refundWay == AfterSaleEnum::REFUND_WAYS_BALANCE) {
  86. self::balancePayRefund($order, $refundAmount, $afterSaleId);
  87. return true;
  88. }
  89. switch ($order['pay_way']) {
  90. //余额退款
  91. case PayEnum::BALANCE_PAY:
  92. self::balancePayRefund($order, $refundAmount,$afterSaleId);
  93. break;
  94. //微信退款
  95. case PayEnum::WECHAT_PAY:
  96. self::wechatPayRefund($order, $refundAmount);
  97. break;
  98. //支付宝退款
  99. case PayEnum::ALI_PAY:
  100. self::aliPayRefund($order, $refundAmount);
  101. break;
  102. //字节退款
  103. case PayEnum::BYTE_PAY:
  104. self::bytePayRefund($order, $refundAmount);
  105. break;
  106. //混合支付退款
  107. case PayEnum::MIXED_PAY:
  108. self::mixedPayRefund($order, $refundAmount, $afterSaleId);
  109. break;
  110. // 线下支付 直接完成
  111. case PayEnum::OFFLINE_PAY:
  112. return true;
  113. break;
  114. }
  115. return true;
  116. }
  117. /**
  118. * @notes 余额退款
  119. * @param $order
  120. * @param $refundAmount
  121. * @author 段誉
  122. * @date 2021/8/5 10:25
  123. */
  124. public static function balancePayRefund($order, $refundAmount,$afterSaleId)
  125. {
  126. (new BalancePayService())->refund($order, $refundAmount,$afterSaleId);
  127. }
  128. /**
  129. * @notes 微信退款
  130. * @param $refundWay
  131. * @param $order
  132. * @param $refundAmount
  133. * @return bool|void
  134. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  135. * @author 段誉
  136. * @date 2021/8/5 10:25
  137. */
  138. public static function wechatPayRefund($order, $refundAmount)
  139. {
  140. $pay = new WeChatPayService($order['order_terminal']);
  141. $result = $pay->refund([
  142. 'transaction_id' => $order['transaction_id'],
  143. 'refund_sn' => self::$refund['sn'],
  144. 'total_fee' => $order['order_amount'],
  145. 'refund_fee' => $refundAmount,
  146. ]);
  147. if ($result !== true) {
  148. throw new \Exception($pay->realPay()->getMessage());
  149. }
  150. //更新退款日志记录
  151. Refund::update([
  152. 'wechat_refund_id' => 0,
  153. 'refund_status' => 1,
  154. 'refund_msg' => json_encode([], JSON_UNESCAPED_UNICODE),
  155. ], ['id' => self::$refund['id']]);
  156. }
  157. /**
  158. * @notes 支付宝退款
  159. * @param $order
  160. * @param $refundAmount
  161. * @return bool
  162. * @throws \Exception
  163. * @author 段誉
  164. * @date 2021/8/5 10:25
  165. */
  166. public static function aliPayRefund($order, $refundAmount)
  167. {
  168. //原路退回到支付宝的情况
  169. $result = (new AliPayService())->refund($order['sn'], $refundAmount, self::$refund['sn']);
  170. $result = (array)$result;
  171. //更新退款日志记录
  172. Refund::update([
  173. 'refund_status' => (isset($result['result_code']) && $result['result_code'] == 'SUCCESS') ? 1 : 2,
  174. 'refund_msg' => json_encode($result['httpBody'], JSON_UNESCAPED_UNICODE),
  175. ], ['id' => self::$refund['id']]);
  176. if ($result['code'] != '10000' || $result['msg'] != 'Success' || $result['fundChange'] != 'Y') {
  177. throw new \Exception('支付宝退款失败');
  178. }
  179. return true;
  180. }
  181. /**
  182. * @notes 字节退款
  183. * @param $order
  184. * @param $refundAmount
  185. * @author Tab
  186. * @date 2021/11/18 14:09
  187. */
  188. public static function bytePayRefund($order, $refundAmount)
  189. {
  190. (new ToutiaoPayService())->refund($order, $refundAmount, self::$refund);
  191. }
  192. /**
  193. * @notes 混合支付退款
  194. * @param $order 订单信息
  195. * @param $refundAmount 退款金额
  196. * @param $afterSaleId 售后ID
  197. * @return bool
  198. * @author 系统
  199. * @date 2024/12/19
  200. */
  201. public static function mixedPayRefund($order, $refundAmount, $afterSaleId)
  202. {
  203. try {
  204. $mixedPayServiceModel = new MixedPayService($order['order_terminal'], $order['user_id']);
  205. $result = $mixedPayServiceModel->refund($order, $refundAmount, $afterSaleId);
  206. if ($result === false) {
  207. throw new \Exception('混合支付退款失败:' . $mixedPayServiceModel->getError());
  208. }
  209. return true;
  210. } catch (\Exception $e) {
  211. throw new \Exception('混合支付退款处理失败:' . $e->getMessage());
  212. }
  213. }
  214. /**
  215. * @notes 退款日志
  216. * @param $order
  217. * @param $afterSaleId
  218. * @param $refundAmount
  219. * @author 段誉
  220. * @date 2021/8/9 17:32
  221. */
  222. public static function log($order, $afterSaleId, $refundAmount)
  223. {
  224. $result = Refund::create([
  225. 'order_id' => $order['id'],
  226. 'after_sale_id' => $afterSaleId,
  227. 'user_id' => $order['user_id'],
  228. 'sn' => generate_sn(new Refund(), 'sn'),
  229. 'order_amount' => $order['order_amount'],
  230. 'refund_amount' => $refundAmount,
  231. ]);
  232. self::$refund = $result;
  233. }
  234. /**
  235. * @notes 售后退款更新订单或订单商品状态
  236. * @param $order_id
  237. * @return void
  238. * @throws DbException
  239. * @throws ModelNotFoundException
  240. * @throws DataNotFoundException
  241. * @author lbzy
  242. * @datetime 2023-07-07 13:53:19
  243. */
  244. static function afterSaleRefundUpdate($order_id): void
  245. {
  246. $order_goods_count = OrderGoods::where('order_id', $order_id)->count();
  247. $after_sale_count = AfterSale::where('order_id', $order_id)
  248. ->group('order_goods_id')
  249. ->where('status', AfterSaleEnum::STATUS_SUCCESS)
  250. ->count();
  251. //如果订单商品已全部退款
  252. if ($order_goods_count == $after_sale_count) {
  253. Order::update([ 'order_status' => OrderEnum::STATUS_CLOSE ], [
  254. [ 'id', '=', $order_id ],
  255. ]);
  256. } else {
  257. // 更新订单发货状态
  258. $orderGoodsIds = OrderGoods::where(['order_id'=>$order_id,'express_status'=>[DeliveryEnum::NOT_SHIPPED,DeliveryEnum::PART_SHIPPED]])->column('id');
  259. if (!empty($orderGoodsIds)) {
  260. foreach ($orderGoodsIds as $key=>$orderGoodsId) {
  261. $afterSale = AfterSale::where(['order_goods_id'=>$orderGoodsId,'status'=>AfterSaleEnum::STATUS_SUCCESS])->findOrEmpty();
  262. if (!$afterSale->isEmpty()) {
  263. unset($orderGoodsIds[$key]);
  264. }
  265. }
  266. }
  267. if (empty($orderGoodsIds)) {
  268. $order = Order::find($order_id);
  269. $order->order_status = max($order->order_status, OrderEnum::STATUS_WAIT_RECEIVE);
  270. $order->express_status = DeliveryEnum::SHIPPED;
  271. $order->save();
  272. }
  273. }
  274. }
  275. }