From 802d52bdc47886482dfc92eceb062f8e508e83e6 Mon Sep 17 00:00:00 2001 From: khushboos Date: Thu, 3 Oct 2024 13:56:23 +0200 Subject: [PATCH 1/3] Generate event dispatcher/observer mechanism for reliability events. Adding all the necessary requirements. --- Api/AdyenAnalyticsRepositoryInterface.php | 15 ++++ Api/Data/AdyenAnalyticsInterface.php | 41 ++++++++++ Model/AdyenAnalytics.php | 93 +++++++++++++++++++++++ Model/AdyenAnalyticsRepository.php | 48 ++++++++++++ Model/ResourceModel/AdyenAnalytics.php | 12 +++ Observer/DispatchAnalyticsEvent.php | 37 +++++++++ Observer/ProcessAnalyticsEvent.php | 54 +++++++++++++ etc/db_schema.xml | 14 ++++ etc/db_schema_whitelist.json | 13 ++++ etc/events.xml | 3 + 10 files changed, 330 insertions(+) create mode 100644 Api/AdyenAnalyticsRepositoryInterface.php create mode 100644 Api/Data/AdyenAnalyticsInterface.php create mode 100644 Model/AdyenAnalytics.php create mode 100644 Model/AdyenAnalyticsRepository.php create mode 100644 Model/ResourceModel/AdyenAnalytics.php create mode 100644 Observer/DispatchAnalyticsEvent.php create mode 100644 Observer/ProcessAnalyticsEvent.php diff --git a/Api/AdyenAnalyticsRepositoryInterface.php b/Api/AdyenAnalyticsRepositoryInterface.php new file mode 100644 index 0000000000..d19d16887e --- /dev/null +++ b/Api/AdyenAnalyticsRepositoryInterface.php @@ -0,0 +1,15 @@ +_init('Vendor\Module\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); + } +} diff --git a/Model/AdyenAnalyticsRepository.php b/Model/AdyenAnalyticsRepository.php new file mode 100644 index 0000000000..9ca4758e7b --- /dev/null +++ b/Model/AdyenAnalyticsRepository.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/Model/ResourceModel/AdyenAnalytics.php b/Model/ResourceModel/AdyenAnalytics.php new file mode 100644 index 0000000000..8db77d250b --- /dev/null +++ b/Model/ResourceModel/AdyenAnalytics.php @@ -0,0 +1,12 @@ +_init('adyen_analytics', 'id'); // Table name and primary key + } +} diff --git a/Observer/DispatchAnalyticsEvent.php b/Observer/DispatchAnalyticsEvent.php new file mode 100644 index 0000000000..ff5ddafd65 --- /dev/null +++ b/Observer/DispatchAnalyticsEvent.php @@ -0,0 +1,37 @@ +eventManager = $eventManager; + $this->adyenAnalyticsRepository = $adyenAnalyticsRepository; + } + + 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]); + } +} diff --git a/Observer/ProcessAnalyticsEvent.php b/Observer/ProcessAnalyticsEvent.php new file mode 100644 index 0000000000..02b2a8a2a8 --- /dev/null +++ b/Observer/ProcessAnalyticsEvent.php @@ -0,0 +1,54 @@ +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()); + } + } +} diff --git a/etc/db_schema.xml b/etc/db_schema.xml index b27cee6c35..da51b73018 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -123,4 +123,18 @@ + + + + + + + + + + + + + +
diff --git a/etc/db_schema_whitelist.json b/etc/db_schema_whitelist.json index 8be925e25e..ccd42aa095 100644 --- a/etc/db_schema_whitelist.json +++ b/etc/db_schema_whitelist.json @@ -109,5 +109,18 @@ "constraint": { "PRIMARY": true } + }, + "adyen_analytics": { + "columns": { + "id": {}, + "checkoutAttemptId": {}, + "eventType": {}, + "topic": {}, + "message": {}, + "errorCount": {}, + "done": {}, + "createdAt": {}, + "updatedAt": {} + } } } \ No newline at end of file diff --git a/etc/events.xml b/etc/events.xml index 0a6acaf769..ffad88c6a8 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -302,4 +302,7 @@ + + + \ No newline at end of file From c5a740a7fc58560496605700ba2dad11100a961b Mon Sep 17 00:00:00 2001 From: khushboos Date: Tue, 8 Oct 2024 10:22:38 +0200 Subject: [PATCH 2/3] Adding dispatcher class to test the DB entry and if it works --- Observer/DispatchAnalyticsEvent.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Observer/DispatchAnalyticsEvent.php b/Observer/DispatchAnalyticsEvent.php index ff5ddafd65..de01f118f1 100644 --- a/Observer/DispatchAnalyticsEvent.php +++ b/Observer/DispatchAnalyticsEvent.php @@ -5,18 +5,22 @@ 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 + AdyenAnalyticsRepositoryInterface $adyenAnalyticsRepository, + AdyenAnalyticsInterfaceFactory $analyticsFactory ) { $this->eventManager = $eventManager; $this->adyenAnalyticsRepository = $adyenAnalyticsRepository; + $this->analyticsFactory = $analyticsFactory; } public function execute(Observer $observer) @@ -33,5 +37,19 @@ public function execute(Observer $observer) // 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); } } From 52e2cbc760380c8379a1704d1c0caa2e9722e6a4 Mon Sep 17 00:00:00 2001 From: khushboos Date: Tue, 8 Oct 2024 10:23:45 +0200 Subject: [PATCH 3/3] Adding dispatcher class to test the DB entry and if it works --- Model/AdyenAnalytics.php | 2 +- Model/ResourceModel/AdyenAnalytics.php | 2 +- etc/di.xml | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Model/AdyenAnalytics.php b/Model/AdyenAnalytics.php index dcb891dc44..b5b52c8aec 100644 --- a/Model/AdyenAnalytics.php +++ b/Model/AdyenAnalytics.php @@ -8,7 +8,7 @@ class AdyenAnalytics extends AbstractModel implements AdyenAnalyticsInterface { protected function _construct() { - $this->_init('Vendor\Module\Model\ResourceModel\AdyenAnalytics'); + $this->_init('Adyen\Payment\Model\ResourceModel\AdyenAnalytics'); } public function getCheckoutAttemptId() diff --git a/Model/ResourceModel/AdyenAnalytics.php b/Model/ResourceModel/AdyenAnalytics.php index 8db77d250b..76a1068cec 100644 --- a/Model/ResourceModel/AdyenAnalytics.php +++ b/Model/ResourceModel/AdyenAnalytics.php @@ -7,6 +7,6 @@ class AdyenAnalytics extends AbstractDb { protected function _construct() { - $this->_init('adyen_analytics', 'id'); // Table name and primary key + $this->_init('adyen_analytics', 'id'); } } diff --git a/etc/di.xml b/etc/di.xml index 7b4cdc5532..2182e1e5ed 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -1671,6 +1671,13 @@ + + + Adyen\Payment\Model\AdyenAnalytics + + + +