Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added build gateway request event #42

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Omnipay integration package for Craft Commerce

## Unreleased

- It’s now possible to modify gateway requests before they are sent.
- Added `craft\commerce\omnipay\base\Gateway::EVENT_BUILD_GATEWAY_REQUEST`.
- Added `craft\commerce\omnipay\events\BuildGatewayRequestEvent`.

## 4.1.0 - 2024-03-14

- Added Craft CMS 5 and Craft Commerce 5 compatibility.
Expand Down
34 changes: 33 additions & 1 deletion src/base/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\commerce\models\payments\CreditCardPaymentForm;
use craft\commerce\models\PaymentSource;
use craft\commerce\models\Transaction;
use craft\commerce\omnipay\events\BuildGatewayRequestEvent;
use craft\commerce\omnipay\events\GatewayRequestEvent;
use craft\commerce\omnipay\events\ItemBagEvent;
use craft\commerce\omnipay\events\SendPaymentRequestEvent;
Expand Down Expand Up @@ -105,6 +106,29 @@ abstract class Gateway extends BaseGateway
*/
public const EVENT_BEFORE_SEND_PAYMENT_REQUEST = 'beforeSendPaymentRequest';

/**
* @event BuildGatewayRequestEvent The event that is triggered when a gateway request is being built.
* @since 4.2.0
*
* Plugins get a chance to provide additional data to any request that is made in the context of paying for an order.
*
* There are some restrictions:
* Changes to the `Transaction` model available as the `transaction` property will be ignored;
*
* ```php
* use craft\commerce\omnipay\base\Gateway;
* use craft\commerce\omnipay\events\BuildGatewayRequestEvent;
* use yii\base\Event;
*
* Event::on(Gateway::class, Gateway::EVENT_BUILD_GATEWAY_REQUEST, function(BuildGatewayRequestEvent $e) {
* if ($e->transaction->type === 'purchase') {
* $e->request['someKey'] = 'some value';
* }
* });
* ```
*/
public const EVENT_BUILD_GATEWAY_REQUEST = 'buildGatewayRequest';

/**
* @var bool|string Whether cart information should be sent to the payment gateway
*/
Expand Down Expand Up @@ -611,7 +635,15 @@ protected function createRequest(Transaction $transaction, ?BasePaymentForm $for
$this->populateRequest($request, $form);
}

return $request;
$event = new BuildGatewayRequestEvent([
'transaction' => $transaction,
'request' => $request,
'type' => $transaction->type,
]);

$this->trigger(self::EVENT_BUILD_GATEWAY_REQUEST, $event);

return $event->request;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/events/BuildGatewayRequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\commerce\omnipay\events;

use craft\commerce\models\Transaction;
use yii\base\Event;

/**
* Class BuildGatewayRequestEvent
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.2.0
*/
class BuildGatewayRequestEvent extends Event
{
/**
* @var Transaction The transaction being used as the base for request
*/
public Transaction $transaction;

/**
* @var array The request being used
*/
public array $request;

/**
* @var string|null The type of request being made
*/
public ?string $type = null;
}
5 changes: 5 additions & 0 deletions src/events/GatewayRequestEvent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\commerce\omnipay\events;

Expand Down
Loading