diff --git a/CHANGELOG.md b/CHANGELOG.md index ee59ec8..38b98f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [4.0.5](https://github.com/unzerdev/magento2/compare/4.0.4..4.0.5) +### Fixed +* Fix customerId assignment for guest customers in UPL payment methods +* Fix and improve salutation handling logic + ## [4.0.4](https://github.com/unzerdev/magento2/compare/4.0.3..4.0.4) ### Fixed * Resolve issue with customer company information mapping diff --git a/Helper/Order.php b/Helper/Order.php index 9353bfe..62633de 100644 --- a/Helper/Order.php +++ b/Helper/Order.php @@ -341,7 +341,11 @@ public function createCustomerFromQuote(Quote $quote, string $email, bool $creat ->setFirstname($billingAddress->getFirstname()) ->setLastname($billingAddress->getLastname()); - $customer->setSalutation($this->getSalutationFromQuote($quote)); + $customer->setSalutation( + $this->getSalutationFromPrefix($billingAddress->getPrefix()) + ?? $this->getSalutationFromQuote($quote) + ); + $customer->setEmail($email); $customer->setPhone($billingAddress->getTelephone()); $customer->setBirthDate($quote->getCustomer()->getDob()); @@ -419,12 +423,12 @@ public function createCustomerFromOrder( $customer->setCompany($company); } - $gender = $order->getCustomerGender(); - if ($gender) { - $customer->setSalutation($this->getSalutationFromGender($gender)); - } else { - $customer->setSalutation($this->getSalutationFromPayment($order->getPayment())); - } + $customer->setSalutation( + $this->getSalutationFromPrefix($billingAddress->getPrefix()) + ?? $this->getSalutationFromGender($order->getCustomerGender()) + ?? $this->getSalutationFromPayment($order->getPayment()) + ); + $birthDate = $this->getBirthdateFromPayment($order->getPayment()); if ($birthDate) { $customer->setBirthDate($birthDate); @@ -500,7 +504,7 @@ private function updateGatewayAddressFromMagento( ): void { $street = $this->convertStreetLinesToString($magentoAddress->getStreet()); - $gatewayAddress->setName($magentoAddress->getName()); + $gatewayAddress->setName($magentoAddress->getFirstname() . ' ' . $magentoAddress->getLastname()); $gatewayAddress->setCity($magentoAddress->getCity()); $gatewayAddress->setCountry($magentoAddress->getCountryId()); $gatewayAddress->setStreet($street); @@ -571,6 +575,11 @@ public function updateGatewayCustomerFromOrder(OrderModel $order, Customer $gate $gatewayCustomer->setFirstname($billingAddress->getFirstname()); $gatewayCustomer->setLastname($billingAddress->getLastname()); + $gatewayCustomer->setSalutation( + $this->getSalutationFromPrefix($billingAddress->getPrefix()) + ?? Salutations::UNKNOWN + ); + $gatewayCustomer->setCompany($billingAddress->getCompany()); $gatewayCustomer->setEmail($billingAddress->getEmail()); @@ -659,9 +668,9 @@ private function validateGatewayAddressAgainstOrderAddress( * * @param Quote $quote * - * @return string + * @return ?string */ - protected function getSalutationFromQuote(Quote $quote): string + protected function getSalutationFromQuote(Quote $quote): ?string { return $this->getSalutationFromGender($quote->getCustomer()->getGender()); } @@ -671,9 +680,9 @@ protected function getSalutationFromQuote(Quote $quote): string * * @param float|int $gender * - * @return string + * @return ?string */ - protected function getSalutationFromGender($gender): string + protected function getSalutationFromGender($gender): ?string { switch ($gender) { case self::GENDER_MALE: @@ -683,7 +692,7 @@ protected function getSalutationFromGender($gender): string $salutation = Salutations::MRS; break; default: - $salutation = Salutations::UNKNOWN; + $salutation = null; } return $salutation; } @@ -700,6 +709,24 @@ protected function getSalutationFromPayment(InfoInterface $payment): ?string return $payment->getAdditionalInformation('salutation'); } + /** + * @param ?string $prefix + * + * @return ?string + */ + protected function getSalutationFromPrefix(?string $prefix): ?string + { + $prefix = strtolower(trim($prefix ?? '')); + if (str_starts_with($prefix, 'mrs') || str_starts_with($prefix, 'ms') || str_starts_with($prefix, 'mis')) { + return Salutations::MRS; + } + if (str_starts_with($prefix, 'mr')) { + return Salutations::MR; + } + + return null; + } + /** * Get Birthdate from Payment * diff --git a/composer.json b/composer.json index 2a4d80f..e14a6d3 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "unzerdev/magento2", "description": "This extension for Magento 2 provides a direct integration of the Unzer payment types to your Magento 2 shop via the Unzer Payment API (PAPI).", "type": "magento2-module", - "version": "4.0.4", + "version": "4.0.5", "license": "Apache-2.0", "require": { "php": "~7.4.0|~8.1.0|~8.2.0|~8.3.0|~8.4.0", diff --git a/etc/module.xml b/etc/module.xml index f052ef0..0affbf9 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/view/frontend/web/js/view/payment/method-renderer/basev2.js b/view/frontend/web/js/view/payment/method-renderer/basev2.js index 5ad74d1..7c00f83 100644 --- a/view/frontend/web/js/view/payment/method-renderer/basev2.js +++ b/view/frontend/web/js/view/payment/method-renderer/basev2.js @@ -274,17 +274,33 @@ define( const shipping = quote.shippingAddress(); const customerId = window.checkoutConfig?.customerData.id || ''; - const uniqueCustomerId = `${customerId}_${email}_${shop}`; + const uniqueCustomerId = !quote.guestEmail && customerId ? `${customerId}_${email}_${shop}` : ''; if (unzerCustomerId) { this.customer = unzerCustomerId; } + const mapSalutation = (prefix) => { + if (!prefix) return "unknown"; + + const normalized = prefix.toLowerCase().trim(); + + if (normalized.startsWith('mrs') || normalized.startsWith('ms') || normalized.startsWith('mis')) { + return 'mrs'; + } + if (normalized.startsWith('mr')) { + return 'mr'; + } + + return "unknown"; + }; + const customer = { id: unzerCustomerId || '', customerId: uniqueCustomerId, firstname: billing ? billing.firstname : '', lastname: billing ? billing.lastname : '', + salutation: billing ? mapSalutation(billing.prefix) : 'unknown', email: email, ...(customerData?.dob ? {birthDate: customerData.dob.split('T')[0]} : {}), billingAddress: billing ? {