You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Payment/RechargeMethodsService.php

58 lines
1.9 KiB
PHP

<?php
namespace App\Services\Payment;
use App\Models\User;
/** Exposes only payment methods currently usable by a user. */
class RechargeMethodsService
{
public static function forUser(User $user): array
{
$methods = [];
if (PaytaroConfig::isUserVisible($user)) {
$values = PaytaroConfig::values();
$methods[] = self::method('paytaro', '支付宝', $values['paytaro_min_amount'], $values['paytaro_max_amount'], [
'provider' => 'paytaro',
]);
}
$yipayValues = YipayConfig::values();
foreach (YipayConfig::visibleMethods($user, $yipayValues) as $method) {
// visibleMethods reflects switches; runtimeForUser additionally
// validates credentials, callback URL and the selected method.
try {
YipayConfig::runtimeForUser($user, $method);
} catch (\Throwable $exception) {
continue;
}
$label = $method === 'wxpay' ? '微信支付' : '支付宝支付';
$methods[] = self::method('yipay_' . $method, $label, $yipayValues['yipay_min_amount'], $yipayValues['yipay_max_amount'], [
'provider' => 'yipay',
'method' => $method,
]);
}
if (UsdtConfig::isUserVisible($user)) {
$values = UsdtConfig::values();
$methods[] = self::method('usdt', 'USDT', $values['usdt_min_amount'], '9999999.99', [
'provider' => 'usdt',
'network' => 'tron',
]);
}
return $methods;
}
private static function method(string $id, string $label, $minimum, $maximum, array $extra = []): array
{
return array_merge([
'id' => $id,
'label' => $label,
'min_amount' => PaymentAmount::normalize($minimum),
'max_amount' => PaymentAmount::normalize($maximum),
], $extra);
}
}