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

[ECP-9414] Generate event dispatcher/observer mechanism for reliability events #2765

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions Api/AdyenAnalyticsRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Adyen\Payment\Api;

use Adyen\Payment\Api\Data\AdyenAnalyticsInterface;

interface AdyenAnalyticsRepositoryInterface
{
public function save(AdyenAnalyticsInterface $analytics);

public function getById($id);

public function delete(AdyenAnalyticsInterface $analytics);

public function deleteById($id);
}
41 changes: 41 additions & 0 deletions Api/Data/AdyenAnalyticsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Adyen\Payment\Api\Data;

interface AdyenAnalyticsInterface
{
const ID = 'id';
const CHECKOUT_ATTEMPT_ID = 'checkoutAttemptId';
const EVENT_TYPE = 'eventType';
const TOPIC = 'topic';
const MESSAGE = 'message';
const ERROR_COUNT = 'errorCount';
const DONE = 'done';
const CREATED_AT = 'createdAt';
const UPDATED_AT = 'updatedAt';

public function getId();

public function getCheckoutAttemptId();
public function setCheckoutAttemptId($checkoutAttemptId);

public function getEventType();
public function setEventType($eventType);

public function getTopic();
public function setTopic($topic);

public function getMessage();
public function setMessage($message);

public function getErrorCount();
public function setErrorCount($errorCount);

public function getDone();
public function setDone($done);

public function getCreatedAt();
public function setCreatedAt($createdAt);

public function getUpdatedAt();
public function setUpdatedAt($updatedAt);
}
93 changes: 93 additions & 0 deletions Model/AdyenAnalytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace Adyen\Payment\Model;

use Adyen\Payment\Api\Data\AdyenAnalyticsInterface;
use Magento\Framework\Model\AbstractModel;

class AdyenAnalytics extends AbstractModel implements AdyenAnalyticsInterface
{
protected function _construct()
{
$this->_init('Adyen\Payment\Model\ResourceModel\AdyenAnalytics');
}

public function getCheckoutAttemptId()
{
return $this->getData('checkoutAttemptId');
}

public function setCheckoutAttemptId($checkoutAttemptId)
{
return $this->setData('checkoutAttemptId', $checkoutAttemptId);
}

public function getEventType()
{
return $this->getData('eventType');
}

public function setEventType($eventType)
{
return $this->setData('eventType', $eventType);
}

public function getTopic()
{
return $this->getData('topic');
}

public function setTopic($topic)
{
return $this->setData('topic', $topic);
}

public function getMessage()
{
return $this->getData('message');
}

public function setMessage($message)
{
return $this->setData('message', $message);
}

public function getErrorCount()
{
return $this->getData('errorCount');
}

public function setErrorCount($errorCount)
{
return $this->setData('errorCount', $errorCount);
}

public function getDone()
{
return $this->getData('done');
}

public function setDone($done)
{
return $this->setData('done', $done);
}

public function getCreatedAt()
{
return $this->getData('createdAt');
}

public function setCreatedAt($createdAt)
{
return $this->setData('createdAt', $createdAt);
}

public function getUpdatedAt()
{
return $this->getData('updatedAt');
}

public function setUpdatedAt($updatedAt)
{
return $this->setData('updatedAt', $updatedAt);
}
}
48 changes: 48 additions & 0 deletions Model/AdyenAnalyticsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Adyen\Payment\Model;

use Adyen\Payment\Api\AdyenAnalyticsRepositoryInterface;
use Adyen\Payment\Api\Data\AdyenAnalyticsInterface;
use Adyen\Payment\Model\ResourceModel\AdyenAnalytics as ResourceModel;
use Adyen\Payment\Model\AdyenAnalyticsFactory;
use Magento\Framework\Exception\NoSuchEntityException;

class AdyenAnalyticsRepository implements AdyenAnalyticsRepositoryInterface
{
protected ResourceModel $resourceModel;
protected \Adyen\Payment\Model\AdyenAnalyticsFactory $analyticsFactory;

public function __construct(
ResourceModel $resourceModel,
AdyenAnalyticsFactory $analyticsFactory
) {
$this->resourceModel = $resourceModel;
$this->analyticsFactory = $analyticsFactory;
}

public function save(AdyenAnalyticsInterface $analytics)
{
$this->resourceModel->save($analytics);
}

public function getById($id)
{
$analytics = $this->analyticsFactory->create();
$this->resourceModel->load($analytics, $id);
if (!$analytics->getId()) {
throw new NoSuchEntityException(__('Unable to find analytics with ID "%1"', $id));
}
return $analytics;
}

public function delete(AdyenAnalyticsInterface $analytics)
{
$this->resourceModel->delete($analytics);
}

public function deleteById($id)
{
$analytics = $this->getById($id);
$this->delete($analytics);
}
}
12 changes: 12 additions & 0 deletions Model/ResourceModel/AdyenAnalytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Adyen\Payment\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class AdyenAnalytics extends AbstractDb
{
protected function _construct()
{
$this->_init('adyen_analytics', 'id');
}
}
55 changes: 55 additions & 0 deletions Observer/DispatchAnalyticsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Adyen\Payment\Observer;

use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Adyen\Payment\Api\AdyenAnalyticsRepositoryInterface;
use Adyen\Payment\Api\Data\AdyenAnalyticsInterfaceFactory;

class DispatchAnalyticsEvent implements ObserverInterface
{
protected ManagerInterface $eventManager;
protected AdyenAnalyticsRepositoryInterface $adyenAnalyticsRepository;
protected AdyenAnalyticsInterfaceFactory $analyticsFactory;

public function __construct(
ManagerInterface $eventManager,
AdyenAnalyticsRepositoryInterface $adyenAnalyticsRepository,
AdyenAnalyticsInterfaceFactory $analyticsFactory
) {
$this->eventManager = $eventManager;
$this->adyenAnalyticsRepository = $adyenAnalyticsRepository;
$this->analyticsFactory = $analyticsFactory;
}

public function execute(Observer $observer)
{
// Sample data for dispatching the event
$eventData = [
'checkoutAttemptId' => '12345',
'eventType' => 'payment_attempt',
'topic' => 'payment_method_adyen_analytics',
'message' => 'Sample payment analytics message.',
'errorCount' => 0,
'done' => false,
];

// Dispatch the event
$this->eventManager->dispatch('payment_method_adyen_analytics', ['data' => $eventData]);

// Create an instance of AdyenAnalyticsInterface
$analytics = $this->analyticsFactory->create();

// Set data to the analytics object
$analytics->setCheckoutAttemptId($eventData['checkoutAttemptId']);
$analytics->setEventType($eventData['eventType']);
$analytics->setTopic($eventData['topic']);
$analytics->setMessage($eventData['message']);
$analytics->setErrorCount($eventData['errorCount']);
$analytics->setDone($eventData['done']);

// Save the analytics data to the database
$this->adyenAnalyticsRepository->save($analytics);
}
}
54 changes: 54 additions & 0 deletions Observer/ProcessAnalyticsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Adyen\Payment\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Adyen\Payment\Api\AdyenAnalyticsRepositoryInterface;
use Adyen\Payment\Api\Data\AdyenAnalyticsInterfaceFactory;
use Psr\Log\LoggerInterface;

class ProcessAnalyticsEvent implements ObserverInterface
{
protected AdyenAnalyticsRepositoryInterface $adyenAnalyticsRepository;
protected AdyenAnalyticsInterfaceFactory $adyenAnalyticsFactory;
protected $logger;

public function __construct(
AdyenAnalyticsRepositoryInterface $adyenAnalyticsRepository,
AdyenAnalyticsInterfaceFactory $adyenAnalyticsFactory,
LoggerInterface $logger
) {
$this->adyenAnalyticsRepository = $adyenAnalyticsRepository;
$this->adyenAnalyticsFactory = $adyenAnalyticsFactory;
$this->logger = $logger;
}

public function execute(Observer $observer)
{
try {
// Get the event data
$eventData = $observer->getEvent()->getData('data');

// Log the event data for debugging (optional)
$this->logger->info('Received event data for payment_method_adyen_analytics: ', $eventData);

// Create a new instance of the AdyenAnalytics model
$analytics = $this->adyenAnalyticsFactory->create();

// Populate the model with event data
$analytics->setCheckoutAttemptId($eventData['checkoutAttemptId']);
$analytics->setEventType($eventData['eventType']);
$analytics->setTopic($eventData['topic']);
$analytics->setMessage($eventData['message']);
$analytics->setErrorCount($eventData['errorCount']);
$analytics->setDone($eventData['done']);

// Save the event data to the database
$this->adyenAnalyticsRepository->save($analytics);

} catch (\Exception $e) {
// Log any exceptions
$this->logger->error('Error processing payment_method_adyen_analytics event: ' . $e->getMessage());
}
}
}
14 changes: 14 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,18 @@
</constraint>
<constraint xsi:type="foreign" referenceId="ADYEN_CREDITMEMO_ADYEN_ORDER_PAYMENT_ID_ADYEN_ORDER_PAYMENT_ENTITY_ID" table="adyen_creditmemo" column="adyen_order_payment_id" referenceTable="adyen_order_payment" referenceColumn="entity_id" onDelete="CASCADE"/>
</table>
<table name="adyen_analytics" resource="default" engine="innodb" comment="Adyen Analytics Event Queue">
<column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/>
<column xsi:type="varchar" name="checkoutAttemptId" length="255" nullable="false" comment="Checkout Attempt ID"/>
<column xsi:type="varchar" name="eventType" length="255" nullable="false" comment="Event Type"/>
<column xsi:type="varchar" name="topic" length="255" nullable="false" comment="Event Topic"/>
<column xsi:type="text" name="message" nullable="false" comment="Message"/>
<column xsi:type="int" name="errorCount" nullable="false" default="0" comment="Error Count"/>
<column xsi:type="boolean" name="done" nullable="false" default="0" comment="Done"/>
<column xsi:type="timestamp" name="createdAt" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
<column xsi:type="timestamp" name="updatedAt" nullable="true" on_update="true" comment="Updated At"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="id"/>
</constraint>
</table>
</schema>
13 changes: 13 additions & 0 deletions etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,18 @@
"constraint": {
"PRIMARY": true
}
},
"adyen_analytics": {
"columns": {
"id": {},
"checkoutAttemptId": {},
"eventType": {},
"topic": {},
"message": {},
"errorCount": {},
"done": {},
"createdAt": {},
"updatedAt": {}
}
}
}
7 changes: 7 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,13 @@
</argument>
</arguments>
</type>
<type name="Adyen\Payment\Api\Data\AdyenAnalyticsInterface">
<arguments>
<argument name="instance" xsi:type="string">Adyen\Payment\Model\AdyenAnalytics</argument>
</arguments>
</type>
<preference for="Adyen\Payment\Api\Data\AdyenAnalyticsInterface" type="Adyen\Payment\Model\AdyenAnalytics"/>
<preference for="Adyen\Payment\Api\AdyenAnalyticsRepositoryInterface" type="Adyen\Payment\Model\AdyenAnalyticsRepository"/>
<preference for="Adyen\Payment\Api\GuestAdyenPaymentMethodManagementInterface" type="Adyen\Payment\Model\Api\GuestAdyenPaymentMethodManagement" />
<preference for="Adyen\Payment\Api\AdyenPaymentMethodManagementInterface" type="Adyen\Payment\Model\Api\AdyenPaymentMethodManagement" />
<preference for="Adyen\Payment\Api\AdyenPaymentsDetailsInterface" type="Adyen\Payment\Model\Api\AdyenPaymentsDetails" />
Expand Down
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@
<event name="payment_method_assign_data_adyen_dana">
<observer name="adyen_dana_gateway_data_assign" instance="Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver" />
</event>
<event name="payment_method_adyen_analytics">
<observer name="adyen_analytics_observer" instance="Adyen\Payment\Observer\ProcessAnalyticsEvent" />
</event>
<event name="payment_method_assign_data_adyen_kcp_naverpay">
<observer name="adyen_kcp_naverpay_gateway_data_assign" instance="Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver" />
</event>
Expand Down
Loading