SimulationFundService.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Services\Simulation;
  3. use App\Constants\HttpStatus;
  4. use App\Models\PaymentOrder;
  5. use App\Models\Wallet;
  6. use App\Services\BalanceLogService;
  7. use App\Services\PaymentOrderService;
  8. use App\Services\WageringService;
  9. use App\Services\WalletFreezeService;
  10. use App\Services\WithdrawFundsService;
  11. use Exception;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * 模拟会员的资金:订单、流水、冻结都写真实业务表并走真实入账代码,只是不向任何三方发请求。
  15. * 充值有两条路:公司入款(人工充值单审核通过)和三方充值(代收单按成功回调入账);提现走人工提现。
  16. */
  17. final class SimulationFundService
  18. {
  19. public const CHANNEL = 'simulation';
  20. /** @param string $channel company|third @param string $result success|pending|fail */
  21. public static function recharge($memberId, $amount, string $channel = 'third', string $result = 'success'): PaymentOrder
  22. {
  23. SimulationSettings::assertEnabled();
  24. $user = SimulationSettings::member($memberId);
  25. $amount = SimulationSettings::assertAmount($amount);
  26. $type = $channel === 'company' ? PaymentOrderService::TYPE_SELF_PAY : PaymentOrderService::TYPE_PAY;
  27. $order = PaymentOrder::create(['type' => $type, 'member_id' => $user->member_id, 'amount' => $amount, 'channel' => self::CHANNEL,
  28. 'order_no' => PaymentOrderService::createOrderNo('sim' . $type . '_', $user->member_id), 'remark' => '模拟充值',
  29. 'status' => $type === PaymentOrderService::TYPE_SELF_PAY ? PaymentOrderService::STATUS_AUDIT : PaymentOrderService::STATUS_PROCESS]
  30. + PaymentOrderService::feeFields($amount, 0));
  31. if ($result === 'fail') {
  32. $order->status = PaymentOrderService::STATUS_FAIL;
  33. $order->remark = '模拟充值失败';
  34. $order->save();
  35. } elseif ($result === 'success') {
  36. if ($type === PaymentOrderService::TYPE_PAY) {
  37. if (!PaymentOrderService::simulatePaySuccess($order)) throw new Exception('模拟充值入账失败', HttpStatus::CUSTOM_ERROR);
  38. } else {
  39. self::approveCompanyDeposit((int) $order->id);
  40. }
  41. }
  42. return $order->refresh();
  43. }
  44. /** 与后台“人工充值审核通过”相同的入账:订单置成功、加可用余额、写“充值”流水。 */
  45. private static function approveCompanyDeposit(int $id): void
  46. {
  47. DB::transaction(function () use ($id) {
  48. $order = PaymentOrder::where('id', $id)->where('type', PaymentOrderService::TYPE_SELF_PAY)->lockForUpdate()->first();
  49. if (!$order || (int) $order->status !== PaymentOrderService::STATUS_AUDIT) throw new Exception('订单状态不可审核', HttpStatus::CUSTOM_ERROR);
  50. $order->status = PaymentOrderService::STATUS_SUCCESS;
  51. $order->state = 1;
  52. $order->save();
  53. $wallet = Wallet::where('member_id', $order->member_id)->lockForUpdate()->first();
  54. if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  55. $before = (string) $wallet->available_balance;
  56. $after = bcadd($before, (string) $order->amount, 10);
  57. $wallet->available_balance = $after;
  58. $wallet->save();
  59. $log = BalanceLogService::addLog($order->member_id, $order->amount, $before, $after, '充值', $order->id, '模拟充值');
  60. \App\Services\RechargeBonusService::grant('payment_order', $order, $log);
  61. });
  62. }
  63. public static function randomBank(): string
  64. {
  65. return ['工商银行', '建设银行', '农业银行', '中国银行', '招商银行', '交通银行', '邮储银行'][mt_rand(0, 6)];
  66. }
  67. public static function randomCard(): string
  68. {
  69. return '62' . str_pad((string) mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT) . str_pad((string) mt_rand(0, 999999999), 9, '0', STR_PAD_LEFT);
  70. }
  71. /** 人工提现申请:校验打码量、冻结资金、建待审核提现单。 */
  72. public static function withdrawApply($memberId, $amount): PaymentOrder
  73. {
  74. SimulationSettings::assertEnabled();
  75. $user = SimulationSettings::member($memberId);
  76. $amount = SimulationSettings::assertAmount($amount);
  77. if (SimulationSettings::get('simulation_wager_bypass') === '1' && WageringService::enabled()) {
  78. $wallet = Wallet::where('member_id', $user->member_id)->first();
  79. if ($wallet) WageringService::resetCycle($wallet, 'simulation', null, '模拟提现跳过打码量');
  80. }
  81. $data = ['type' => PaymentOrderService::TYPE_SELF_PAYOUT, 'member_id' => $user->member_id, 'amount' => $amount, 'channel' => self::CHANNEL,
  82. 'order_no' => PaymentOrderService::createOrderNo('sim4_', $user->member_id), 'bank_name' => self::randomBank(), 'account' => (string) $user->first_name,
  83. 'card_no' => self::randomCard(), 'status' => PaymentOrderService::STATUS_STAY] + PaymentOrderService::feeFields($amount, 0);
  84. [$order] = WithdrawFundsService::hold((string) $user->member_id, $amount, '人工提现', '模拟提现', WithdrawFundsService::PAYMENT_ORDERS,
  85. function () use ($data, $user) {
  86. $pending = PaymentOrder::where('type', PaymentOrderService::TYPE_SELF_PAYOUT)->where('member_id', $user->member_id)
  87. ->where('status', PaymentOrderService::STATUS_STAY)->lockForUpdate()->first(['id']);
  88. if ($pending) throw new Exception('该模拟会员已有一笔待审核的提现', HttpStatus::CUSTOM_ERROR);
  89. return PaymentOrder::create($data);
  90. });
  91. return $order->refresh();
  92. }
  93. /** 审核模拟提现:通过则扣除冻结余额,拒绝则退回可用余额。 */
  94. public static function withdrawAudit(int $id, bool $approve, string $remark = ''): PaymentOrder
  95. {
  96. SimulationSettings::assertEnabled();
  97. $order = PaymentOrder::where('id', $id)->where('type', PaymentOrderService::TYPE_SELF_PAYOUT)->first();
  98. if (!$order) throw new Exception('提现单不存在', HttpStatus::CUSTOM_ERROR);
  99. SimulationSettings::member($order->member_id);
  100. if ($approve) {
  101. DB::transaction(function () use ($id) {
  102. $locked = PaymentOrder::where('id', $id)->where('status', PaymentOrderService::STATUS_STAY)->lockForUpdate()->first();
  103. if (!$locked) throw new Exception('提现单已处理', HttpStatus::CUSTOM_ERROR);
  104. $locked->status = PaymentOrderService::STATUS_SUCCESS;
  105. $locked->save();
  106. WithdrawFundsService::settle(WithdrawFundsService::PAYMENT_ORDERS, $locked);
  107. });
  108. } else {
  109. $result = PaymentOrderService::withdrawalFailed($id, $remark !== '' ? $remark : '模拟提现拒绝');
  110. if ($result['code'] !== 0) throw new Exception($result['msg'], HttpStatus::CUSTOM_ERROR);
  111. }
  112. return $order->refresh();
  113. }
  114. /** 人工调账:加款记“人工充值”,扣款记“人工扣款”,与后台上下分一致。 */
  115. public static function adjust($memberId, $amount, string $direction, string $remark)
  116. {
  117. SimulationSettings::assertEnabled();
  118. $user = SimulationSettings::member($memberId);
  119. $amount = SimulationSettings::assertAmount($amount);
  120. return DB::transaction(function () use ($user, $amount, $direction, $remark) {
  121. $wallet = Wallet::where('member_id', $user->member_id)->lockForUpdate()->first();
  122. if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  123. $delta = $direction === 'debit' ? bcsub('0', $amount, 2) : $amount;
  124. $before = (string) $wallet->available_balance;
  125. $after = bcadd($before, $delta, 10);
  126. if (bccomp($after, '0', 10) < 0) throw new Exception('可用余额不足', HttpStatus::CUSTOM_ERROR);
  127. $wallet->available_balance = $after;
  128. $wallet->save();
  129. return BalanceLogService::addLog($user->member_id, $delta, $before, $after, $direction === 'debit' ? '人工扣款' : '人工充值', null, $remark);
  130. });
  131. }
  132. public static function freeze($memberId, $amount, string $reason, int $adminId, bool $unfreeze = false): array
  133. {
  134. SimulationSettings::assertEnabled();
  135. $user = SimulationSettings::member($memberId);
  136. $amount = SimulationSettings::assertAmount($amount);
  137. $requestId = 'sim-' . bin2hex(random_bytes(8));
  138. return $unfreeze
  139. ? WalletFreezeService::unfreeze($requestId, $adminId, (string) $user->member_id, $amount, '0', $reason)
  140. : WalletFreezeService::freeze($requestId, $adminId, (string) $user->member_id, $amount, '0', $reason);
  141. }
  142. }