-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOnePageCheckoutGuestInitHandler.php
More file actions
709 lines (612 loc) · 21.2 KB
/
OnePageCheckoutGuestInitHandler.php
File metadata and controls
709 lines (612 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
namespace PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax;
use Cart;
use Configuration;
use Context;
use Customer;
use Db;
use PrestaShop\Module\PsOnePageCheckout\Checkout\ExistingCustomerState;
use PrestaShop\Module\PsOnePageCheckout\Form\OnePageCheckoutForm;
use PrestaShop\PrestaShop\Core\Util\InternationalizedDomainNameConverter;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @phpstan-type GuestInitResponse array{
* success: bool,
* customer_created: bool,
* id_customer: int,
* errors: array<string, array<int, string>>,
* token?: string,
* static_token?: string
* }
*
* Handles guest initialization in one-page checkout.
* Keep checkout idempotent and link the cart to the right customer.
*/
class OnePageCheckoutGuestInitHandler
{
private const ERROR_FIELD_GLOBAL = '';
private const ERROR_FIELD_EMAIL = 'email';
private const ERROR_FIELD_TOKEN = 'token';
private const ERROR_GUEST_EMAIL_UPDATE_FAILED = 'Unable to update guest email.';
private const ERROR_CART_CUSTOMER_SYNC_FAILED = 'Unable to synchronize cart customer link.';
private const CART_UPDATE_APPLIED = 1;
private const CART_UPDATE_NOT_APPLIED = 0;
private const CART_UPDATE_FAILED = -1;
/**
* Customer id used when no customer is resolved.
*/
private const CUSTOMER_ID_NONE = 0;
private const RESPONSE_DEFAULT = [
'success' => false,
'customer_created' => false,
'id_customer' => 0,
'errors' => [],
];
/**
* @var \Context
*/
private $context;
/**
* @var OnePageCheckoutForm
*/
private $opcForm;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var \CustomerPersister
*/
private $customerPersister;
/**
* @var bool
*/
private $isOnePageCheckoutEnabled;
/**
* @var InternationalizedDomainNameConverter
*/
private $idnConverter;
/**
* @param \Context $context
* @param OnePageCheckoutForm $opcForm
* @param TranslatorInterface $translator
* @param \CustomerPersister $customerPersister
* @param bool $isOnePageCheckoutEnabled
*/
public function __construct(
\Context $context,
OnePageCheckoutForm $opcForm,
TranslatorInterface $translator,
\CustomerPersister $customerPersister,
bool $isOnePageCheckoutEnabled,
) {
$this->context = $context;
$this->opcForm = $opcForm;
$this->translator = $translator;
$this->customerPersister = $customerPersister;
$this->isOnePageCheckoutEnabled = $isOnePageCheckoutEnabled;
$this->idnConverter = new InternationalizedDomainNameConverter();
}
/**
* Entry point for guest init AJAX payload.
*
* @param array<string, mixed> $requestParameters
*
* @return GuestInitResponse
*/
public function handle(array $requestParameters): array
{
// Only available in one-page checkout mode.
if (!$this->isOnePageCheckoutEnabled) {
return $this->errorResponse(
self::ERROR_FIELD_GLOBAL,
$this->translator->trans(
'One-page checkout is not enabled.',
[],
'Shop.Notifications.Error'
)
);
}
// No eligible cart or guest checkout disabled: nothing to initialize.
if (!$this->hasEligibleCart() || !$this->isGuestCheckoutEnabled()) {
return $this->successResponse();
}
// This endpoint can create/update customer data, token is mandatory.
if (!$this->isTokenValid($requestParameters)) {
return $this->errorResponse(
self::ERROR_FIELD_TOKEN,
$this->translator->trans(
'Invalid security token.',
[],
'Shop.Notifications.Error'
),
false
);
}
$existingCustomerState = $this->resolveExistingCustomerState();
$existingCustomerResponse = $this->handleExistingCustomer($existingCustomerState);
if ($existingCustomerResponse !== null) {
return $existingCustomerResponse;
}
$submittedEmail = $this->getSubmittedEmail($requestParameters);
$emailDecisionResponse = $this->resolveGuestEmail($submittedEmail, $existingCustomerState);
if ($emailDecisionResponse !== null) {
return $emailDecisionResponse;
}
// Keep customer creation in the existing OPC form workflow.
if (!$this->opcForm->submitGuestInit($requestParameters)) {
$response = self::RESPONSE_DEFAULT;
$response['errors'] = $this->opcForm->getErrors();
return $this->withSecurityToken($response);
}
// Attach the created guest to the cart.
$createdCustomerId = (int) $this->context->customer->id;
$resolvedCustomerId = $this->persistCartCustomerId($createdCustomerId);
if ($resolvedCustomerId === self::CUSTOMER_ID_NONE) {
return $this->cartSyncErrorResponse();
}
$this->syncResolvedCustomerContext($resolvedCustomerId);
// If another request already claimed the cart, return that owner.
return $this->successResponse($resolvedCustomerId, $resolvedCustomerId === $createdCustomerId);
}
/**
* Read guest checkout configuration flag.
*
* @return bool
*/
protected function isGuestCheckoutEnabled(): bool
{
return (bool) \Configuration::get('PS_GUEST_CHECKOUT_ENABLED');
}
/**
* @param int $customerId
*
* @return \Customer
*/
protected function loadCustomerById(int $customerId): \Customer
{
return new \Customer($customerId);
}
/**
* Check whether an email already belongs to a customer.
*
* @param string $email
*
* @return int
*/
protected function findCustomerByEmail(string $email): int
{
return (int) \Customer::customerExists($email, true, false);
}
/**
* Read the security token source used by request and response.
*
* @return string
*/
protected function getExpectedToken(): string
{
return (string) \Tools::getToken(false);
}
/**
* @param int $cartId
*
* @return \Cart
*/
protected function loadCartById(int $cartId): \Cart
{
return new \Cart($cartId);
}
/**
* Change cart owner only if the cart still belongs to the expected customer.
*
* @param int $cartId
* @param int $expectedCustomerId
* @param int $newCustomerId
*
* @return int One of CART_UPDATE_APPLIED, CART_UPDATE_NOT_APPLIED, CART_UPDATE_FAILED
*/
protected function replaceCartCustomerIdIfMatches(int $cartId, int $expectedCustomerId, int $newCustomerId): int
{
// If a concurrent request already changed id_customer, this update matches no row (0 affected rows).
$query = sprintf(
'UPDATE `%scart` SET `id_customer` = %d WHERE `id_cart` = %d AND `id_customer` = %d',
_DB_PREFIX_,
$newCustomerId,
$cartId,
$expectedCustomerId
);
$db = \Db::getInstance();
if (!$db->execute($query)) {
return self::CART_UPDATE_FAILED;
}
return $db->Affected_Rows() > 0 ? self::CART_UPDATE_APPLIED : self::CART_UPDATE_NOT_APPLIED;
}
/**
* Check if this customer exists.
*
* @param int $customerId
*
* @return bool
*/
private function isLoadedCustomerId(int $customerId): bool
{
if ($customerId <= 0) {
return false;
}
$customer = $this->loadCustomerById($customerId);
return \Validate::isLoadedObject($customer);
}
/**
* Resolve checkout customer id with cart owner as source of truth.
*
* @return int Existing customer id, or CUSTOMER_ID_NONE when no customer can be resolved
*/
private function resolveExistingCustomerId(): int
{
$contextCustomerId = (int) $this->context->customer->id;
if (!\Validate::isLoadedObject($this->context->cart)) {
return $contextCustomerId;
}
// Read latest persisted owner before using in-memory cart value.
$freshCartCustomerId = $this->getFreshCartCustomerId();
if ($freshCartCustomerId > 0 && $this->isLoadedCustomerId($freshCartCustomerId)) {
return $freshCartCustomerId;
}
if ($freshCartCustomerId > 0) {
// Ignore stale persisted owner and continue with current request context.
return $contextCustomerId > 0 ? $contextCustomerId : self::CUSTOMER_ID_NONE;
}
$contextCartCustomerId = (int) $this->context->cart->id_customer;
if ($contextCartCustomerId > 0) {
return $contextCartCustomerId;
}
return $contextCustomerId;
}
/**
* Read latest persisted cart owner from database.
*
* @return int Customer id linked to cart in DB, or CUSTOMER_ID_NONE when unavailable
*/
protected function getFreshCartCustomerId(): int
{
if (!\Validate::isLoadedObject($this->context->cart)) {
return self::CUSTOMER_ID_NONE;
}
$cartId = (int) $this->context->cart->id;
if ($cartId <= 0) {
return self::CUSTOMER_ID_NONE;
}
// Read cart ownership directly from DB to avoid stale in-memory cart state.
$customerId = \Db::getInstance()->getValue(sprintf(
'SELECT `id_customer` FROM `%scart` WHERE `id_cart` = %d',
_DB_PREFIX_,
$cartId
));
if ($customerId === false) {
return self::CUSTOMER_ID_NONE;
}
return (int) $customerId;
}
/**
* Guest init applies only when cart exists and has products.
*
* @return bool
*/
private function hasEligibleCart(): bool
{
if (!\Validate::isLoadedObject($this->context->cart)) {
return false;
}
return (int) $this->context->cart->nbProducts() > 0;
}
/**
* Accept both `token` and `static_token`.
*
* @param array<string, mixed> $requestParameters
*
* @return bool
*/
private function isTokenValid(array $requestParameters): bool
{
$submittedToken = (string) ($requestParameters['token'] ?? $requestParameters['static_token'] ?? '');
if ($submittedToken === '') {
return false;
}
return hash_equals($this->getExpectedToken(), $submittedToken);
}
/**
* Resolve the current customer state for guest init.
* Return an empty state when no valid customer is attached.
*
* @return ExistingCustomerState
*/
private function resolveExistingCustomerState(): ExistingCustomerState
{
$existingCustomerId = $this->resolveExistingCustomerId();
if ($existingCustomerId <= 0) {
return ExistingCustomerState::empty();
}
$existingCustomer = $this->loadCustomerById($existingCustomerId);
if (!\Validate::isLoadedObject($existingCustomer)) {
// Stale owner id: continue as if no customer was attached.
return ExistingCustomerState::empty();
}
return ExistingCustomerState::fromCustomer($existingCustomer);
}
/**
* If the cart is already linked to a registered customer, skip guest init and return success.
*
* @param ExistingCustomerState $existingCustomerState
*
* @return GuestInitResponse|null
*/
private function handleExistingCustomer(ExistingCustomerState $existingCustomerState): ?array
{
if (!$existingCustomerState->hasCustomer()) {
return null;
}
if (!$existingCustomerState->isGuestCustomer()) {
return $this->successResponse($existingCustomerState->getId());
}
return null;
}
/**
* Normalize submitted email before validation and comparison.
*
* @param array<string, mixed> $requestParameters
*
* @return string
*/
private function getSubmittedEmail(array $requestParameters): string
{
$submittedEmail = trim((string) ($requestParameters['email'] ?? ''));
if ($submittedEmail === '') {
return '';
}
return $this->idnConverter->emailToUtf8($submittedEmail);
}
/**
* Update the customer email and prevent cart ownership from being switched to another account.
*
* @param string $submittedEmail
* @param ExistingCustomerState $existingCustomerState
*
* @return GuestInitResponse|null Terminal response or null to continue submit flow
*/
private function resolveGuestEmail(string $submittedEmail, ExistingCustomerState $existingCustomerState): ?array
{
$existingCustomerId = $existingCustomerState->getId();
$existingCustomer = $existingCustomerState->getCustomer();
if ($submittedEmail === '' || !\Validate::isEmail($submittedEmail)) {
return null;
}
if (!$existingCustomerState->isGuestCustomer()) {
return $this->resolveEmailForNonGuest($submittedEmail, $existingCustomerId);
}
// Same email: nothing to change.
if (strcasecmp((string) $existingCustomer->email, $submittedEmail) === 0) {
return $this->successResponse($existingCustomerId);
}
$existingCustomerForEmailId = $this->findCustomerByEmail($submittedEmail);
// Email already used by another account: keep current guest.
if ($existingCustomerForEmailId > 0 && $existingCustomerForEmailId !== $existingCustomerId) {
return $this->successResponse($existingCustomerId);
}
// Align context with target guest so the email update is saved on the right account.
$this->ensureContextCustomerMatches($existingCustomer);
$existingCustomer->email = $submittedEmail;
if (!$this->customerPersister->save($existingCustomer, '', '', false)) {
return $this->errorResponse(
self::ERROR_FIELD_EMAIL,
$this->translator->trans(
self::ERROR_GUEST_EMAIL_UPDATE_FAILED,
[],
'Shop.Notifications.Error'
)
);
}
$resolvedCustomerId = $this->persistCartCustomerId($existingCustomerId);
if ($resolvedCustomerId === self::CUSTOMER_ID_NONE) {
return $this->cartSyncErrorResponse();
}
$this->syncResolvedCustomerContext($resolvedCustomerId);
return $this->successResponse($resolvedCustomerId);
}
/**
* In non-guest, submitted email must never change checkout identity:
* if it belongs to another account, keep the current cart owner.
*
* @param string $submittedEmail
* @param int $existingCustomerId
*
* @return GuestInitResponse|null
*/
private function resolveEmailForNonGuest(string $submittedEmail, int $existingCustomerId): ?array
{
$existingCustomerForEmailId = $this->findCustomerByEmail($submittedEmail);
if ($existingCustomerForEmailId <= 0) {
return null;
}
if ($existingCustomerId === self::CUSTOMER_ID_NONE) {
return $this->successResponse();
}
// Keep current owner if email points to another account.
if ($existingCustomerForEmailId !== $existingCustomerId) {
return $this->successResponse($existingCustomerId);
}
return null;
}
/**
* Keep cart ownership consistent, including concurrent requests.
*
* @param int $customerId
*
* @return int resolved customer id
*/
private function persistCartCustomerId(int $customerId): int
{
if ($customerId <= 0 || !\Validate::isLoadedObject($this->context->cart)) {
return $customerId;
}
$cartId = (int) $this->context->cart->id;
if ($cartId <= 0) {
return $customerId;
}
$freshCartCustomerId = $this->getFreshCartCustomerId();
// Another request already claimed cart ownership: keep winner for deterministic result.
if ($freshCartCustomerId > self::CUSTOMER_ID_NONE) {
return $this->isLoadedCustomerId($freshCartCustomerId)
? $freshCartCustomerId
: self::CUSTOMER_ID_NONE;
}
return $this->claimCartForCustomer($cartId, $customerId);
}
/**
* Ensure context customer matches the guest being updated so persistence targets the right account.
*
* @param \Customer $customer
*
* @return void
*/
private function ensureContextCustomerMatches(\Customer $customer): void
{
if ((int) $this->context->customer->id === (int) $customer->id) {
return;
}
$this->context->customer = $customer;
}
/**
* Preserve checkout identity after cart owner resolution.
* Prevent next requests from reusing stale cookie data and rebinding the cart.
*
* @param int $customerId
*
* @return void
*/
private function syncResolvedCustomerContext(int $customerId): void
{
if ($customerId <= 0) {
return;
}
$customer = $this->loadCustomerById($customerId);
if (!\Validate::isLoadedObject($customer)) {
return;
}
$this->context->customer = $customer;
if (!isset($this->context->cookie)) {
return;
}
$this->context->cookie->id_customer = (int) $customer->id;
$this->context->cookie->customer_lastname = (string) $customer->lastname;
$this->context->cookie->customer_firstname = (string) $customer->firstname;
$this->context->cookie->passwd = (string) $customer->passwd;
$this->context->cookie->logged = true;
$this->context->cookie->email = (string) $customer->email;
$this->context->cookie->is_guest = (bool) $customer->isGuest();
$this->context->cookie->write();
}
/**
* Claim unassigned cart for customer, or return the winner if race is lost.
*
* @param int $cartId
* @param int $customerId
*
* @return int
*/
private function claimCartForCustomer(int $cartId, int $customerId): int
{
$claimResult = $this->replaceCartCustomerIdIfMatches(
$cartId,
self::CUSTOMER_ID_NONE,
$customerId
);
if ($claimResult === self::CART_UPDATE_FAILED) {
return self::CUSTOMER_ID_NONE;
}
if ($claimResult === self::CART_UPDATE_APPLIED) {
return $customerId;
}
return $this->resolveWinnerAfterRace();
}
/**
* Reload cart owner after a race to return the effective owner.
*
* @return int resolved owner id, or CUSTOMER_ID_NONE when owner cannot be resolved
*/
private function resolveWinnerAfterRace(): int
{
$resolvedCustomerId = $this->getFreshCartCustomerId();
if (!$this->isLoadedCustomerId($resolvedCustomerId)) {
return self::CUSTOMER_ID_NONE;
}
return $resolvedCustomerId;
}
/**
* Return a success response in the frontend.
*
* @param int $customerId
* @param bool $customerCreated
*
* @return GuestInitResponse
*/
private function successResponse(int $customerId = self::CUSTOMER_ID_NONE, bool $customerCreated = false): array
{
$response = self::RESPONSE_DEFAULT;
$response['success'] = true;
$response['id_customer'] = $customerId;
$response['customer_created'] = $customerCreated;
return $this->withSecurityToken($response);
}
/**
* Return an error response in the frontend.
*
* @param string $field
* @param string $message
* @param bool $withSecurityToken
*
* @return GuestInitResponse
*/
private function errorResponse(string $field, string $message, bool $withSecurityToken = true): array
{
$response = self::RESPONSE_DEFAULT;
$response['errors'][$field][] = $message;
if (!$withSecurityToken) {
return $response;
}
return $this->withSecurityToken($response);
}
/**
* Return an error when cart ownership cannot be synchronized with the customer.
*
* @return GuestInitResponse
*/
private function cartSyncErrorResponse(): array
{
return $this->errorResponse(
self::ERROR_FIELD_GLOBAL,
$this->translator->trans(
self::ERROR_CART_CUSTOMER_SYNC_FAILED,
[],
'Shop.Notifications.Error'
)
);
}
/**
* Always return fresh security tokens in response payload.
*
* @param array<string, mixed> $response
*
* @return array<string, mixed>
*/
private function withSecurityToken(array $response): array
{
$securityToken = $this->getExpectedToken();
$response['token'] = $securityToken;
$response['static_token'] = $securityToken;
return $response;
}
}