From 84eec510b247b91621e952bb5253b5977cceea53 Mon Sep 17 00:00:00 2001 From: L3RAZ Date: Wed, 23 Oct 2024 17:19:36 +0300 Subject: [PATCH] Removed retries from MaaslandHttpClient and added custom ttl for PayPal orders by status --- config/cache.yml | 2 +- src/Http/MaaslandHttpClient.php | 13 ++------- src/PayPal/Order/Cache/PayPalOrderCache.php | 27 ++++++++++++++++++ src/PayPal/Order/Cache/index.php | 28 +++++++++++++++++++ ...lOrderForCheckoutCompletedQueryHandler.php | 2 +- 5 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 src/PayPal/Order/Cache/PayPalOrderCache.php create mode 100644 src/PayPal/Order/Cache/index.php diff --git a/config/cache.yml b/config/cache.yml index 397b22943..d5b28e93c 100644 --- a/config/cache.yml +++ b/config/cache.yml @@ -18,7 +18,7 @@ services: - '@=service("PrestaShop\\ModuleLibCacheDirectoryProvider\\Cache\\CacheDirectoryProvider").getPath()' ps_checkout.cache.paypal.order: - class: 'Symfony\Component\Cache\Simple\ChainCache' + class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\Cache\PayPalOrderCache' public: true arguments: - [ diff --git a/src/Http/MaaslandHttpClient.php b/src/Http/MaaslandHttpClient.php index f0d8f2682..632fd93f8 100644 --- a/src/Http/MaaslandHttpClient.php +++ b/src/Http/MaaslandHttpClient.php @@ -110,18 +110,9 @@ public function fetchOrder(array $payload, array $options = []) * @throws PayPalException * @throws HttpTimeoutException */ - public function captureOrder(array $payload, array $options = [], $maxRetries = 0, $backOff = 1) + public function captureOrder(array $payload, array $options = []) { - try { - return $this->sendRequest(new Request('POST', '/payments/order/capture', $options, json_encode($payload))); - } catch (HttpTimeoutException $exception) { - if ($maxRetries > 0) { - sleep($backOff); - return $this->captureOrder($payload, $options, $maxRetries - 1, $backOff * 2); - } - - throw $exception; - } + return $this->sendRequest(new Request('POST', '/payments/order/capture', $options, json_encode($payload))); } /** diff --git a/src/PayPal/Order/Cache/PayPalOrderCache.php b/src/PayPal/Order/Cache/PayPalOrderCache.php new file mode 100644 index 000000000..0a6be18b4 --- /dev/null +++ b/src/PayPal/Order/Cache/PayPalOrderCache.php @@ -0,0 +1,27 @@ + 600, + PsCheckoutCart::STATUS_PAYER_ACTION_REQUIRED => 600, + PsCheckoutCart::STATUS_APPROVED => 600, + PsCheckoutCart::STATUS_VOIDED => 3600, + PsCheckoutCart::STATUS_SAVED => 3600, + PsCheckoutCart::STATUS_CANCELED => 3600, + PsCheckoutCart::STATUS_COMPLETED => 3600, + ]; + public function set($key, $value, $ttl = null) + { + if (!$ttl && isset($value['status']) && isset(self::CACHE_TTL[$value['status']])) { + $ttl = self::CACHE_TTL[$value['status']]; + } + + return parent::set($key, $value, $ttl); + } +} diff --git a/src/PayPal/Order/Cache/index.php b/src/PayPal/Order/Cache/index.php new file mode 100644 index 000000000..296d682e8 --- /dev/null +++ b/src/PayPal/Order/Cache/index.php @@ -0,0 +1,28 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php b/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php index 6c4a439cc..5c5862591 100644 --- a/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php +++ b/src/PayPal/Order/QueryHandler/GetPayPalOrderForCheckoutCompletedQueryHandler.php @@ -49,7 +49,7 @@ public function handle(GetPayPalOrderForCheckoutCompletedQuery $getPayPalOrderQu /** @var array{id: string, status: string} $order */ $order = $this->orderPayPalCache->get($getPayPalOrderQuery->getOrderPayPalId()->getValue()); - if (!empty($order) && $order['status'] === 'APPROVED') { + if (!empty($order) && !in_array($order['status'], ['APPROVED', 'COMPLETED'])) { return new GetPayPalOrderForCheckoutCompletedQueryResult($order); }