BalancePayService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop100%开源免费商用电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
  10. // | 访问官网:https://www.likemarket.net
  11. // | 访问社区:https://home.likemarket.net
  12. // | 访问手册:http://doc.likemarket.net
  13. // | 微信公众号:好象科技
  14. // | 好象科技开发团队 版权所有 拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | Author: LikeShopTeam
  17. // +----------------------------------------------------------------------
  18. namespace app\common\service\pay;
  19. use app\common\enum\AccountLogEnum;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\AfterSaleLogEnum;
  22. use app\common\enum\BargainEnum;
  23. use app\common\enum\NoticeEnum;
  24. use app\common\enum\OrderEnum;
  25. use app\common\enum\PayEnum;
  26. use app\common\logic\AccountLogLogic;
  27. use app\common\model\AfterSale;
  28. use app\common\model\BargainInitiate;
  29. use app\common\model\User;
  30. use app\common\service\after_sale\AfterSaleService;
  31. use app\common\service\pay\base\StatusTrait;
  32. /**
  33. * 余额支付
  34. * Class BalancePayService
  35. * @package app\common\server
  36. */
  37. class BalancePayService extends BasePayService
  38. {
  39. use StatusTrait;
  40. function realPay()
  41. {
  42. return $this;
  43. }
  44. /**
  45. * @notes 余额支付
  46. * @param $from //订单类型 (order-普通商品订单, recharge-充值订单, ....)
  47. * @param $order //订单信息
  48. * @return array|false
  49. * @author 段誉
  50. * @date 2021/8/13 16:49
  51. */
  52. public function pay($from, $order)
  53. {
  54. try {
  55. $user = User::findOrEmpty($order['user_id']);
  56. if ($user->isEmpty() || $user['user_money'] < $order['order_amount']) {
  57. throw new \Exception('余额不足');
  58. }
  59. //扣除余额
  60. User::update([
  61. 'user_money' => ['dec', $order['order_amount']]
  62. ], ['id' => $order['user_id']]);
  63. //余额流水
  64. AccountLogLogic::add(
  65. $order['user_id'],
  66. AccountLogEnum::BNW_DEC_ORDER,
  67. AccountLogEnum::DEC,
  68. $order['order_amount'],
  69. $order['sn']
  70. );
  71. return [
  72. 'pay_way' => PayEnum::BALANCE_PAY
  73. ];
  74. } catch (\Exception $e) {
  75. $this->setStatus(false, $e->getMessage());
  76. return false;
  77. }
  78. }
  79. /**
  80. * @notes 余额退款
  81. * @param $order
  82. * @param $refundAmount
  83. * @author 段誉
  84. * @date 2021/8/12 18:01
  85. */
  86. public function refund($order, $refundAmount,$afterSaleId)
  87. {
  88. //返回余额
  89. User::update([
  90. 'user_money' => ['inc', $refundAmount]
  91. ], ['id' => $order['user_id']]);
  92. //余额流水
  93. $afterSale = AfterSale::findOrEmpty($afterSaleId);
  94. AccountLogLogic::add(
  95. $order['user_id'],
  96. AccountLogEnum::BNW_INC_AFTER_SALE,
  97. AccountLogEnum::INC,
  98. $refundAmount,
  99. $afterSale->sn
  100. );
  101. // 更新售后状态
  102. $afterSale->status = AfterSaleEnum::STATUS_SUCCESS;
  103. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_SUCCESS;
  104. $afterSale->refund_status = AfterSaleEnum::FULL_REFUND;
  105. $afterSale->save();
  106. AfterSaleService::createAfterLog($afterSale->id, '系统已完成退款', 0, AfterSaleLogEnum::ROLE_SYS);
  107. // 消息通知
  108. event('Notice', [
  109. 'scene_id' => NoticeEnum::REFUND_SUCCESS_NOTICE,
  110. 'params' => [
  111. 'user_id' => $afterSale->user_id,
  112. 'order_sn' => $order['sn'],
  113. 'after_sale_sn' => $afterSale->sn,
  114. 'refund_type' => AfterSaleEnum::getRefundTypeDesc($afterSale->refund_type),
  115. 'refund_total_amount' => $afterSale->refund_total_amount,
  116. 'refund_time' => date('Y-m-d H:i:s'),
  117. ]
  118. ]);
  119. }
  120. /**
  121. * @notes 积分订单退款
  122. * @param $order
  123. * @param $refundAmount
  124. * @author 段誉
  125. * @date 2022/4/1 10:40
  126. */
  127. public static function integralOrderRefund($order, $refundAmount)
  128. {
  129. User::where(['id' => $order['user_id']])->update([
  130. 'user_money' => ['inc', $refundAmount]
  131. ]);
  132. AccountLogLogic::add(
  133. $order['user_id'],
  134. AccountLogEnum::BNW_INC_AFTER_SALE,
  135. AccountLogEnum::INC,
  136. $refundAmount,
  137. $order['user_sn']
  138. );
  139. }
  140. }