From d4773eee1f6b65598160f62cea86fe0d1ae4a0db Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Thu, 6 Feb 2025 16:13:30 +0800 Subject: [PATCH 1/2] New gateway events --- src/events/GatewayEvent.php | 30 ++++++++++++++++ src/services/Gateways.php | 68 ++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/events/GatewayEvent.php diff --git a/src/events/GatewayEvent.php b/src/events/GatewayEvent.php new file mode 100644 index 0000000000..57461603a2 --- /dev/null +++ b/src/events/GatewayEvent.php @@ -0,0 +1,30 @@ + + * @since 5.4.0 + */ +class GatewayEvent extends Event +{ + /** + * @var Gateway The gateway model associated with the event. + */ + public Gateway $pdf; + + /** + * @var bool Whether the Gateway is brand new + */ + public bool $isNew = false; +} diff --git a/src/services/Gateways.php b/src/services/Gateways.php index 6f8fbcea4b..6fea64b071 100644 --- a/src/services/Gateways.php +++ b/src/services/Gateways.php @@ -12,6 +12,7 @@ use craft\commerce\base\GatewayInterface; use craft\commerce\base\SubscriptionGateway; use craft\commerce\db\Table; +use craft\commerce\events\GatewayEvent; use craft\commerce\gateways\Dummy; use craft\commerce\gateways\Manual; use craft\commerce\gateways\MissingGateway; @@ -30,6 +31,7 @@ use Throwable; use yii\base\Component; use yii\base\ErrorException; +use yii\base\Event; use yii\base\Exception; use yii\base\InvalidConfigException; use yii\base\NotSupportedException; @@ -48,6 +50,56 @@ */ class Gateways extends Component { + /** + * @event GatewayEvent The event that is triggered before a gateway is saved. + * + * ```php + * use craft\commerce\events\GatewayEvent; + * use craft\commerce\services\Gateways; + * use craft\commerce\models\Gateway; + * use yii\base\Event; + * + * Event::on( + * Gateways::class, + * Gateways::EVENT_BEFORE_SAVE_GATEWAY, + * function(GatewayEvent $event) { + * // @var Gateway $gateway + * $gateway = $event->gateway; + * // @var bool $isNew + * $isNew = $event->isNew; + * + * // ... + * } + * ); + * ``` + */ + public const EVENT_BEFORE_SAVE_GATEWAY = 'beforeSaveGateway'; + + /** + * @event GatewayEvent The event that is triggered after a gateway is saved. + * + * ```php + * use craft\commerce\events\GatewayEvent; + * use craft\commerce\services\Gateways; + * use craft\commerce\models\Gateway; + * use yii\base\Event; + * + * Event::on( + * Gateways::class, + * Gateways::EVENT_AFTER_SAVE_GATEWAY, + * function(GatewayEvent $event) { + * // @var Gateway $gateway + * $gateway = $event->gateway; + * // @var bool $isNew + * $isNew = $event->isNew; + * + * // ... + * } + * ); + * ``` + */ + public const EVENT_AFTER_SAVE_GATEWAY = 'afterSaveGateway'; + /** * @var array|null Gateway setting overrides */ @@ -235,6 +287,13 @@ public function saveGateway(Gateway $gateway, bool $runValidation = true): bool { $isNewGateway = $gateway->getIsNew(); + if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_GATEWAY)) { + $this->trigger(self::EVENT_BEFORE_SAVE_GATEWAY, new GatewayEvent([ + 'gateway' => $gateway, + 'isNew' => $isNewGateway, + ])); + } + if ($runValidation && !$gateway->validate()) { Craft::info('Gateway not saved due to validation error.', __METHOD__); return false; @@ -294,7 +353,7 @@ public function handleChangedGateway(ConfigEvent $event): void $transaction = Craft::$app->getDb()->beginTransaction(); try { $gatewayRecord = $this->_getGatewayRecord($gatewayUid); - + $isNewGateway = $gatewayRecord->getIsNewRecord(); $gatewayRecord->name = $data['name']; $gatewayRecord->handle = $data['handle']; $gatewayRecord->type = $data['type']; @@ -318,6 +377,13 @@ public function handleChangedGateway(ConfigEvent $event): void $transaction->rollBack(); throw $e; } + + if ($this->hasEventHandlers(self::EVENT_AFTER_SAVE_GATEWAY)) { + $this->trigger(self::EVENT_AFTER_SAVE_GATEWAY, new GatewayEvent([ + 'gateway' => $this->getGatewayById($gatewayRecord->id), + 'isNew' => $isNewGateway, + ])); + } } /** From 3bbaed53c108f58afa64b2e06441cf32316d4340 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Thu, 6 Feb 2025 16:17:32 +0800 Subject: [PATCH 2/2] Release notes --- CHANGELOG-WIP.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index ba35cdee61..0c5aafb440 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -1 +1,7 @@ -# Release Notes for Craft Commerce 5.4 (WIP) \ No newline at end of file +# Release Notes for Craft Commerce 5.4 (WIP) + +### Extensibility + +- Added `craft\commerce\services\Gateways::EVENT_AFTER_SAVE_GATEWAY`. +- Added `craft\commerce\services\Gateways::EVENT_BEFORE_SAVE_GATEWAY`. +- Added `craft\commerce\events\GatewayEvent`.