PaymentChannelLinkService.php 7.3 KB

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