Skip to content

Commit

Permalink
Merge branch 'spike/PAYSHIP-3046' into spike/capture-retry
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Oct 31, 2024
2 parents 059a4d3 + 87f1481 commit f3d39da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
32 changes: 15 additions & 17 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForOrderConfirmationQuery;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForOrderConfirmationQueryResult;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\ValueObject\PayPalOrderId;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalOrderProvider;
use PrestaShop\Module\PrestashopCheckout\Repository\PaymentTokenRepository;
use PrestaShop\Module\PrestashopCheckout\Repository\PayPalOrderRepository;

Expand All @@ -52,7 +51,7 @@ class Ps_CheckoutPaymentModuleFrontController extends AbstractFrontController

public function checkAccess()
{
return $this->context->customer && $this->context->customer->isLogged() && $this->context->cart;
return $this->context->customer && $this->context->cart;
}

public function initContent()
Expand Down Expand Up @@ -83,40 +82,34 @@ public function postProcess()

/** @var PayPalOrderRepository $payPalOrderRepository */
$payPalOrderRepository = $this->module->getService(PayPalOrderRepository::class);
/** @var PayPalOrderProvider $payPalOrderProvider */
$payPalOrderProvider = $this->module->getService(PayPalOrderProvider::class);
/** @var CommandBusInterface $commandBus */
$commandBus = $this->module->getService('ps_checkout.bus.command');
/** @var Psr\SimpleCache\CacheInterface $payPalOrderCache */
$payPalOrderCache = $this->module->getService('ps_checkout.cache.paypal.order');

$payPalOrder = $payPalOrderRepository->getPayPalOrderById($this->paypalOrderId);

$orders = new PrestaShopCollection(Order::class);
$orders->where('id_cart', '=', $payPalOrder->getIdCart());

if ($orders->count()) {
$this->redirectToOrderHistoryPage();
}

if ($payPalOrder->getIdCart() !== $this->context->cart->id) {
throw new Exception('PayPal order does not belong to this customer');
$this->redirectToOrderPage();
}

/** @var GetPayPalOrderForOrderConfirmationQueryResult $payPalOrderQueryResult */
$payPalOrderQueryResult = $commandBus->handle(new GetPayPalOrderForOrderConfirmationQuery($this->paypalOrderId->getValue()));
$payPalOrderFromCache = $payPalOrderQueryResult->getOrderPayPal();

if ($payPalOrderFromCache['status'] === 'COMPLETED') {
$orders = new PrestaShopCollection(Order::class);
$orders->where('id_cart', '=', $payPalOrder->getIdCart());

if (!$orders->count()) {
$this->createOrder($payPalOrderFromCache, $payPalOrder);
}

$this->createOrder($payPalOrderFromCache, $payPalOrder);
$this->redirectToOrderConfirmationPage($payPalOrder->getIdCart(), $payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0]['id'], $payPalOrderFromCache['status']);
}

if ($payPalOrderFromCache['status'] === 'PAYER_ACTION_REQUIRED') {
// Delete from cache so when user is redirected from 3DS authentication page the order is fetched from PayPal
if ($payPalOrderCache->has($this->paypalOrderId->getValue())) {
$payPalOrderCache->delete($this->paypalOrderId->getValue());
}

$this->redirectTo3DSVerification($payPalOrderFromCache);
}

Expand Down Expand Up @@ -235,4 +228,9 @@ private function redirectToOrderConfirmationPage($cartId, $captureId, $payPalOrd
));
}
}

private function redirectToOrderHistoryPage()
{
Tools::redirect($this->context->link->getPageLink('history'));
}
}
2 changes: 1 addition & 1 deletion src/PayPal/Order/PaypalOrderDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function isTokenSaved()

public function getPaymentTokenIdentifier()
{
if ($this->payPalOrder) {
if ($this->payPalOrder && isset($this->payPalOrder->getPaymentSource()[$this->payPalOrder->getFundingSource()])) {
$paymentSource = $this->payPalOrder->getPaymentSource()[$this->payPalOrder->getFundingSource()];

if ($this->payPalOrder->getFundingSource() === 'card') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForCheckoutCompletedQuery;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForCheckoutCompletedQueryResult;
use PrestaShop\Module\PrestashopCheckout\PaypalOrder;
use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository;
use PsCheckoutCart;
use Psr\SimpleCache\CacheInterface;

/**
Expand All @@ -40,28 +38,18 @@ class GetPayPalOrderForCheckoutCompletedQueryHandler
* @var CacheInterface
*/
private $orderPayPalCache;
/**
* @var PsCheckoutCartRepository
*/
private $psCheckoutCartRepository;

public function __construct(CacheInterface $orderPayPalCache, PsCheckoutCartRepository $psCheckoutCartRepository)
public function __construct(CacheInterface $orderPayPalCache)
{
$this->orderPayPalCache = $orderPayPalCache;
$this->psCheckoutCartRepository = $psCheckoutCartRepository;
}

public function handle(GetPayPalOrderForCheckoutCompletedQuery $getPayPalOrderQuery)
{
/** @var array{id: string, status: string} $order */
$order = $this->orderPayPalCache->get($getPayPalOrderQuery->getOrderPayPalId()->getValue());
$psCheckoutCart = $this->psCheckoutCartRepository->findOneByPayPalOrderId($getPayPalOrderQuery->getOrderPayPalId()->getValue());

$psCheckoutCartDateUpdated = new \DateTime($psCheckoutCart->date_upd);
$currentDateTime = new \DateTime();
$interval = $currentDateTime->getTimestamp() - $psCheckoutCartDateUpdated->getTimestamp();

if (!empty($order) && ($psCheckoutCart->paypal_status === $order['status'] && $interval < 30 || in_array($order['status'], [PsCheckoutCart::STATUS_COMPLETED, PsCheckoutCart::STATUS_CANCELED]))) {
if (!empty($order) && in_array($order['status'], ['COMPLETED', 'CANCELED'])) {
return new GetPayPalOrderForCheckoutCompletedQueryResult($order);
}

Expand Down

0 comments on commit f3d39da

Please sign in to comment.