chore: bootstrap module from working instance and add install guide
This commit is contained in:
209
models/forms/ProviderSettingsForm.php
Normal file
209
models/forms/ProviderSettingsForm.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models\forms;
|
||||
|
||||
use humhub\modules\content\components\ContentContainerActiveRecord;
|
||||
use humhub\modules\donations\models\DonationProviderConfig;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
class ProviderSettingsForm extends Model
|
||||
{
|
||||
public ContentContainerActiveRecord $contentContainer;
|
||||
|
||||
public $sandbox_mode = true;
|
||||
public $default_currency = 'USD';
|
||||
public $animal_tile_extra_height_px = 0;
|
||||
public $animal_donation_form_header = '';
|
||||
|
||||
public $paypal_enabled = false;
|
||||
public $paypal_recurring_enabled = false;
|
||||
public $paypal_sandbox_client_id = '';
|
||||
public $paypal_sandbox_client_secret = '';
|
||||
public $paypal_sandbox_webhook_id = '';
|
||||
public $paypal_live_client_id = '';
|
||||
public $paypal_live_client_secret = '';
|
||||
public $paypal_live_webhook_id = '';
|
||||
|
||||
public $stripe_enabled = false;
|
||||
public $stripe_recurring_enabled = false;
|
||||
public $stripe_sandbox_publishable_key = '';
|
||||
public $stripe_sandbox_secret_key = '';
|
||||
public $stripe_sandbox_webhook_secret = '';
|
||||
public $stripe_live_publishable_key = '';
|
||||
public $stripe_live_secret_key = '';
|
||||
public $stripe_live_webhook_secret = '';
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['sandbox_mode', 'paypal_enabled', 'paypal_recurring_enabled', 'stripe_enabled', 'stripe_recurring_enabled'], 'boolean'],
|
||||
[['animal_tile_extra_height_px'], 'integer', 'min' => 0, 'max' => 600],
|
||||
[['animal_donation_form_header'], 'string', 'max' => 255],
|
||||
[[
|
||||
'paypal_sandbox_client_id',
|
||||
'paypal_sandbox_client_secret',
|
||||
'paypal_sandbox_webhook_id',
|
||||
'paypal_live_client_id',
|
||||
'paypal_live_client_secret',
|
||||
'paypal_live_webhook_id',
|
||||
'stripe_sandbox_publishable_key',
|
||||
'stripe_sandbox_secret_key',
|
||||
'stripe_sandbox_webhook_secret',
|
||||
'stripe_live_publishable_key',
|
||||
'stripe_live_secret_key',
|
||||
'stripe_live_webhook_secret',
|
||||
], 'string', 'max' => 255],
|
||||
[['default_currency'], 'string', 'max' => 8],
|
||||
[['default_currency'], 'match', 'pattern' => '/^[A-Z]{3}$/'],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'sandbox_mode' => Yii::t('DonationsModule.base', 'Use Sandbox Mode'),
|
||||
'default_currency' => Yii::t('DonationsModule.base', 'Default Currency'),
|
||||
'animal_tile_extra_height_px' => Yii::t('DonationsModule.base', 'Animal Tile Extra Height (px)'),
|
||||
'animal_donation_form_header' => Yii::t('DonationsModule.base', 'Animal Donation Form Header'),
|
||||
'paypal_enabled' => Yii::t('DonationsModule.base', 'Enable PayPal'),
|
||||
'paypal_recurring_enabled' => Yii::t('DonationsModule.base', 'Enable PayPal Recurring'),
|
||||
'paypal_sandbox_client_id' => Yii::t('DonationsModule.base', 'Sandbox Client ID'),
|
||||
'paypal_sandbox_client_secret' => Yii::t('DonationsModule.base', 'Sandbox Client Secret'),
|
||||
'paypal_sandbox_webhook_id' => Yii::t('DonationsModule.base', 'Sandbox Webhook ID'),
|
||||
'paypal_live_client_id' => Yii::t('DonationsModule.base', 'Live Client ID'),
|
||||
'paypal_live_client_secret' => Yii::t('DonationsModule.base', 'Live Client Secret'),
|
||||
'paypal_live_webhook_id' => Yii::t('DonationsModule.base', 'Live Webhook ID'),
|
||||
'stripe_enabled' => Yii::t('DonationsModule.base', 'Enable Stripe'),
|
||||
'stripe_recurring_enabled' => Yii::t('DonationsModule.base', 'Enable Stripe Recurring'),
|
||||
'stripe_sandbox_publishable_key' => Yii::t('DonationsModule.base', 'Sandbox Publishable Key'),
|
||||
'stripe_sandbox_secret_key' => Yii::t('DonationsModule.base', 'Sandbox Secret Key'),
|
||||
'stripe_sandbox_webhook_secret' => Yii::t('DonationsModule.base', 'Sandbox Webhook Secret'),
|
||||
'stripe_live_publishable_key' => Yii::t('DonationsModule.base', 'Live Publishable Key'),
|
||||
'stripe_live_secret_key' => Yii::t('DonationsModule.base', 'Live Secret Key'),
|
||||
'stripe_live_webhook_secret' => Yii::t('DonationsModule.base', 'Live Webhook Secret'),
|
||||
];
|
||||
}
|
||||
|
||||
public function loadValues(): void
|
||||
{
|
||||
$record = null;
|
||||
if ($this->hasProviderConfigTable()) {
|
||||
$record = DonationProviderConfig::findOne(['contentcontainer_id' => $this->contentContainer->contentcontainer_id]);
|
||||
}
|
||||
|
||||
$settings = Yii::$app->getModule('donations')->settings->contentContainer($this->contentContainer);
|
||||
|
||||
$this->sandbox_mode = (bool)($settings->get('sandbox_mode', $record->sandbox_mode ?? 1));
|
||||
$this->default_currency = strtoupper((string)$settings->get('default_currency', $record->default_currency ?? 'USD'));
|
||||
$this->animal_tile_extra_height_px = (int)$settings->get('animal_tile_extra_height_px', 0);
|
||||
$this->animal_donation_form_header = (string)$settings->get('animal_donation_form_header', Yii::t('DonationsModule.base', 'Your Donation directly supports [animal-name]'));
|
||||
|
||||
$this->paypal_enabled = (bool)($settings->get('paypal_enabled', $record->paypal_enabled ?? 0));
|
||||
$this->paypal_recurring_enabled = (bool)($settings->get('paypal_recurring_enabled', $record->paypal_recurring_enabled ?? 0));
|
||||
$legacyPaypalClientId = (string)$settings->get('paypal_client_id', $record->paypal_client_id ?? '');
|
||||
$legacyPaypalClientSecret = (string)$settings->get('paypal_client_secret', $record->paypal_client_secret ?? '');
|
||||
$legacyPaypalWebhookId = (string)$settings->get('paypal_webhook_id', $record->paypal_webhook_id ?? '');
|
||||
|
||||
$this->paypal_sandbox_client_id = (string)$settings->get('paypal_sandbox_client_id', $legacyPaypalClientId);
|
||||
$this->paypal_sandbox_client_secret = (string)$settings->get('paypal_sandbox_client_secret', $legacyPaypalClientSecret);
|
||||
$this->paypal_sandbox_webhook_id = (string)$settings->get('paypal_sandbox_webhook_id', $legacyPaypalWebhookId);
|
||||
$this->paypal_live_client_id = (string)$settings->get('paypal_live_client_id', $legacyPaypalClientId);
|
||||
$this->paypal_live_client_secret = (string)$settings->get('paypal_live_client_secret', $legacyPaypalClientSecret);
|
||||
$this->paypal_live_webhook_id = (string)$settings->get('paypal_live_webhook_id', $legacyPaypalWebhookId);
|
||||
|
||||
$this->stripe_enabled = (bool)($settings->get('stripe_enabled', $record->stripe_enabled ?? 0));
|
||||
$this->stripe_recurring_enabled = (bool)($settings->get('stripe_recurring_enabled', $record->stripe_recurring_enabled ?? 0));
|
||||
$legacyStripePublishable = (string)$settings->get('stripe_publishable_key', $record->stripe_publishable_key ?? '');
|
||||
$legacyStripeSecret = (string)$settings->get('stripe_secret_key', $record->stripe_secret_key ?? '');
|
||||
$legacyStripeWebhook = (string)$settings->get('stripe_webhook_secret', $record->stripe_webhook_secret ?? '');
|
||||
|
||||
$this->stripe_sandbox_publishable_key = (string)$settings->get('stripe_sandbox_publishable_key', $legacyStripePublishable);
|
||||
$this->stripe_sandbox_secret_key = (string)$settings->get('stripe_sandbox_secret_key', $legacyStripeSecret);
|
||||
$this->stripe_sandbox_webhook_secret = (string)$settings->get('stripe_sandbox_webhook_secret', $legacyStripeWebhook);
|
||||
$this->stripe_live_publishable_key = (string)$settings->get('stripe_live_publishable_key', $legacyStripePublishable);
|
||||
$this->stripe_live_secret_key = (string)$settings->get('stripe_live_secret_key', $legacyStripeSecret);
|
||||
$this->stripe_live_webhook_secret = (string)$settings->get('stripe_live_webhook_secret', $legacyStripeWebhook);
|
||||
}
|
||||
|
||||
public function save(): bool
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$settings = Yii::$app->getModule('donations')->settings->contentContainer($this->contentContainer);
|
||||
|
||||
$settings->set('sandbox_mode', $this->sandbox_mode ? 1 : 0);
|
||||
$currency = strtoupper(trim((string)$this->default_currency)) ?: 'USD';
|
||||
$settings->set('default_currency', $currency);
|
||||
$settings->set('animal_tile_extra_height_px', (int)$this->animal_tile_extra_height_px);
|
||||
$settings->set('animal_donation_form_header', trim((string)$this->animal_donation_form_header));
|
||||
|
||||
$settings->set('paypal_enabled', $this->paypal_enabled ? 1 : 0);
|
||||
$settings->set('paypal_recurring_enabled', $this->paypal_recurring_enabled ? 1 : 0);
|
||||
$settings->set('paypal_sandbox_client_id', trim((string)$this->paypal_sandbox_client_id));
|
||||
$settings->set('paypal_sandbox_client_secret', trim((string)$this->paypal_sandbox_client_secret));
|
||||
$settings->set('paypal_sandbox_webhook_id', trim((string)$this->paypal_sandbox_webhook_id));
|
||||
$settings->set('paypal_live_client_id', trim((string)$this->paypal_live_client_id));
|
||||
$settings->set('paypal_live_client_secret', trim((string)$this->paypal_live_client_secret));
|
||||
$settings->set('paypal_live_webhook_id', trim((string)$this->paypal_live_webhook_id));
|
||||
|
||||
$settings->set('stripe_enabled', $this->stripe_enabled ? 1 : 0);
|
||||
$settings->set('stripe_recurring_enabled', $this->stripe_recurring_enabled ? 1 : 0);
|
||||
$settings->set('stripe_sandbox_publishable_key', trim((string)$this->stripe_sandbox_publishable_key));
|
||||
$settings->set('stripe_sandbox_secret_key', trim((string)$this->stripe_sandbox_secret_key));
|
||||
$settings->set('stripe_sandbox_webhook_secret', trim((string)$this->stripe_sandbox_webhook_secret));
|
||||
$settings->set('stripe_live_publishable_key', trim((string)$this->stripe_live_publishable_key));
|
||||
$settings->set('stripe_live_secret_key', trim((string)$this->stripe_live_secret_key));
|
||||
$settings->set('stripe_live_webhook_secret', trim((string)$this->stripe_live_webhook_secret));
|
||||
|
||||
$paypalClientId = trim((string)($this->sandbox_mode ? $this->paypal_sandbox_client_id : $this->paypal_live_client_id));
|
||||
$paypalClientSecret = trim((string)($this->sandbox_mode ? $this->paypal_sandbox_client_secret : $this->paypal_live_client_secret));
|
||||
$paypalWebhookId = trim((string)($this->sandbox_mode ? $this->paypal_sandbox_webhook_id : $this->paypal_live_webhook_id));
|
||||
|
||||
$stripePublishable = trim((string)($this->sandbox_mode ? $this->stripe_sandbox_publishable_key : $this->stripe_live_publishable_key));
|
||||
$stripeSecret = trim((string)($this->sandbox_mode ? $this->stripe_sandbox_secret_key : $this->stripe_live_secret_key));
|
||||
$stripeWebhook = trim((string)($this->sandbox_mode ? $this->stripe_sandbox_webhook_secret : $this->stripe_live_webhook_secret));
|
||||
|
||||
$settings->set('paypal_client_id', $paypalClientId);
|
||||
$settings->set('paypal_client_secret', $paypalClientSecret);
|
||||
$settings->set('paypal_webhook_id', $paypalWebhookId);
|
||||
$settings->set('stripe_publishable_key', $stripePublishable);
|
||||
$settings->set('stripe_secret_key', $stripeSecret);
|
||||
$settings->set('stripe_webhook_secret', $stripeWebhook);
|
||||
|
||||
if ($this->hasProviderConfigTable()) {
|
||||
$record = DonationProviderConfig::findOne(['contentcontainer_id' => $this->contentContainer->contentcontainer_id]);
|
||||
if (!$record instanceof DonationProviderConfig) {
|
||||
$record = new DonationProviderConfig();
|
||||
$record->contentcontainer_id = $this->contentContainer->contentcontainer_id;
|
||||
}
|
||||
|
||||
$record->sandbox_mode = $this->sandbox_mode ? 1 : 0;
|
||||
$record->paypal_enabled = $this->paypal_enabled ? 1 : 0;
|
||||
$record->paypal_recurring_enabled = $this->paypal_recurring_enabled ? 1 : 0;
|
||||
$record->paypal_client_id = $paypalClientId;
|
||||
$record->paypal_client_secret = $paypalClientSecret;
|
||||
$record->paypal_webhook_id = $paypalWebhookId;
|
||||
$record->stripe_enabled = $this->stripe_enabled ? 1 : 0;
|
||||
$record->stripe_recurring_enabled = $this->stripe_recurring_enabled ? 1 : 0;
|
||||
$record->stripe_publishable_key = $stripePublishable;
|
||||
$record->stripe_secret_key = $stripeSecret;
|
||||
$record->stripe_webhook_secret = $stripeWebhook;
|
||||
$record->default_currency = $currency;
|
||||
|
||||
if (!$record->save()) {
|
||||
$this->addErrors($record->getErrors());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function hasProviderConfigTable(): bool
|
||||
{
|
||||
return Yii::$app->db->schema->getTableSchema(DonationProviderConfig::tableName(), true) !== null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user