From 6f3a43b2a7c288a8f999fac88e293acf72a61d23 Mon Sep 17 00:00:00 2001 From: sushmita Date: Wed, 5 Jun 2024 16:27:20 +0200 Subject: [PATCH] [ECP-9206-v3] Update Adyen order check logic by using order transaction instead of original psp-reference and create and end point for this check --- src/Controller/AdminController.php | 39 ++++++++++++++++++- .../src/component/adyen-refund/index.js | 16 +------- .../src/service/adyenService.js | 26 +++++++------ 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index 8effab36..b2e10e61 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -28,6 +28,7 @@ use Adyen\Shopware\Entity\AdyenPayment\AdyenPaymentEntity; use Adyen\Shopware\Entity\Notification\NotificationEntity; use Adyen\Shopware\Exception\CaptureException; +use Adyen\Shopware\Provider\AdyenPluginProvider; use Adyen\Shopware\Service\AdyenPaymentService; use Adyen\Shopware\Service\CaptureService; use Adyen\Shopware\Service\ConfigurationService; @@ -96,6 +97,9 @@ class AdminController /** @var OrderTransactionRepository */ private $orderTransactionRepository; + /** @var AdyenPluginProvider */ + private AdyenPluginProvider $pluginProvider; + /** * AdminController constructor. * @@ -111,6 +115,7 @@ class AdminController * @param ConfigurationService $configurationService * @param AdyenPaymentService $adyenPaymentService * @param OrderTransactionRepository $orderTransactionRepository + * @param AdyenPluginProvider $pluginProvider */ public function __construct( LoggerInterface $logger, @@ -124,7 +129,8 @@ public function __construct( Currency $currencyUtil, ConfigurationService $configurationService, AdyenPaymentService $adyenPaymentService, - OrderTransactionRepository $orderTransactionRepository + OrderTransactionRepository $orderTransactionRepository, + AdyenPluginProvider $pluginProvider ) { $this->logger = $logger; $this->orderRepository = $orderRepository; @@ -138,6 +144,7 @@ public function __construct( $this->configurationService = $configurationService; $this->adyenPaymentService = $adyenPaymentService; $this->orderTransactionRepository = $orderTransactionRepository; + $this->pluginProvider = $pluginProvider; } /** @@ -535,4 +542,34 @@ public function rescheduleNotification(string $notificationId): JsonResponse return new JsonResponse($notificationId); } + + /** + * + * @Route( + * '/api/adyen/orders/{orderId}/is-adyen-order', + * name: 'api.adyen_is_adyen_order.get', + * methods: ['GET'] + * ) + * + * @param string $orderId + * @return JsonResponse + */ + + public function isAdyenOrder(string $orderId): JsonResponse + { + try { + $transaction = $this->orderTransactionRepository->getFirstAdyenOrderTransaction( + $orderId, + Context::createDefaultContext() + ); + + if (!is_null($transaction)) { + return new JsonResponse(['status' => true]); + } + + return new JsonResponse(['status' => false]); + } catch (Throwable $t) { + return new JsonResponse(['message' => "Something went wrong."], 500); + } + } } diff --git a/src/Resources/app/administration/src/component/adyen-refund/index.js b/src/Resources/app/administration/src/component/adyen-refund/index.js index a898c127..dd46a0b2 100644 --- a/src/Resources/app/administration/src/component/adyen-refund/index.js +++ b/src/Resources/app/administration/src/component/adyen-refund/index.js @@ -117,25 +117,11 @@ Component.register('adyen-refund', { } this.allowRefund = this.order.amountTotal > (refundedAmount / 100); - }, - - isAdyenOrder() { - const orderTransactions = this.order.transactions; - let isAdyen = false; - for (let i = 0; i < orderTransactions.length; i++) { - if (orderTransactions[i].customFields !== undefined) { - if (orderTransactions[i].customFields.originalPspReference !== undefined) { - isAdyen = true; - } - } - } - - this.showWidget = isAdyen; } }, beforeMount() { - this.isAdyenOrder(); + this.showWidget = this.adyenService.isAdyenOrder(this.order); if (this.showWidget) { this.fetchRefunds(); } diff --git a/src/Resources/app/administration/src/service/adyenService.js b/src/Resources/app/administration/src/service/adyenService.js index 2ef5de2a..a4f7c539 100644 --- a/src/Resources/app/administration/src/service/adyenService.js +++ b/src/Resources/app/administration/src/service/adyenService.js @@ -160,17 +160,21 @@ class ApiClient extends ApiService { } isAdyenOrder(order) { - const orderTransactions = order.transactions; - let isAdyen = false; - for (let i = 0; i < orderTransactions.length; i++) { - if (orderTransactions[i].customFields !== undefined) { - if (orderTransactions[i].customFields.originalPspReference !== undefined) { - isAdyen = true; - } - } - } - - return isAdyen; + const headers = this.getBasicHeaders({}); + return this.httpClient + .get(this.getApiBasePath() + '/orders/' + order.id + '/is-adyen-order', { + headers + }) + .then((response) => { + return ApiService.handleResponse(response); + }) + .then((response) => { + return response.status + }) + .catch((error) => { + console.error('An error occurred: ' + error.message); + throw error; + }); } fetchAdyenPartialPayments(orderId) {