Skip to content

Commit 67e5f16

Browse files
committed
Added latest features from PS8
1 parent 4e26106 commit 67e5f16

32 files changed

+350
-177
lines changed

config/cache.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- '@=service("PrestaShop\\ModuleLibCacheDirectoryProvider\\Cache\\CacheDirectoryProvider").getPath()'
1919

2020
ps_checkout.cache.paypal.order:
21-
class: 'Symfony\Component\Cache\Adapter\ChainAdapter'
21+
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\Cache\PayPalOrderCache'
2222
public: true
2323
arguments:
2424
- [

config/common.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ services:
269269
- '@PrestaShop\Module\PrestashopCheckout\Context\PrestaShopContext'
270270
- '@ps_checkout.logger'
271271
- '@PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceEligibilityConstraint'
272-
- '@PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\PayPalCustomerIdProvider'
272+
- '@ps_checkout.bus.query'
273273

274274
PrestaShop\Module\PrestashopCheckout\Presenter\Store\Modules\PaypalModule:
275275
class: 'PrestaShop\Module\PrestashopCheckout\Presenter\Store\Modules\PaypalModule'
@@ -518,11 +518,3 @@ services:
518518
public: true
519519
arguments:
520520
- '@PrestaShop\Module\PrestashopCheckout\Translations\Translations'
521-
522-
PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\PayPalCustomerIdProvider:
523-
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\PayPalCustomerIdProvider'
524-
public: true
525-
arguments:
526-
- '@PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\OAuthService'
527-
- '@PrestaShop\Module\PrestashopCheckout\Repository\PayPalCustomerRepository'
528-
- '@PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration'

controllers/front/applepay.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartException;
2222
use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId;
2323
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
24+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2425
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2526
use PrestaShop\Module\PrestashopCheckout\PayPal\ApplePay\Query\GetApplePayPaymentRequestQuery;
2627
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;
@@ -37,7 +38,7 @@ class Ps_CheckoutApplepayModuleFrontController extends AbstractFrontController
3738

3839
private CommandBusInterface $commandBus;
3940

40-
private CommandBusInterface $queryBus;
41+
private QueryBusInterface $queryBus;
4142

4243
/**
4344
* @see FrontController::postProcess()
@@ -61,6 +62,7 @@ public function postProcess()
6162
}
6263

6364
$this->commandBus = $this->module->getService('ps_checkout.bus.command');
65+
$this->queryBus = $this->module->getService('ps_checkout.bus.query');
6466

6567
switch ($action) {
6668
case 'getPaymentRequest':

controllers/front/create.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException;
2323
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
24+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2425
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2526
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CreatePayPalOrderCommand;
2627
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForCartIdQuery;
@@ -129,7 +130,7 @@ public function postProcess()
129130
/** @var CommandBusInterface $commandBus */
130131
$commandBus = $this->module->getService('ps_checkout.bus.command');
131132

132-
/** @var CommandBusInterface $queryBus */
133+
/** @var QueryBusInterface $queryBus */
133134
$queryBus = $this->module->getService('ps_checkout.bus.query');
134135

135136
$commandBus->handle(new CreatePayPalOrderCommand($cartId, $fundingSource, $isCardFields, $isExpressCheckout, $vaultId, $favorite, $vault));

controllers/front/googlepay.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId;
22-
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
22+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2323
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2424
use PrestaShop\Module\PrestashopCheckout\PayPal\GooglePay\Query\GetGooglePayTransactionInfoQuery;
2525

@@ -33,7 +33,7 @@ class Ps_CheckoutGooglepayModuleFrontController extends AbstractFrontController
3333
*/
3434
public $module;
3535

36-
private CommandBusInterface $queryBus;
36+
private QueryBusInterface $queryBus;
3737

3838
/**
3939
* @see FrontController::postProcess()

controllers/front/payment.php

+59-26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
22+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2223
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2324
use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException;
2425
use PrestaShop\Module\PrestashopCheckout\Order\Command\CreateOrderCommand;
@@ -48,9 +49,12 @@ class Ps_CheckoutPaymentModuleFrontController extends AbstractFrontController
4849
*/
4950
private $paypalOrderId;
5051

52+
private CommandBusInterface $commandBus;
53+
private QueryBusInterface $queryBus;
54+
5155
public function checkAccess()
5256
{
53-
return $this->context->customer && $this->context->customer->isLogged() && $this->context->cart;
57+
return $this->context->customer && $this->context->cart;
5458
}
5559

5660
public function initContent()
@@ -79,33 +83,42 @@ public function postProcess()
7983
try {
8084
$this->paypalOrderId = new PayPalOrderId($orderId);
8185

86+
$this->commandBus = $this->module->getService('ps_checkout.bus.command');
87+
$this->queryBus = $this->module->getService('ps_checkout.bus.query');
88+
8289
/** @var PayPalOrderRepository $payPalOrderRepository */
8390
$payPalOrderRepository = $this->module->getService(PayPalOrderRepository::class);
84-
/** @var PayPalOrderProvider $payPalOrderProvider */
85-
$payPalOrderProvider = $this->module->getService(PayPalOrderProvider::class);
86-
/** @var CommandBusInterface $commandBus */
87-
$commandBus = $this->module->getService('ps_checkout.bus.command');
8891
/** @var Symfony\Contracts\Cache\CacheInterface $payPalOrderCache */
8992
$payPalOrderCache = $this->module->getService('ps_checkout.cache.paypal.order');
9093

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

96+
$orders = new PrestaShopCollection(Order::class);
97+
$orders->where('id_cart', '=', $payPalOrder->getIdCart());
98+
99+
if ($orders->count()) {
100+
if ($this->context->customer->isLogged()) {
101+
$this->redirectToOrderHistoryPage();
102+
} else {
103+
$payPalOrderQueryResult = $this->getPayPalOrder($orderId);
104+
$payPalOrderFromCache = $payPalOrderQueryResult->getPayPalOrder();
105+
106+
$this->redirectToOrderConfirmationPage($payPalOrder->getIdCart(), $payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0]['id'], $payPalOrderFromCache['status']);
107+
}
108+
}
109+
93110
if ($payPalOrder->getIdCart() !== $this->context->cart->id) {
94-
throw new Exception('PayPal order does not belong to this customer');
111+
$this->redirectToOrderPage();
95112
}
96113

97-
$payPalOrderFromCache = $payPalOrderProvider->getById($payPalOrder->getId()->getValue());
114+
$payPalOrderQueryResult = $this->getPayPalOrder($orderId);
115+
$payPalOrderFromCache = $payPalOrderQueryResult->getPayPalOrder();
98116

99117
if ($payPalOrderFromCache['status'] === 'COMPLETED') {
100118
$this->createOrder($payPalOrderFromCache, $payPalOrder);
101119
}
102120

103121
if ($payPalOrderFromCache['status'] === 'PAYER_ACTION_REQUIRED') {
104-
// Delete from cache so when user is redirected from 3DS authentication page the order is fetched from PayPal
105-
if ($payPalOrderCache->hasItem($this->paypalOrderId->getValue())) {
106-
$payPalOrderCache->delete($this->paypalOrderId->getValue());
107-
}
108-
109122
$this->redirectTo3DSVerification($payPalOrderFromCache);
110123
}
111124

@@ -117,15 +130,21 @@ public function postProcess()
117130
$this->redirectTo3DSVerification($payPalOrderFromCache);
118131
break;
119132
case Card3DSecure::PROCEED:
120-
$commandBus->handle(new CapturePayPalOrderCommand($this->paypalOrderId->getValue(), array_keys($payPalOrderFromCache['payment_source'])[0]));
121-
$payPalOrderFromCache = $payPalOrderCache->get($this->paypalOrderId->getValue());
133+
$this->commandBus->handle(new CapturePayPalOrderCommand($orderId, array_keys($payPalOrderFromCache['payment_source'])[0]));
134+
$payPalOrderFromCache = $payPalOrderCache->get($orderId);
122135
$this->createOrder($payPalOrderFromCache, $payPalOrder);
123136
break;
124137
case Card3DSecure::NO_DECISION:
125138
default:
126139
break;
127140
}
128141
}
142+
143+
if ($payPalOrderFromCache['status'] === 'APPROVED') {
144+
$this->commandBus->handle(new CapturePayPalOrderCommand($orderId, array_keys($payPalOrderFromCache['payment_source'])[0]));
145+
$payPalOrderFromCache = $payPalOrderCache->get($orderId);
146+
$this->createOrder($payPalOrderFromCache, $payPalOrder);
147+
}
129148
} catch (Exception $exception) {
130149
$this->context->smarty->assign('error', $exception->getMessage());
131150
}
@@ -143,19 +162,14 @@ public function postProcess()
143162
*/
144163
private function createOrder($payPalOrderFromCache, $payPalOrder)
145164
{
146-
/** @var CommandBusInterface $commandBus */
147-
$commandBus = $this->module->getService('ps_checkout.bus.command');
148-
149-
$capture = $payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0];
150-
if ($capture['status'] === 'COMPLETED') {
151-
$commandBus->handle(new CreateOrderCommand($payPalOrder->getId()->getValue(), $capture));
152-
if ($payPalOrder->getPaymentTokenId() && $payPalOrder->checkCustomerIntent(PayPalOrder::CUSTOMER_INTENT_FAVORITE)) {
153-
/** @var PaymentTokenRepository $paymentTokenRepository */
154-
$paymentTokenRepository = $this->module->getService(PaymentTokenRepository::class);
155-
$paymentTokenRepository->setTokenFavorite($payPalOrder->getPaymentTokenId());
156-
}
157-
$this->redirectToOrderConfirmationPage($payPalOrder->getIdCart(), $capture['id'], $payPalOrderFromCache['status']);
165+
$capture = isset($payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0]) ? $payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0] : null;
166+
$this->commandBus->handle(new CreateOrderCommand($payPalOrder->getId()->getValue(), $capture));
167+
if ($payPalOrder->getPaymentTokenId() && $payPalOrder->checkCustomerIntent(PayPalOrder::CUSTOMER_INTENT_FAVORITE)) {
168+
/** @var PaymentTokenRepository $paymentTokenRepository */
169+
$paymentTokenRepository = $this->module->getService(PaymentTokenRepository::class);
170+
$paymentTokenRepository->setTokenFavorite($payPalOrder->getPaymentTokenId());
158171
}
172+
$this->redirectToOrderConfirmationPage($payPalOrder->getIdCart(), $capture ? $capture['id'] : null, $payPalOrderFromCache['status']);
159173
}
160174

161175
/**
@@ -218,4 +232,23 @@ private function redirectToOrderConfirmationPage($cartId, $captureId, $payPalOrd
218232
));
219233
}
220234
}
235+
236+
/**
237+
* @param string $orderId
238+
*
239+
* @return GetPayPalOrderForCheckoutCompletedQueryResult
240+
*
241+
* @throws PayPalOrderException
242+
*/
243+
private function getPayPalOrder($orderId)
244+
{
245+
$payPalOrderQuery = new GetPayPalOrderForCheckoutCompletedQuery($orderId);
246+
247+
return $this->queryBus->handle($payPalOrderQuery);
248+
}
249+
250+
private function redirectToOrderHistoryPage()
251+
{
252+
Tools::redirect($this->context->link->getPageLink('history'));
253+
}
221254
}

controllers/front/validate.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
use PrestaShop\Module\PrestashopCheckout\Checkout\Event\CheckoutCompletedEvent;
23-
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
23+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2424
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2525
use PrestaShop\Module\PrestashopCheckout\Event\EventDispatcherInterface;
2626
use PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherAdapter;
@@ -126,7 +126,7 @@ private function generateResponse()
126126
}
127127

128128
try {
129-
/** @var CommandBusInterface $queryBus */
129+
/** @var QueryBusInterface $queryBus */
130130
$queryBus = $this->module->getService('ps_checkout.bus.query');
131131

132132
/** @var PsCheckoutCartRepository $psCheckoutCartRepository */
@@ -438,6 +438,9 @@ private function handleException(Exception $exception)
438438
'body' => [
439439
'error' => [
440440
'message' => $exceptionMessageForCustomer,
441+
'code' => (int) $exception->getCode() < 400 && $exception->getPrevious() !== null
442+
? (int) $exception->getPrevious()->getCode()
443+
: (int) $exception->getCode(),
441444
],
442445
],
443446
'exceptionCode' => $exception->getCode(),

controllers/front/vault.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
22+
use PrestaShop\Module\PrestashopCheckout\CommandBus\QueryBusInterface;
2223
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
2324
use PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\Command\DeletePaymentTokenCommand;
2425
use PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\Query\GetCustomerPaymentTokensQuery;
@@ -40,7 +41,7 @@ public function postProcess()
4041
/** @var CommandBusInterface $commandBus */
4142
$commandBus = $this->module->getService('ps_checkout.bus.command');
4243

43-
/** @var CommandBusInterface $queryBus */
44+
/** @var QueryBusInterface $queryBus */
4445
$queryBus = $this->module->getService('ps_checkout.bus.query');
4546

4647
$bodyValues = [];

src/Checkout/CheckoutChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(LoggerInterface $logger)
5454
public function continueWithAuthorization($cartId, $orderPayPal)
5555
{
5656
if ($orderPayPal['status'] === 'COMPLETED') {
57-
throw new PsCheckoutException(sprintf('PayPal Order %s is already captured', $orderPayPal['id']));
57+
throw new PsCheckoutException(sprintf('PayPal Order %s is already captured', $orderPayPal['id']), PsCheckoutException::PAYPAL_ORDER_ALREADY_CAPTURED);
5858
}
5959

6060
$paymentSource = isset($orderPayPal['payment_source']) ? key($orderPayPal['payment_source']) : '';

0 commit comments

Comments
 (0)