PaymentChannelLinkService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Services;
  3. use App\Models\RechargeChannel;
  4. use App\Models\RechargeChannelGroup;
  5. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Collection;
  8. use RuntimeException;
  9. use App\Services\Payment\PaymentProviderCatalog;
  10. class PaymentChannelLinkService
  11. {
  12. public function validate(int $channelId, array $groupIds, int $expectedDataType): array
  13. {
  14. $channel = RechargeChannel::query()->find($channelId);
  15. if (!$channel) throw new RuntimeException('充值通道不存在');
  16. if ((int)$channel->data_type !== $expectedDataType) {
  17. throw new RuntimeException($expectedDataType === 2 ? '请选择提现通道' : '请选择充值通道');
  18. }
  19. $groupIds = array_values(array_unique(array_filter(array_map('intval', $groupIds))));
  20. if (!$groupIds) throw new RuntimeException('至少选择一个通道组合');
  21. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get();
  22. if ($groups->count() !== count($groupIds)) throw new RuntimeException('部分通道组合不存在');
  23. $field = $this->groupField($expectedDataType);
  24. foreach ($groups as $group) {
  25. if (!in_array((string)$channel->type, (array)$group->{$field}, true)) {
  26. throw new RuntimeException("通道组合“{$group->name}”没有包含通道“{$channel->name}”");
  27. }
  28. }
  29. return [$channel, $groupIds];
  30. }
  31. public function formatMany($models): Collection
  32. {
  33. $models = $models instanceof Collection ? $models : collect($models);
  34. if ($models instanceof EloquentCollection) $models->loadMissing('rechargeChannel');
  35. $groupIds = $models->flatMap(fn ($model) => (array)($model->recharge_channel_group_ids ?? []))
  36. ->map(fn ($id) => (int)$id)->filter()->unique()->values();
  37. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get()->keyBy('id');
  38. return $models->map(fn (Model $model) => $this->formatOne($model, $groups));
  39. }
  40. public function formatOne(Model $model, ?Collection $groups = null): array
  41. {
  42. $model->loadMissing('rechargeChannel');
  43. $groups ??= RechargeChannelGroup::query()
  44. ->whereIn('id', (array)($model->recharge_channel_group_ids ?? []))->get()->keyBy('id');
  45. $channel = $model->rechargeChannel;
  46. $assignedIds = array_values(array_unique(array_filter(array_map(
  47. 'intval',
  48. (array)($model->recharge_channel_group_ids ?? [])
  49. ))));
  50. $effectiveIds = [];
  51. $unavailableIds = [];
  52. $groupRows = [];
  53. foreach ($assignedIds as $groupId) {
  54. $group = $groups->get($groupId);
  55. $effective = false;
  56. if ($group && $channel) {
  57. $effective = in_array(
  58. (string)$channel->type,
  59. (array)$group->{$this->groupField((int)$channel->data_type)},
  60. true
  61. );
  62. }
  63. if ($effective) {
  64. $effectiveIds[] = $groupId;
  65. } else {
  66. $unavailableIds[] = $groupId;
  67. }
  68. $groupRows[] = [
  69. 'id' => $groupId,
  70. 'name' => (string)($group->name ?? ''),
  71. 'effective' => $effective ? 1 : 0,
  72. ];
  73. }
  74. $data = $model->toArray();
  75. $identity = $channel ? PaymentProviderCatalog::channelIdentity($channel->toArray()) : null;
  76. if (array_key_exists('payment_company', $data)) {
  77. $data['payment_company_label'] = PaymentProviderCatalog::label((string)$data['payment_company']);
  78. $data['payment_method_label'] = $identity && $identity['method_code'] === $data['payment_method']
  79. ? $identity['method_label'] : (string)$data['payment_method'];
  80. }
  81. if (array_key_exists('provider_name', $data)) {
  82. $data['provider_label'] = PaymentProviderCatalog::label((string)$data['provider_name']);
  83. $data['collection_method_label'] = $identity && $identity['method_code'] === $data['collection_method']
  84. ? $identity['method_label'] : (string)$data['collection_method'];
  85. }
  86. if (array_key_exists('payment_company', $data) || array_key_exists('provider_name', $data)) {
  87. $data['credential_source'] = 'environment';
  88. $data['merchant_config_applied'] = false;
  89. }
  90. $data['recharge_channel_group_ids'] = $assignedIds;
  91. $data['effective_group_ids'] = $effectiveIds;
  92. $data['unavailable_group_ids'] = $unavailableIds;
  93. $data['groups'] = $groupRows;
  94. $data['recharge_channel'] = $channel ? [
  95. 'id' => (int)$channel->id,
  96. 'data_type' => (int)$channel->data_type,
  97. 'name' => (string)$channel->name,
  98. 'key' => (string)$channel->key,
  99. 'type' => (string)$channel->type,
  100. 'rate' => number_format((float)$channel->rate, 4, '.', ''),
  101. 'fee_rate' => bcmul((string)$channel->rate, '100', 4),
  102. 'min_amount' => $channel->min === null ? null : number_format((float)$channel->min, 2, '.', ''),
  103. 'max_amount' => $channel->max === null ? null : number_format((float)$channel->max, 2, '.', ''),
  104. 'fixed_amounts' => $channel->fixed ?: [],
  105. 'sort' => (int)$channel->sort,
  106. 'status' => (int)$channel->status,
  107. ] : null;
  108. $data['fee_rate'] = $channel ? bcmul((string)$channel->rate, '100', 4) : null;
  109. $data['min_amount'] = $channel?->min;
  110. $data['max_amount'] = $channel?->max;
  111. $data['sort'] = $channel ? (int)$channel->sort : null;
  112. $data['config_status'] = (int)$model->status;
  113. $data['channel_status'] = $channel ? (int)$channel->status : 0;
  114. $data['runtime_channel_available'] = $channel
  115. && (int)$channel->status === 1 && count($effectiveIds) > 0 ? 1 : 0;
  116. $data['effective_status'] = (int)$model->status === 1
  117. && $data['runtime_channel_available'] === 1 ? 1 : 0;
  118. if (array_key_exists('amount', $data)) {
  119. $data['actual_amount'] = $channel
  120. ? bcsub((string)$model->amount, bcmul((string)$model->amount, (string)$channel->rate, 6), 2)
  121. : null;
  122. }
  123. $data['channel_fields_readonly'] = [
  124. 'recharge_channel.name', 'recharge_channel.key', 'recharge_channel.type',
  125. 'fee_rate', 'min_amount', 'max_amount', 'recharge_channel.fixed_amounts',
  126. 'sort', 'channel_status',
  127. ];
  128. unset($data['level_scope']);
  129. return $data;
  130. }
  131. private function groupField(int $dataType): string
  132. {
  133. return match ($dataType) {
  134. 2 => 'withdraw_type',
  135. 3 => 'activity_type',
  136. default => 'recharge_type',
  137. };
  138. }
  139. }