Skip to content

Commit

Permalink
Removed retries from MaaslandHttpClient and added custom ttl for PayP…
Browse files Browse the repository at this point in the history
…al orders by status
  • Loading branch information
L3RAZ committed Oct 23, 2024
1 parent ac77090 commit 84eec51
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
- [
Expand Down
13 changes: 2 additions & 11 deletions src/Http/MaaslandHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/PayPal/Order/Cache/PayPalOrderCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\Cache;

use PsCheckoutCart;
use Symfony\Component\Cache\Simple\ChainCache;

class PayPalOrderCache extends ChainCache
{
const CACHE_TTL = [
PsCheckoutCart::STATUS_CREATED => 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);
}
}
28 changes: 28 additions & 0 deletions src/PayPal/Order/Cache/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 84eec51

Please sign in to comment.