RechargeLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\recharge;
  15. use app\common\enum\RefundEnum;
  16. use app\common\enum\user\AccountLogEnum;
  17. use app\common\enum\YesNoEnum;
  18. use app\common\logic\AccountLogLogic;
  19. use app\common\logic\BaseLogic;
  20. use app\common\logic\RefundLogic;
  21. use app\common\model\recharge\RechargeOrder;
  22. use app\common\model\refund\RefundRecord;
  23. use app\common\model\user\User;
  24. use app\common\service\ConfigService;
  25. use think\facade\Db;
  26. /**
  27. * 充值逻辑层
  28. * Class RechargeLogic
  29. * @package app\adminapi\logic\recharge
  30. */
  31. class RechargeLogic extends BaseLogic
  32. {
  33. /**
  34. * @notes 获取充值设置
  35. * @return array
  36. * @author 段誉
  37. * @date 2023/2/22 16:54
  38. */
  39. public static function getConfig()
  40. {
  41. $config = [
  42. 'status' => ConfigService::get('recharge', 'status', 0),
  43. 'min_amount' => ConfigService::get('recharge', 'min_amount', 0)
  44. ];
  45. return $config;
  46. }
  47. /**
  48. * @notes 充值设置
  49. * @param $params
  50. * @return bool
  51. * @author 段誉
  52. * @date 2023/2/22 16:54
  53. */
  54. public static function setConfig($params)
  55. {
  56. try {
  57. if (isset($params['status'])) {
  58. ConfigService::set('recharge', 'status', $params['status']);
  59. }
  60. if (isset($params['min_amount'])) {
  61. ConfigService::set('recharge', 'min_amount', $params['min_amount']);
  62. }
  63. return true;
  64. } catch (\Exception $e) {
  65. self::setError($e->getMessage());
  66. return false;
  67. }
  68. }
  69. /**
  70. * @notes 退款
  71. * @param $params
  72. * @param $adminId
  73. * @return array|false
  74. * @author 段誉
  75. * @date 2023/3/3 11:42
  76. */
  77. public static function refund($params, $adminId)
  78. {
  79. Db::startTrans();
  80. try {
  81. $order = RechargeOrder::findOrEmpty($params['recharge_id']);
  82. // 更新订单信息, 标记已发起退款状态,具体退款成功看退款日志
  83. RechargeOrder::update([
  84. 'id' => $order['id'],
  85. 'refund_status' => YesNoEnum::YES,
  86. ]);
  87. // 更新用户余额及累计充值金额
  88. // User::where(['id' => $order['user_id']])
  89. // ->dec('total_recharge_amount', $order['order_amount'])
  90. // ->dec('user_money', $order['order_amount'])
  91. // ->update();
  92. // 记录日志
  93. // AccountLogLogic::add(
  94. // $order['user_id'],
  95. // AccountLogEnum::UM_INC_ADMIN,
  96. // AccountLogEnum::DEC,
  97. // $order['order_amount'],
  98. // $order['sn'],
  99. // '充值订单退款'
  100. // );
  101. // 生成退款记录
  102. $recordSn = generate_sn(RefundRecord::class, 'sn');
  103. $record = RefundRecord::create([
  104. 'sn' => $recordSn,
  105. 'user_id' => $order['user_id'],
  106. 'order_id' => $order['id'],
  107. 'order_sn' => $order['sn'],
  108. 'order_type' => RefundEnum::ORDER_TYPE_RECHARGE,
  109. 'order_amount' => $order['order_amount'],
  110. 'refund_amount' => $order['order_amount'],
  111. 'refund_type' => RefundEnum::TYPE_ADMIN,
  112. 'transaction_id' => $order['transaction_id'] ?? '',
  113. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  114. ]);
  115. // 退款
  116. $result = RefundLogic::refund($order, $record['id'], $order['order_amount'], $adminId);
  117. $flag = true;
  118. $resultMsg = '操作成功';
  119. if ($result !== true) {
  120. $flag = false;
  121. $resultMsg = RefundLogic::getError();
  122. }
  123. Db::commit();
  124. return [$flag, $resultMsg];
  125. } catch (\Exception $e) {
  126. Db::rollback();
  127. self::$error = $e->getMessage();
  128. return [false, $e->getMessage()];
  129. }
  130. }
  131. /**
  132. * @notes 重新退款
  133. * @param $params
  134. * @param $adminId
  135. * @return array
  136. * @author 段誉
  137. * @date 2023/3/3 11:44
  138. */
  139. public static function refundAgain($params, $adminId)
  140. {
  141. Db::startTrans();
  142. try {
  143. $record = RefundRecord::findOrEmpty($params['record_id']);
  144. $order = RechargeOrder::findOrEmpty($record['order_id']);
  145. // 退款
  146. $result = RefundLogic::refund($order, $record['id'], $order['order_amount'], $adminId);
  147. $flag = true;
  148. $resultMsg = '操作成功';
  149. if ($result !== true) {
  150. $flag = false;
  151. $resultMsg = RefundLogic::getError();
  152. }
  153. Db::commit();
  154. return [$flag, $resultMsg];
  155. } catch (\Exception $e) {
  156. Db::rollback();
  157. self::$error = $e->getMessage();
  158. return [false, $e->getMessage()];
  159. }
  160. }
  161. }