RechargeChannel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Models;
  3. use App\Services\Payment\PaymentProviderCatalog;
  4. use App\Services\Payment\PaymentProviderService;
  5. use App\Constants\Util;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. class RechargeChannel extends BaseModel
  8. {
  9. use SoftDeletes;
  10. protected $table = 'recharge_channel';
  11. protected $fillable = ['id', 'data_type', 'from', 'key', 'name', 'type', 'rate', 'min', 'max', 'fixed', 'sort', 'status', 'logo'];
  12. protected $hidden = [];
  13. public function getLogoAttribute($value): string
  14. {
  15. return $value ? (string)Util::ensureUrl($value) : '';
  16. }
  17. public function scopeAvailable($query)
  18. {
  19. $query->where('status', 1);
  20. $statuses = PaymentProviderService::statuses();
  21. foreach (PaymentProviderCatalog::codes() as $code) {
  22. if (($statuses[$code] ?? 0) === 1) continue;
  23. foreach ([1, 2] as $direction) {
  24. $types = PaymentProviderCatalog::channelTypesForProvider($code, $direction);
  25. if ($types === []) continue;
  26. $query->whereNot(fn ($part) => $part->where('data_type', $direction)->whereIn('type', $types));
  27. }
  28. }
  29. return $query;
  30. }
  31. public function getFixedAttribute($value)
  32. {
  33. return $value ? explode(',', $value) : null;
  34. }
  35. public static function getChannel($data_type, $type = [])
  36. {
  37. $query = self::where('status', 1)->where('data_type', $data_type);
  38. if ($type) {
  39. $query = $query->whereIn('type', $type);
  40. }
  41. $statuses = PaymentProviderService::statuses();
  42. $channel = $query->orderBy('sort')->orderBy('id')->get(['type', 'name', 'from', 'data_type', 'status'])
  43. ->map(function ($row) use ($statuses) {
  44. $data = $row->toArray() + PaymentProviderCatalog::channelDisplay($row->toArray(), $statuses);
  45. return $data + ['disabled' => $data['company_status'] === 0,
  46. 'disabled_reason' => $data['company_status'] === 0 ? '支付公司已禁用' : ''];
  47. })->all();
  48. $channel = array_column($channel, null, 'type');
  49. return array_values($channel);
  50. }
  51. //获取充值通道
  52. public static function product($from = '')
  53. {
  54. $where['status'] = 1;
  55. $where['data_type'] = 1;
  56. if($from){
  57. $where['from'] = $from;
  58. }
  59. $list = self::where($where)->available()->orderBy('sort', 'asc')->get()->toArray();
  60. return array_column($list, null, 'key');
  61. }
  62. public static function getFormatChannel($data_type, $recharge_channel_group_id = 1)
  63. {
  64. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  65. $query = self::where('data_type', $data_type)->available();
  66. $field = 'recharge_type'; //充值类型
  67. if ($data_type == 2) {
  68. //提现类型
  69. $field = 'withdraw_type';
  70. } elseif ($data_type == 3) {
  71. //提现类型
  72. $field = 'activity_type';
  73. }
  74. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  75. $types = $group ? (array)$group->{$field} : [];
  76. if (!$types) {
  77. return [];
  78. }
  79. $query = $query->whereIn('type', $types);
  80. $direction = (int)$data_type;
  81. $fields = ['id', 'data_type','name','type', 'min','max','fixed','rate'];
  82. if (\App\Services\Payment\RechargeChannelLogoService::supportsDirection($direction)) {
  83. $fields[] = 'logo';
  84. }
  85. $product = $query->orderBy('sort', 'asc')->orderBy('id')->select($fields)->get()->toArray();
  86. $logos = $direction === 1 ? \App\Services\Payment\RechargeChannelLogoService::configured(array_column($product, 'type')) : [];
  87. $list = [];
  88. foreach($product as $key => $pv) {
  89. if (isset($list[$pv['type']]['config'])) {
  90. $config = $list[$pv['type']]['config'];
  91. if (empty($config['range'])) {
  92. $config['range'][] = $config;
  93. }
  94. if ($pv['min'] < $config['min']) {
  95. $config['min'] = $pv['min'];
  96. }
  97. if ($pv['max'] > $config['max']) {
  98. $config['max'] = $pv['max'];
  99. }
  100. if ($pv['rate'] < $config['rate']) {
  101. $config['max_rate'] = $config['rate'];
  102. $config['min_rate'] = $pv['rate'];
  103. }
  104. if ($pv['rate'] > $config['rate']) {
  105. $config['max_rate'] = $pv['rate'];
  106. }
  107. $config['range'][] = $pv;
  108. } else {
  109. $config = $pv;
  110. }
  111. $list[$pv['type']] = [
  112. 'key' => $key,
  113. 'label' => lang($pv['name']),
  114. 'value' => $pv['type'],
  115. 'config' => $config ?? [],
  116. ];
  117. if (\App\Services\Payment\RechargeChannelLogoService::supportsDirection($direction)) {
  118. $logo = $direction === 1 && array_key_exists($pv['type'], $logos)
  119. ? $logos[$pv['type']]
  120. : ($config['logo'] ?? '');
  121. $list[$pv['type']]['logo'] = $logo ? (string)Util::ensureUrl($logo) : '';
  122. }
  123. }
  124. $list = array_column($list, null, 'key');
  125. return array_values($list);
  126. }
  127. //校验是否支持此提现方式
  128. public static function checkWithdrawChannel($type, $recharge_channel_group_id = 1) {
  129. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  130. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  131. $withdrawTypes = $group ? (array)$group->withdraw_type : [];
  132. if (!in_array($type, $withdrawTypes, true)) {
  133. return false;
  134. }
  135. return self::query()->where('data_type', 2)->where('type', $type)
  136. ->available()->orderBy('sort')->first() ?: false;
  137. }
  138. //校验是否支持此充值方式
  139. public static function checkRechargeChannel($type, $recharge_channel_group_id = 1) {
  140. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  141. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  142. $rechargeTypes = $group ? (array)$group->recharge_type : [];
  143. if (!in_array($type, $rechargeTypes, true)) {
  144. return false;
  145. }
  146. return self::query()->where('data_type', 1)->where('type', $type)
  147. ->available()->orderBy('sort')->first() ?: false;
  148. }
  149. }