RechargeChannel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $fields = ['id', 'data_type','name','type', 'min','max','fixed','rate'];
  81. if ((int)$data_type === 1) $fields[] = 'logo';
  82. $product = $query->orderBy('sort', 'asc')->orderBy('id')->select($fields)->get()->toArray();
  83. $logos = (int)$data_type === 1 ? \App\Services\Payment\RechargeChannelLogoService::configured(array_column($product, 'type')) : [];
  84. $list = [];
  85. foreach($product as $key => $pv) {
  86. if (isset($list[$pv['type']]['config'])) {
  87. $config = $list[$pv['type']]['config'];
  88. if (empty($config['range'])) {
  89. $config['range'][] = $config;
  90. }
  91. if ($pv['min'] < $config['min']) {
  92. $config['min'] = $pv['min'];
  93. }
  94. if ($pv['max'] > $config['max']) {
  95. $config['max'] = $pv['max'];
  96. }
  97. if ($pv['rate'] < $config['rate']) {
  98. $config['max_rate'] = $config['rate'];
  99. $config['min_rate'] = $pv['rate'];
  100. }
  101. if ($pv['rate'] > $config['rate']) {
  102. $config['max_rate'] = $pv['rate'];
  103. }
  104. $config['range'][] = $pv;
  105. } else {
  106. $config = $pv;
  107. }
  108. $list[$pv['type']] = [
  109. 'key' => $key,
  110. 'label' => lang($pv['name']),
  111. 'value' => $pv['type'],
  112. 'config' => $config ?? [],
  113. ];
  114. if ((int)$data_type === 1) {
  115. $logo = array_key_exists($pv['type'], $logos) ? $logos[$pv['type']] : ($config['logo'] ?? '');
  116. $list[$pv['type']]['logo'] = $logo ? (string)Util::ensureUrl($logo) : '';
  117. }
  118. }
  119. $list = array_column($list, null, 'key');
  120. return array_values($list);
  121. }
  122. //校验是否支持此提现方式
  123. public static function checkWithdrawChannel($type, $recharge_channel_group_id = 1) {
  124. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  125. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  126. $withdrawTypes = $group ? (array)$group->withdraw_type : [];
  127. if (!in_array($type, $withdrawTypes, true)) {
  128. return false;
  129. }
  130. return self::query()->where('data_type', 2)->where('type', $type)
  131. ->available()->orderBy('sort')->first() ?: false;
  132. }
  133. //校验是否支持此充值方式
  134. public static function checkRechargeChannel($type, $recharge_channel_group_id = 1) {
  135. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  136. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  137. $rechargeTypes = $group ? (array)$group->recharge_type : [];
  138. if (!in_array($type, $rechargeTypes, true)) {
  139. return false;
  140. }
  141. return self::query()->where('data_type', 1)->where('type', $type)
  142. ->available()->orderBy('sort')->first() ?: false;
  143. }
  144. }