chore: bootstrap module from working instance and add install guide
This commit is contained in:
46
models/DonationBlock.php
Normal file
46
models/DonationBlock.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
|
||||
class DonationBlock extends ActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_block';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['contentcontainer_id', 'placement'], 'required'],
|
||||
[['contentcontainer_id', 'goal_id', 'is_active', 'sort_order'], 'integer'],
|
||||
[['placement'], 'string', 'max' => 64],
|
||||
[['header'], 'string', 'max' => 190],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = $now;
|
||||
}
|
||||
$this->updated_at = $now;
|
||||
|
||||
if ($this->sort_order === null) {
|
||||
$this->sort_order = 100;
|
||||
}
|
||||
|
||||
if ($this->is_active === null) {
|
||||
$this->is_active = 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
69
models/DonationGoal.php
Normal file
69
models/DonationGoal.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
use Yii;
|
||||
|
||||
class DonationGoal extends ActiveRecord
|
||||
{
|
||||
public const TYPE_RESCUE_GENERAL = 'rescue_general';
|
||||
public const TYPE_ANIMAL = 'animal';
|
||||
public const TYPE_NEED = 'need';
|
||||
public const TYPE_SPECIAL_NEED = 'special_need';
|
||||
public const TYPE_SPONSORSHIP = 'sponsorship';
|
||||
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_goal';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['contentcontainer_id', 'goal_type', 'title'], 'required'],
|
||||
[['contentcontainer_id', 'target_animal_id', 'is_active'], 'integer'],
|
||||
[['description'], 'string'],
|
||||
[['target_amount', 'current_amount'], 'number', 'min' => 0],
|
||||
[['goal_type'], 'string', 'max' => 32],
|
||||
[['title'], 'string', 'max' => 190],
|
||||
[['image_path'], 'string', 'max' => 255],
|
||||
[['currency'], 'string', 'max' => 8],
|
||||
[['goal_type'], 'in', 'range' => array_keys(self::goalTypeOptions())],
|
||||
];
|
||||
}
|
||||
|
||||
public static function goalTypeOptions(): array
|
||||
{
|
||||
return [
|
||||
self::TYPE_RESCUE_GENERAL => Yii::t('DonationsModule.base', 'Rescue General'),
|
||||
self::TYPE_ANIMAL => Yii::t('DonationsModule.base', 'Animal'),
|
||||
self::TYPE_NEED => Yii::t('DonationsModule.base', 'Need Specific'),
|
||||
self::TYPE_SPECIAL_NEED => Yii::t('DonationsModule.base', 'Special Need'),
|
||||
self::TYPE_SPONSORSHIP => Yii::t('DonationsModule.base', 'Sponsorship'),
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = $now;
|
||||
}
|
||||
$this->updated_at = $now;
|
||||
|
||||
if (empty($this->currency)) {
|
||||
$this->currency = 'USD';
|
||||
}
|
||||
|
||||
if ($this->is_active === null) {
|
||||
$this->is_active = 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
44
models/DonationProviderConfig.php
Normal file
44
models/DonationProviderConfig.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
|
||||
class DonationProviderConfig extends ActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_provider_config';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['contentcontainer_id'], 'required'],
|
||||
[['contentcontainer_id'], 'integer'],
|
||||
[['paypal_enabled', 'paypal_recurring_enabled', 'stripe_enabled', 'stripe_recurring_enabled', 'sandbox_mode'], 'boolean'],
|
||||
[['paypal_client_id', 'paypal_client_secret', 'paypal_webhook_id', 'stripe_publishable_key', 'stripe_secret_key', 'stripe_webhook_secret'], 'string', 'max' => 255],
|
||||
[['default_currency'], 'string', 'max' => 8],
|
||||
[['contentcontainer_id'], 'unique'],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = $now;
|
||||
}
|
||||
$this->updated_at = $now;
|
||||
|
||||
if (empty($this->default_currency)) {
|
||||
$this->default_currency = 'USD';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
45
models/DonationSubscription.php
Normal file
45
models/DonationSubscription.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
|
||||
class DonationSubscription extends ActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_subscription';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['contentcontainer_id', 'provider', 'provider_subscription_id', 'status', 'amount'], 'required'],
|
||||
[['contentcontainer_id', 'donor_user_id', 'goal_id', 'interval_count'], 'integer'],
|
||||
[['amount'], 'number', 'min' => 0.01],
|
||||
[['provider', 'status'], 'string', 'max' => 32],
|
||||
[['provider_subscription_id'], 'string', 'max' => 190],
|
||||
[['currency'], 'string', 'max' => 8],
|
||||
[['interval_unit'], 'string', 'max' => 16],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = $now;
|
||||
}
|
||||
$this->updated_at = $now;
|
||||
|
||||
if (empty($this->currency)) {
|
||||
$this->currency = 'USD';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
52
models/DonationTransaction.php
Normal file
52
models/DonationTransaction.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
|
||||
class DonationTransaction extends ActiveRecord
|
||||
{
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_SUCCEEDED = 'succeeded';
|
||||
public const STATUS_FAILED = 'failed';
|
||||
public const STATUS_CANCELLED = 'cancelled';
|
||||
public const STATUS_REFUNDED = 'refunded';
|
||||
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_transaction';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['contentcontainer_id', 'provider', 'mode', 'status', 'amount'], 'required'],
|
||||
[['contentcontainer_id', 'donor_user_id', 'is_anonymous', 'goal_id', 'target_animal_id'], 'integer'],
|
||||
[['amount'], 'number', 'min' => 0.01],
|
||||
[['metadata_json'], 'string'],
|
||||
[['provider', 'status', 'goal_type'], 'string', 'max' => 32],
|
||||
[['mode'], 'string', 'max' => 16],
|
||||
[['currency'], 'string', 'max' => 8],
|
||||
[['provider_payment_id', 'provider_checkout_id', 'provider_subscription_id', 'provider_customer_id'], 'string', 'max' => 190],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = $now;
|
||||
}
|
||||
$this->updated_at = $now;
|
||||
|
||||
if (empty($this->currency)) {
|
||||
$this->currency = 'USD';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
43
models/DonationWebhookEvent.php
Normal file
43
models/DonationWebhookEvent.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models;
|
||||
|
||||
use humhub\components\ActiveRecord;
|
||||
|
||||
class DonationWebhookEvent extends ActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return 'rescue_donation_webhook_event';
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['provider', 'provider_event_id'], 'required'],
|
||||
[['is_processed'], 'integer'],
|
||||
[['payload_json'], 'string'],
|
||||
[['provider'], 'string', 'max' => 32],
|
||||
[['provider_event_id'], 'string', 'max' => 190],
|
||||
[['event_type'], 'string', 'max' => 120],
|
||||
[['provider', 'provider_event_id'], 'unique', 'targetAttribute' => ['provider', 'provider_event_id']],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($insert && empty($this->created_at)) {
|
||||
$this->created_at = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
if ($this->is_processed === null) {
|
||||
$this->is_processed = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
271
models/forms/DonationGoalForm.php
Normal file
271
models/forms/DonationGoalForm.php
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\donations\models\forms;
|
||||
|
||||
use humhub\modules\content\components\ContentContainerActiveRecord;
|
||||
use humhub\modules\donations\models\DonationGoal;
|
||||
use humhub\modules\donations\models\DonationProviderConfig;
|
||||
use humhub\modules\rescue_foundation\components\UploadStandards;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\helpers\FileHelper;
|
||||
use yii\web\UploadedFile;
|
||||
|
||||
class DonationGoalForm extends Model
|
||||
{
|
||||
public ContentContainerActiveRecord $contentContainer;
|
||||
|
||||
public $id = null;
|
||||
public $goal_type = DonationGoal::TYPE_RESCUE_GENERAL;
|
||||
public $target_animal_id = null;
|
||||
public $title = '';
|
||||
public $description = '';
|
||||
public $image_path = '';
|
||||
public UploadedFile|string|null $imageFile = null;
|
||||
public $imageGalleryPath = '';
|
||||
public $target_amount = 0.0;
|
||||
public $current_amount = 0.0;
|
||||
public $is_active = true;
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['goal_type', 'title', 'target_amount'], 'required'],
|
||||
[['id', 'target_animal_id'], 'integer'],
|
||||
[['description'], 'string'],
|
||||
[['target_amount', 'current_amount'], 'number', 'min' => 0],
|
||||
[['is_active'], 'boolean'],
|
||||
[['goal_type'], 'in', 'range' => array_keys(DonationGoal::goalTypeOptions())],
|
||||
[['title'], 'string', 'max' => 190],
|
||||
[['image_path'], 'string', 'max' => 255],
|
||||
[['imageGalleryPath'], 'string', 'max' => 500],
|
||||
[['imageGalleryPath'], 'validateGalleryImageSelection'],
|
||||
[['imageFile'], 'file',
|
||||
'skipOnEmpty' => true,
|
||||
'extensions' => UploadStandards::imageExtensions(),
|
||||
'checkExtensionByMimeType' => true,
|
||||
'mimeTypes' => UploadStandards::imageMimeTypes(),
|
||||
'maxSize' => UploadStandards::IMAGE_MAX_BYTES,
|
||||
],
|
||||
[['target_animal_id'], 'required', 'when' => function (self $model) {
|
||||
return $model->goal_type === DonationGoal::TYPE_ANIMAL;
|
||||
}, 'whenClient' => "function () { return $('#donationgoalform-goal_type').val() === 'animal'; }"],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'target_animal_id' => Yii::t('DonationsModule.base', 'Target Animal'),
|
||||
'target_amount' => Yii::t('DonationsModule.base', 'Target Amount'),
|
||||
'current_amount' => Yii::t('DonationsModule.base', 'Current Amount'),
|
||||
'imageGalleryPath' => Yii::t('DonationsModule.base', 'Image'),
|
||||
'imageFile' => Yii::t('DonationsModule.base', 'Image Upload'),
|
||||
];
|
||||
}
|
||||
|
||||
public function loadFromGoal(DonationGoal $goal): void
|
||||
{
|
||||
$this->id = (int)$goal->id;
|
||||
$this->goal_type = (string)$goal->goal_type;
|
||||
$this->target_animal_id = $goal->target_animal_id !== null ? (int)$goal->target_animal_id : null;
|
||||
$this->title = (string)$goal->title;
|
||||
$this->description = (string)$goal->description;
|
||||
$this->image_path = (string)$goal->image_path;
|
||||
$this->imageGalleryPath = (string)$goal->image_path;
|
||||
$this->target_amount = (float)$goal->target_amount;
|
||||
$this->current_amount = (float)$goal->current_amount;
|
||||
$this->is_active = (int)$goal->is_active === 1;
|
||||
}
|
||||
|
||||
public function save(): ?DonationGoal
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$goal = null;
|
||||
if ($this->id) {
|
||||
$goal = DonationGoal::findOne([
|
||||
'id' => $this->id,
|
||||
'contentcontainer_id' => $this->contentContainer->contentcontainer_id,
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$goal instanceof DonationGoal) {
|
||||
$goal = new DonationGoal();
|
||||
$goal->contentcontainer_id = $this->contentContainer->contentcontainer_id;
|
||||
}
|
||||
|
||||
$goal->goal_type = $this->goal_type;
|
||||
$goal->target_animal_id = $this->goal_type === DonationGoal::TYPE_ANIMAL ? (int)$this->target_animal_id : null;
|
||||
$goal->title = trim($this->title);
|
||||
$goal->description = trim($this->description);
|
||||
$goal->image_path = trim((string)$goal->image_path);
|
||||
$goal->target_amount = $this->target_amount;
|
||||
$goal->current_amount = $this->current_amount;
|
||||
$goal->currency = $this->resolveDefaultCurrency();
|
||||
$goal->is_active = $this->is_active ? 1 : 0;
|
||||
|
||||
if (!$goal->save()) {
|
||||
$this->addErrors($goal->getErrors());
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->imageFile instanceof UploadedFile) {
|
||||
$stored = $this->storeImage($this->imageFile, $goal);
|
||||
if ($stored === null) {
|
||||
$this->addError('imageFile', Yii::t('DonationsModule.base', 'Could not upload goal image.'));
|
||||
return null;
|
||||
}
|
||||
$goal->image_path = $stored;
|
||||
} else {
|
||||
$selectedFromGallery = trim((string)$this->imageGalleryPath);
|
||||
if ($selectedFromGallery !== '') {
|
||||
$goal->image_path = $selectedFromGallery;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$goal->save()) {
|
||||
$this->addErrors($goal->getErrors());
|
||||
return null;
|
||||
}
|
||||
|
||||
return $goal;
|
||||
}
|
||||
|
||||
public function validateGalleryImageSelection(string $attribute): void
|
||||
{
|
||||
$value = trim((string)$this->$attribute);
|
||||
if ($value === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->goal_type !== DonationGoal::TYPE_ANIMAL || (int)$this->target_animal_id <= 0) {
|
||||
if (preg_match('/^(https?:\/\/|\/)/i', $value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addError($attribute, Yii::t('DonationsModule.base', 'Please select a valid image source.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$options = $this->getGalleryImageOptionsForAnimal((int)$this->target_animal_id);
|
||||
if (!isset($options[$value])) {
|
||||
$this->addError($attribute, Yii::t('DonationsModule.base', 'Please select an image from this animal gallery or upload a new one.'));
|
||||
}
|
||||
}
|
||||
|
||||
public function getAnimalOptions(): array
|
||||
{
|
||||
$animalClass = 'humhub\\modules\\animal_management\\models\\Animal';
|
||||
if (!class_exists($animalClass)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$tableName = $animalClass::tableName();
|
||||
if (Yii::$app->db->schema->getTableSchema($tableName, true) === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$animals = $animalClass::find()
|
||||
->where(['contentcontainer_id' => $this->contentContainer->contentcontainer_id])
|
||||
->orderBy(['name' => SORT_ASC, 'id' => SORT_ASC])
|
||||
->all();
|
||||
|
||||
$options = [];
|
||||
foreach ($animals as $animal) {
|
||||
$label = method_exists($animal, 'getDisplayName')
|
||||
? (string)$animal->getDisplayName()
|
||||
: trim((string)($animal->name ?? ''));
|
||||
|
||||
if ($label === '') {
|
||||
$label = 'Animal #' . (int)$animal->id;
|
||||
}
|
||||
|
||||
$options[(int)$animal->id] = $label;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function getGalleryImageOptionsForAnimal(int $animalId): array
|
||||
{
|
||||
$animalGalleryClass = 'humhub\\modules\\animal_management\\models\\AnimalGalleryItem';
|
||||
if (!class_exists($animalGalleryClass)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Yii::$app->db->schema->getTableSchema($animalGalleryClass::tableName(), true) === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$items = $animalGalleryClass::find()
|
||||
->where(['animal_id' => $animalId])
|
||||
->orderBy(['id' => SORT_DESC])
|
||||
->all();
|
||||
|
||||
$options = [];
|
||||
foreach ($items as $item) {
|
||||
$url = trim((string)$item->getImageUrl());
|
||||
if ($url === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[$url] = Yii::t('DonationsModule.base', 'Gallery Image #{id}', [
|
||||
'id' => (int)$item->id,
|
||||
]);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
private function resolveDefaultCurrency(): string
|
||||
{
|
||||
$config = DonationProviderConfig::findOne([
|
||||
'contentcontainer_id' => $this->contentContainer->contentcontainer_id,
|
||||
]);
|
||||
|
||||
if ($config instanceof DonationProviderConfig) {
|
||||
$currency = strtoupper(trim((string)$config->default_currency));
|
||||
if ($currency !== '') {
|
||||
return $currency;
|
||||
}
|
||||
}
|
||||
|
||||
return 'USD';
|
||||
}
|
||||
|
||||
private function storeImage(UploadedFile $file, DonationGoal $goal): ?string
|
||||
{
|
||||
$random = Yii::$app->security->generateRandomString(8);
|
||||
$extension = strtolower((string)$file->extension);
|
||||
$fileName = 'goal-' . (int)$goal->id . '-' . time() . '-' . $random . '.' . $extension;
|
||||
|
||||
$candidateDirs = [
|
||||
'/uploads/donations/goals/' . (int)$goal->id,
|
||||
'/uploads/donations-runtime/goals/' . (int)$goal->id,
|
||||
];
|
||||
|
||||
foreach ($candidateDirs as $relativeDir) {
|
||||
$absoluteDir = Yii::getAlias('@webroot') . $relativeDir;
|
||||
try {
|
||||
FileHelper::createDirectory($absoluteDir, 0775, true);
|
||||
} catch (\Throwable $e) {
|
||||
Yii::warning($e->getMessage(), 'donations.goal_image_upload_dir');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_dir($absoluteDir) || !is_writable($absoluteDir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$absolutePath = $absoluteDir . '/' . $fileName;
|
||||
if ($file->saveAs($absolutePath)) {
|
||||
return $relativeDir . '/' . $fileName;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
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