From f5a5c7d8e9cab99e62bf1238010f6974086539c6 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Wed, 4 Dec 2024 12:52:43 +0100 Subject: [PATCH] NTR: PISHPS-396: Reuse guest accounts for express checkout --- .../ApplePayDirect/ApplePayDirect.php | 37 +++++++++++++------ src/Service/CustomerService.php | 12 +++--- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/Components/ApplePayDirect/ApplePayDirect.php b/src/Components/ApplePayDirect/ApplePayDirect.php index e703de143..3a062eb5c 100644 --- a/src/Components/ApplePayDirect/ApplePayDirect.php +++ b/src/Components/ApplePayDirect/ApplePayDirect.php @@ -355,20 +355,31 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema throw new \Exception('PaymentToken not found!'); } - + $updateShippingAddress = true; $applePayID = $this->getActiveApplePayID($context); - + $customer = $context->getCustomer(); + $shippingAddress = new AddressStruct($firstname, $lastname, $email, $street, '', $zipcode, $city, $countryCode, $phone); # if we are not logged in, # then we have to create a new guest customer for our express order - if (! $this->customerService->isCustomerLoggedIn($context)) { - $address = new AddressStruct($firstname, $lastname, $email, $street, '', $zipcode, $city, $countryCode, $phone); - - $customer = $this->customerService->createGuestAccount( - $address, - $applePayID, - $context, - $acceptedDataProtection - ); + if ($customer === null) { + + + # find existing customer by email + $customer = $this->customerService->findCustomerByEmail($shippingAddress->getEmail(), $context); + + if ($customer === null) { + $updateShippingAddress = false; + + + $customer = $this->customerService->createGuestAccount( + $shippingAddress, + $applePayID, + $context, + $acceptedDataProtection + ); + } + + if (! $customer instanceof CustomerEntity) { throw new \Exception('Error when creating customer!'); @@ -380,6 +391,10 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema $this->customerService->loginCustomer($customer, $context); } + if ($updateShippingAddress) { + $this->customerService->reuseOrCreateAddresses($customer, $shippingAddress, $context->getContext()); + } + # also (always) update our payment method to use Apple Pay for our cart return $this->cartService->updatePaymentMethod($context, $applePayID); } diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index 313e3b353..63e2c17fe 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -44,7 +44,7 @@ class CustomerService implements CustomerServiceInterface public const CUSTOM_FIELDS_KEY_SHOULD_SAVE_CARD_DETAIL = 'shouldSaveCardDetail'; public const CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER = 'preferred_ideal_issuer'; public const CUSTOM_FIELDS_KEY_PREFERRED_POS_TERMINAL = 'preferred_pos_terminal'; - public const CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID = 'ppe_address_id'; + public const CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID = 'express_address_id'; /** * @var CountryRepositoryInterface @@ -647,7 +647,7 @@ public function reuseOrCreateAddresses(CustomerEntity $customer, AddressStruct $ $criteria = new Criteria(); $criteria->addFilter(new AndFilter([ new EqualsFilter('customerId', $customer->getId()), - new EqualsAnyFilter('customFields.' . CustomFieldsInterface::MOLLIE_KEY . '.' . self::CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID, $mollieAddressIds) + new EqualsAnyFilter('customFields.' . CustomFieldsInterface::MOLLIE_KEY . '.' . self::CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID, $mollieAddressIds) ])); $customerAddressSearchResult = $this->customerAddressRepository->search($criteria, $context); @@ -689,7 +689,7 @@ public function reuseOrCreateAddresses(CustomerEntity $customer, AddressStruct $ } // skip addresses without custom fields, those are configured by the customer in backend - $mollieAddressId = $addressCustomFields[CustomFieldsInterface::MOLLIE_KEY][self::CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID] ?? null; + $mollieAddressId = $addressCustomFields[CustomFieldsInterface::MOLLIE_KEY][self::CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID] ?? null; if ($mollieAddressId === null) { continue; } @@ -763,7 +763,7 @@ public function createGuestAccount(AddressStruct $shippingAddress, string $payme $shippingAddressData->set('countryId', $countryId); $customFields = new RequestDataBag(); $customFields->set(CustomerAddressDefinition::ENTITY_NAME, [ - CustomFieldsInterface::MOLLIE_KEY => [self::CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID => $shippingAddress->getMollieAddressId()] + CustomFieldsInterface::MOLLIE_KEY => [self::CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID => $shippingAddress->getMollieAddressId()] ]); $shippingAddressData->set('customFields', $customFields); $data->set('shippingAddress', $shippingAddressData); @@ -780,7 +780,7 @@ public function createGuestAccount(AddressStruct $shippingAddress, string $payme $billingAddressData->set('countryId', $countryId); $customFields = new RequestDataBag(); $customFields->set(CustomerAddressDefinition::ENTITY_NAME, [ - CustomFieldsInterface::MOLLIE_KEY => [self::CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID => $billingAddress->getMollieAddressId()] + CustomFieldsInterface::MOLLIE_KEY => [self::CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID => $billingAddress->getMollieAddressId()] ]); $billingAddressData->set('customFields', $customFields); @@ -826,7 +826,7 @@ private function createShopwareAddressArray(string $addressId, string $customerI 'phoneNumber' => '', 'customFields' => [ CustomFieldsInterface::MOLLIE_KEY => [ - self::CUSTOM_FIELDS_KEY_PAYPAL_EXPRESS_ADDRESS_ID => $address->getMollieAddressId() + self::CUSTOM_FIELDS_KEY_EXPRESS_ADDRESS_ID => $address->getMollieAddressId() ] ] ];