Refund.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\service\order;
  13. use app\common\model\User as UserModel;
  14. use app\common\model\Wxapp as WxappModel;
  15. use app\common\model\user\BalanceLog as BalanceLogModel;
  16. use app\common\enum\order\PayType as OrderPayTypeEnum;
  17. use app\common\enum\user\balanceLog\Scene as SceneEnum;
  18. use app\common\library\wechat\WxPay;
  19. use app\common\service\BaseService;
  20. /**
  21. * 订单退款服务类
  22. * Class Refund
  23. * @package app\common\service\order
  24. */
  25. class Refund extends BaseService
  26. {
  27. /**
  28. * 执行订单退款
  29. * @param object $order 订单信息
  30. * @param null $money 指定退款金额
  31. * @return bool
  32. * @throws \app\common\exception\BaseException
  33. */
  34. public function execute($order, $money = null)
  35. {
  36. // 退款金额,如不指定则默认为订单实付款金额
  37. is_null($money) && $money = $order['pay_price'];
  38. // 1.微信支付退款
  39. if ($order['pay_type'] == OrderPayTypeEnum::WECHAT) {
  40. return $this->wxpay($order, $money);
  41. }
  42. // 2.余额支付退款
  43. if ($order['pay_type'] == OrderPayTypeEnum::BALANCE) {
  44. return $this->balance($order, $money);
  45. }
  46. return false;
  47. }
  48. /**
  49. * 余额支付退款
  50. * @param $order
  51. * @param $money
  52. * @return bool
  53. */
  54. private function balance($order, $money)
  55. {
  56. // 回退用户余额
  57. UserModel::setIncBalance((int)$order['user_id'], (float)$money);
  58. // 记录余额明细
  59. BalanceLogModel::add(SceneEnum::REFUND, [
  60. 'user_id' => $order['user_id'],
  61. 'money' => $money,
  62. ], ['order_no' => $order['order_no']]);
  63. return true;
  64. }
  65. /**
  66. * 微信支付退款
  67. * @param $order
  68. * @param $money
  69. * @return bool
  70. * @throws \app\common\exception\BaseException
  71. */
  72. private function wxpay($order, $money)
  73. {
  74. $wxConfig = WxappModel::getWxappCache($order['store_id']);
  75. $WxPay = new WxPay($wxConfig);
  76. return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
  77. }
  78. }