Refund.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\exception\BaseException;
  14. use app\common\model\User as UserModel;
  15. use app\common\model\wxapp\Setting as WxappSettingModel;
  16. use app\common\model\user\BalanceLog as BalanceLogModel;
  17. use app\common\enum\order\PayType as OrderPayTypeEnum;
  18. use app\common\enum\user\balanceLog\Scene as SceneEnum;
  19. use app\common\library\wechat\WxPay;
  20. use app\common\service\BaseService;
  21. /**
  22. * 订单退款服务类
  23. * Class Refund
  24. * @package app\common\service\order
  25. */
  26. class Refund extends BaseService
  27. {
  28. /**
  29. * 执行订单退款
  30. * @param $order
  31. * @param null $money
  32. * @return bool
  33. * @throws \cores\exception\BaseException
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function execute($order, $money = null): bool
  39. {
  40. // 退款金额,如不指定则默认为订单实付款金额
  41. is_null($money) && $money = $order['pay_price'];
  42. // 1.微信支付退款
  43. if ($order['pay_type'] == OrderPayTypeEnum::WECHAT) {
  44. return $this->wxpay($order, (string)$money);
  45. }
  46. // 2.余额支付退款
  47. if ($order['pay_type'] == OrderPayTypeEnum::BALANCE) {
  48. return $this->balance($order, (string)$money);
  49. }
  50. return false;
  51. }
  52. /**
  53. * 余额支付退款
  54. * @param $order
  55. * @param $money
  56. * @return bool
  57. */
  58. private function balance($order, $money): bool
  59. {
  60. // 回退用户余额
  61. UserModel::setIncBalance((int)$order['user_id'], (float)$money);
  62. // 记录余额明细
  63. BalanceLogModel::add(SceneEnum::REFUND, [
  64. 'user_id' => $order['user_id'],
  65. 'money' => $money,
  66. ], ['order_no' => $order['order_no']]);
  67. return true;
  68. }
  69. /**
  70. * 微信支付退款
  71. * @param $order
  72. * @param string $money
  73. * @return bool
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \cores\exception\BaseException
  78. */
  79. private function wxpay($order, string $money): bool
  80. {
  81. $wxConfig = WxappSettingModel::getWxappConfig($order['store_id']);
  82. $WxPay = new WxPay($wxConfig, $order['store_id']);
  83. return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
  84. }
  85. }