OrderRefundService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\common\service\order;
  3. use app\common\library\alipay\AliPay;
  4. use app\common\model\user\User as UserModel;
  5. use app\common\model\user\BalanceLog as BalanceLogModel;
  6. use app\common\enum\order\OrderPayTypeEnum;
  7. use app\common\enum\user\balanceLog\BalanceLogSceneEnum;
  8. use app\common\library\easywechat\WxPay;
  9. use app\common\library\easywechat\AppWx;
  10. use app\common\library\easywechat\AppMp;
  11. /**
  12. * 订单退款服务类
  13. */
  14. class OrderRefundService
  15. {
  16. /**
  17. * 执行订单退款
  18. */
  19. public function execute(&$order, $money = null)
  20. {
  21. // 退款金额,如不指定则默认为订单实付款金额
  22. is_null($money) && $money = $order['pay_price'];
  23. // 1.微信支付退款
  24. if ($order['pay_type']['value'] == OrderPayTypeEnum::WECHAT) {
  25. return $this->wxpay($order, $money);
  26. }
  27. // 2.余额支付退款
  28. if ($order['pay_type']['value'] == OrderPayTypeEnum::BALANCE) {
  29. return $this->balance($order, $money);
  30. }
  31. // 3.支付宝退款
  32. if ($order['pay_type']['value'] == OrderPayTypeEnum::ALIPAY) {
  33. return $this->alipay($order, $money);
  34. }
  35. return false;
  36. }
  37. /**
  38. * 余额支付退款
  39. */
  40. private function balance(&$order, $money)
  41. {
  42. // 回退用户余额
  43. $user = UserModel::detail($order['user_id']);
  44. $user->where('user_id', '=', $order['user_id'])->inc('balance', $money)->update();
  45. // 记录余额明细
  46. BalanceLogModel::add(BalanceLogSceneEnum::REFUND, [
  47. 'user_id' => $user['user_id'],
  48. 'money' => $money,
  49. ], ['order_no' => $order['order_no']]);
  50. return true;
  51. }
  52. /**
  53. * 微信支付退款
  54. */
  55. private function wxpay(&$order, $money)
  56. {
  57. $app = null;
  58. if($order['pay_source'] == 'mp' || $order['pay_source'] == 'payH5'){
  59. $app = AppMp::getWxPayApp($order['app_id']);
  60. }else if($order['pay_source'] == 'wx'){
  61. $app = AppWx::getWxPayApp($order['app_id']);
  62. }
  63. $WxPay = new WxPay($app);
  64. return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
  65. }
  66. /**
  67. * 支付宝退款
  68. */
  69. private function alipay(&$order, $money)
  70. {
  71. $AliPay = new AliPay($order['pay_source']);
  72. return $AliPay->refund($order['transaction_id'], $order['pay_price'], $money);
  73. }
  74. }