RechargeChannel.php 5.7 KB

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