diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 6f5ac6d1f7..40ace48333 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -22,8 +22,10 @@ - Added `craft\commerce\models\ShippingMethod::setCustomerCondition()`. - Added `craft\commerce\models\ShippingRule::getCustomerCondition()`. - Added `craft\commerce\models\ShippingRule::setCustomerCondition()`. +- Added `craft\commerce\services\Gateways::EVENT_AFTER_SAVE_GATEWAY`. +- Added `craft\commerce\services\Gateways::EVENT_BEFORE_SAVE_GATEWAY`. +- Added `craft\commerce\events\GatewayEvent`. ### System - Added the `resave/variants` command. - Fixed a bug where gateway settings weren’t storing project config values consistently. ([#3941](https://github.com/craftcms/commerce/issues/3941)) - 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 40c3bb0270..6883e3dfca 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; @@ -47,6 +49,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 */ @@ -234,6 +286,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; @@ -285,7 +344,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']; @@ -310,6 +369,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, + ])); + } } /**