chore: bootstrap module from working instance and add install guide
This commit is contained in:
134
services/providers/ProviderCredentialResolver.php
Normal file
134
services/providers/ProviderCredentialResolver.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\services\providers;
|
||||
|
||||
use humhub\modules\donations\models\DonationProviderConfig;
|
||||
use humhub\modules\space\models\Space;
|
||||
use Yii;
|
||||
|
||||
class ProviderCredentialResolver
|
||||
{
|
||||
public static function isSandboxMode(DonationProviderConfig $config): bool
|
||||
{
|
||||
$settings = self::settingsForConfig($config);
|
||||
$value = $settings !== null
|
||||
? $settings->get('sandbox_mode', $config->sandbox_mode ?? 1)
|
||||
: ($config->sandbox_mode ?? 1);
|
||||
|
||||
return (int)$value === 1;
|
||||
}
|
||||
|
||||
public static function resolvePayPalClientId(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'paypal_sandbox_client_id',
|
||||
'paypal_live_client_id',
|
||||
(string)$config->paypal_client_id
|
||||
);
|
||||
}
|
||||
|
||||
public static function resolvePayPalClientSecret(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'paypal_sandbox_client_secret',
|
||||
'paypal_live_client_secret',
|
||||
(string)$config->paypal_client_secret
|
||||
);
|
||||
}
|
||||
|
||||
public static function resolvePayPalWebhookId(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'paypal_sandbox_webhook_id',
|
||||
'paypal_live_webhook_id',
|
||||
(string)$config->paypal_webhook_id
|
||||
);
|
||||
}
|
||||
|
||||
public static function resolveStripePublishableKey(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'stripe_sandbox_publishable_key',
|
||||
'stripe_live_publishable_key',
|
||||
(string)$config->stripe_publishable_key
|
||||
);
|
||||
}
|
||||
|
||||
public static function resolveStripeSecretKey(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'stripe_sandbox_secret_key',
|
||||
'stripe_live_secret_key',
|
||||
(string)$config->stripe_secret_key
|
||||
);
|
||||
}
|
||||
|
||||
public static function resolveStripeWebhookSecret(DonationProviderConfig $config): string
|
||||
{
|
||||
return self::resolveModeValue(
|
||||
$config,
|
||||
'stripe_sandbox_webhook_secret',
|
||||
'stripe_live_webhook_secret',
|
||||
(string)$config->stripe_webhook_secret
|
||||
);
|
||||
}
|
||||
|
||||
private static function resolveModeValue(
|
||||
DonationProviderConfig $config,
|
||||
string $sandboxKey,
|
||||
string $liveKey,
|
||||
string $legacyFallback
|
||||
): string {
|
||||
$settings = self::settingsForConfig($config);
|
||||
$isSandbox = self::isSandboxMode($config);
|
||||
|
||||
if ($settings === null) {
|
||||
return trim($legacyFallback);
|
||||
}
|
||||
|
||||
$sandboxValue = trim((string)$settings->get($sandboxKey, ''));
|
||||
$liveValue = trim((string)$settings->get($liveKey, ''));
|
||||
$legacyValue = trim((string)$settings->get(self::legacyKeyFor($sandboxKey, $liveKey), $legacyFallback));
|
||||
|
||||
if ($isSandbox) {
|
||||
return $sandboxValue !== '' ? $sandboxValue : $legacyValue;
|
||||
}
|
||||
|
||||
return $liveValue !== '' ? $liveValue : $legacyValue;
|
||||
}
|
||||
|
||||
private static function legacyKeyFor(string $sandboxKey, string $liveKey): string
|
||||
{
|
||||
$map = [
|
||||
'paypal_sandbox_client_id' => 'paypal_client_id',
|
||||
'paypal_live_client_id' => 'paypal_client_id',
|
||||
'paypal_sandbox_client_secret' => 'paypal_client_secret',
|
||||
'paypal_live_client_secret' => 'paypal_client_secret',
|
||||
'paypal_sandbox_webhook_id' => 'paypal_webhook_id',
|
||||
'paypal_live_webhook_id' => 'paypal_webhook_id',
|
||||
'stripe_sandbox_publishable_key' => 'stripe_publishable_key',
|
||||
'stripe_live_publishable_key' => 'stripe_publishable_key',
|
||||
'stripe_sandbox_secret_key' => 'stripe_secret_key',
|
||||
'stripe_live_secret_key' => 'stripe_secret_key',
|
||||
'stripe_sandbox_webhook_secret' => 'stripe_webhook_secret',
|
||||
'stripe_live_webhook_secret' => 'stripe_webhook_secret',
|
||||
];
|
||||
|
||||
return $map[$sandboxKey] ?? ($map[$liveKey] ?? '');
|
||||
}
|
||||
|
||||
private static function settingsForConfig(DonationProviderConfig $config)
|
||||
{
|
||||
$space = Space::findOne(['contentcontainer_id' => (int)$config->contentcontainer_id]);
|
||||
if (!$space instanceof Space) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Yii::$app->getModule('donations')->settings->contentContainer($space);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user