RefundOperationService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\backend\modules\refund\services;
  3. use app\backend\modules\refund\services\operation\OrderCloseAndRefund;
  4. use app\backend\modules\refund\services\operation\RefundBatchResend;
  5. use app\backend\modules\refund\services\operation\RefundChangePrice;
  6. use app\backend\modules\refund\services\operation\RefundClose;
  7. use app\backend\modules\refund\services\operation\RefundComplete;
  8. use app\backend\modules\refund\services\operation\RefundConsensus;
  9. use app\backend\modules\refund\services\operation\RefundExchangeComplete;
  10. use app\backend\modules\refund\services\operation\RefundPass;
  11. use app\backend\modules\refund\services\operation\RefundReject;
  12. use app\backend\modules\refund\services\operation\RefundResend;
  13. use app\backend\modules\refund\services\operation\RefundSendBack;
  14. use app\backend\modules\refund\services\operation\RefundCancel;
  15. use app\common\models\Order;
  16. use Illuminate\Support\Facades\DB;
  17. use app\backend\modules\refund\models\RefundApply;
  18. use app\common\events\order\AfterOrderRefundedEvent;
  19. use app\common\exceptions\AdminException;
  20. use app\common\exceptions\AppException;
  21. use app\common\events\order\AfterOrderRefundRejectEvent;
  22. use app\common\events\order\AfterOrderRefundSuccessEvent;
  23. /**
  24. * Created by PhpStorm.
  25. * Author: 芸众商城 www.yunzshop.com
  26. * Date: 2017/4/13
  27. * Time: 下午2:21
  28. */
  29. class RefundOperationService
  30. {
  31. //驳回
  32. public static function refundReject($params)
  33. {
  34. $refundApply = RefundReject::find($params['refund_id']);
  35. $result = DB::transaction(function () use ($refundApply) {
  36. return $refundApply->execute();
  37. });
  38. return $result;
  39. }
  40. /**
  41. * 申请通过
  42. * @param $params
  43. * @return bool
  44. * @throws AppException
  45. */
  46. public static function refundPass($params)
  47. {
  48. $refundApply = RefundPass::find($params['refund_id']);
  49. $result = DB::transaction(function () use ($refundApply) {
  50. return $refundApply->execute();
  51. });
  52. return $result;
  53. }
  54. /**
  55. * 同意退款
  56. * @param $params
  57. * @return array|bool|mixed|string|void
  58. * @throws AppException
  59. */
  60. public static function refundPay($params)
  61. {
  62. try {
  63. $payStatus = (new \app\common\modules\refund\services\RefundService())->pay($params['refund_id']);
  64. } catch (\Exception $e) {
  65. \Log::debug("<----{$params['refund_id']}--售后退款支付失败------:".$e->getMessage());
  66. throw new AppException($e->getMessage());
  67. }
  68. return $payStatus;
  69. }
  70. /**
  71. * 订单关闭并退款
  72. * @param Order $order
  73. * @return array|bool|mixed|string|void
  74. * @throws AppException
  75. */
  76. public static function orderCloseAndRefund(Order $order)
  77. {
  78. try {
  79. $refundApply = new OrderCloseAndRefund();
  80. $refundApply->setRelation('order',$order);
  81. DB::transaction(function () use ($refundApply) {
  82. return $refundApply->execute();
  83. });
  84. if (bccomp($refundApply->price, 0, 2) !== 1) {
  85. RefundOperationService::refundConsensus(['refund_id'=> $refundApply->id]);
  86. } else {
  87. $payStatus = (new \app\common\modules\refund\services\RefundService())->pay($refundApply->id);
  88. }
  89. return $payStatus;
  90. } catch (\Exception $e) {
  91. \Log::debug("<----{$order->order_sn}--订单关闭并退款支付失败------:".$e->getMessage(),[$e,$refundApply]);
  92. throw new AppException($e->getMessage());
  93. }
  94. }
  95. /**
  96. * @param $params
  97. * @return bool
  98. * @throws AppException
  99. */
  100. public static function refundBatchResend($params)
  101. {
  102. $refundApply = RefundBatchResend::find($params['refund_id']);
  103. $result = DB::transaction(function () use ($refundApply) {
  104. return $refundApply->execute();
  105. });
  106. return $result;
  107. }
  108. /**
  109. * @param $params
  110. * @return bool
  111. * @throws AppException
  112. */
  113. public static function refundResend($params)
  114. {
  115. $refundApply = RefundResend::find($params['refund_id']);
  116. $result = DB::transaction(function () use ($refundApply) {
  117. return $refundApply->execute();
  118. });
  119. return $result;
  120. }
  121. /**
  122. * @param $params
  123. * @return mixed
  124. */
  125. public static function refundClose($params)
  126. {
  127. $refundApply = RefundClose::find($params['refund_id']);
  128. $result = DB::transaction(function () use ($refundApply) {
  129. return $refundApply->execute();
  130. });
  131. return $result;
  132. }
  133. /**
  134. * todo 前端接口 迁移到一起管理
  135. * @return mixed
  136. * @throws AppException
  137. */
  138. public static function refundSendBack($params)
  139. {
  140. $refundSend = RefundSendBack::find($params['refund_id']);
  141. return $refundSend->execute();
  142. }
  143. /**
  144. * todo 前端接口 迁移到一起管理
  145. * @return mixed
  146. * @throws AppException
  147. */
  148. public static function refundCancel($params)
  149. {
  150. $refundCancel = RefundCancel::find($params['refund_id']);
  151. return $refundCancel->execute();
  152. }
  153. /**
  154. * todo 前端接口 迁移到一起管理
  155. * @return mixed
  156. * @throws AppException
  157. */
  158. public static function refundExchangeComplete($params)
  159. {
  160. $refundComplete = RefundExchangeComplete::find($params['refund_id']);
  161. $result = DB::transaction(function () use ($refundComplete) {
  162. return $refundComplete->execute();
  163. });
  164. return $result;
  165. }
  166. public static function refundConsensus($params)
  167. {
  168. $refundApply = RefundConsensus::find($params['refund_id']);
  169. $result = DB::transaction(function () use ($refundApply) {
  170. return $refundApply->execute();
  171. });
  172. return $result;
  173. }
  174. public static function refundComplete($params)
  175. {
  176. $id = $params['refund_id']?:$params['id'];
  177. $refundApply = RefundComplete::find($id);
  178. if (!isset($refundApply)) {
  179. throw new AdminException('(ID:'.$id.')退款申请不存在');
  180. }
  181. $result = DB::transaction(function () use ($refundApply) {
  182. return $refundApply->execute();
  183. });
  184. return $result;
  185. }
  186. public static function refundChangePrice($params)
  187. {
  188. $refundApply = RefundChangePrice::find($params['refund_id']);
  189. $result = DB::transaction(function () use ($refundApply) {
  190. return $refundApply->execute();
  191. });
  192. return $result;
  193. }
  194. }