Skip to content

Commit

Permalink
NTR: PISHPS-396: Reuse guest accounts for express checkout (#903)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 4, 2024
1 parent fc55d06 commit e394ccf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
37 changes: 26 additions & 11 deletions src/Components/ApplePayDirect/ApplePayDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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()
]
]
];
Expand Down

0 comments on commit e394ccf

Please sign in to comment.