Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #125 from heidelpay/fix-customer-creation
Browse files Browse the repository at this point in the history
UMCS-265: Fix customer creation
  • Loading branch information
Ryouzanpaku authored Jun 22, 2021
2 parents 4e6a32f + 96f3001 commit 352b2d9
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 36 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
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.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [1.1.2][1.1.2]
### Fix
* Payment customer
* Customer resource was created to early in checkout before order was placed.
* Customer data were missing when ordering with a virtual quote.
* Missing customer-email when ordering with invoice secured and direct debit secured.

### Added
* Payment method asks for salutation when needed.

## [1.1.1][1.1.1]
### Fix
* Incompatibility with magento 2.3.x causing DI compilation to fail.
Expand Down Expand Up @@ -31,3 +41,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
[1.0.2]: https://github.com/heidelpay/magento2-merchant-gateway/compare/1.0.1..1.0.2
[1.1.0]: https://github.com/heidelpay/magento2-merchant-gateway/compare/1.0.2..1.1.0
[1.1.1]: https://github.com/heidelpay/magento2-merchant-gateway/compare/1.1.0..1.1.1
[1.1.2]: https://github.com/heidelpay/magento2-merchant-gateway/compare/1.1.1..1.1.2
94 changes: 79 additions & 15 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ public function createMetadataForOrder(OrderModel $order): Metadata
*
* @param Quote $quote
* @param string $email
* @param bool $createResource
*
* @return Customer
* @throws HeidelpayApiException
* @throws NoSuchEntityException
*/
public function createCustomerFromQuote(Quote $quote, string $email): ?Customer
public function createCustomerFromQuote(Quote $quote, string $email, bool $createResource = false): ?Customer
{
// A virtual quote does not have any customer data other than E-Mail so we can't create a customer object.
if ($quote->isVirtual()) {
Expand Down Expand Up @@ -200,7 +201,50 @@ public function createCustomerFromQuote(Quote $quote, string $email): ?Customer
/** @var Heidelpay $client */
$client = $this->_moduleConfig->getHeidelpayClient();

return $client->createCustomer($customer);
return $createResource ? $client->createCustomer($customer) : $customer;
}

/**
* Returns a new or updated Heidelpay Customer resource for the given quote.
*
* @param OrderModel $order
* @param string $email
* @param bool $createResource
*
* @return Customer
* @throws HeidelpayApiException
*/
public function createCustomerFromOrder(OrderModel $order, string $email, bool $createResource = false): ?Customer
{
/** @var Heidelpay $client */
$client = $this->_moduleConfig->getHeidelpayClient();

$billingAddress = $order->getBillingAddress();

/** @var Customer $customer */
$customer = CustomerFactory::createCustomer(
$billingAddress->getFirstname(),
$billingAddress->getLastname()
);

$gender = $order->getCustomerGender();
$customer->setSalutation($this->getSalutationFromGender($gender));
$customer->setEmail($email);
$customer->setPhone($billingAddress->getTelephone());

$company = $billingAddress->getCompany();
if (!empty($company)) {
$customer->setCompany($company);
}

$this->updateGatewayAddressFromMagento($customer->getBillingAddress(), $billingAddress);

$shippingAddress = $order->getShippingAddress();
if($shippingAddress) {
$this->updateGatewayAddressFromMagento($customer->getShippingAddress(), $shippingAddress);
}

return $createResource ? $client->createCustomer($customer) : $customer;
}

/**
Expand All @@ -216,8 +260,9 @@ private function updateGatewayAddressFromMagento(
{
$street = $this->convertStreetLinesToString($magentoAddress->getStreet());

$gatewayAddress->setName($magentoAddress->getName());
$gatewayAddress->setCity($magentoAddress->getCity());
$gatewayAddress->setCountry($magentoAddress->getCountry());
$gatewayAddress->setCountry($magentoAddress->getCountryId());
$gatewayAddress->setStreet($street);
$gatewayAddress->setZip($magentoAddress->getPostcode());
}
Expand Down Expand Up @@ -247,16 +292,20 @@ public function updateGatewayCustomerFromOrder(OrderModel $order, Customer $gate
$gatewayCustomer->setLastname($billingAddress->getLastname());

$gatewayCustomer->setCompany($billingAddress->getCompany());
$gatewayCustomer->setEmail($billingAddress->getEmail());

$this->updateGatewayAddressFromMagento(
$gatewayCustomer->getBillingAddress(),
$billingAddress
);

$this->updateGatewayAddressFromMagento(
$gatewayCustomer->getShippingAddress(),
$order->getShippingAddress()
);
$magentoShippingAddress = $order->getShippingAddress();
if (null !== $magentoShippingAddress) {
$this->updateGatewayAddressFromMagento(
$gatewayCustomer->getShippingAddress(),
$magentoShippingAddress
);
}

$client = $this->_moduleConfig->getHeidelpayClient();
$client->updateCustomer($gatewayCustomer);
Expand All @@ -277,18 +326,23 @@ public function validateGatewayCustomerAgainstOrder(OrderModel $order, Customer
// Magento's getCompany() always returns a string, but the heidelpay Customer Address does not, so we must make
// sure that both have the same type.
$companyValid = ($order->getBillingAddress()->getCompany() ?? '') === ($gatewayCustomer->getCompany() ?? '');
$emailValid = $order->getCustomerEmail() === $gatewayCustomer->getEmail();

$billingAddressValid = $this->validateGatewayAddressAgainstOrderAddress(
$gatewayCustomer->getBillingAddress(),
$order->getBillingAddress()
);

$shippingAddressValid = $this->validateGatewayAddressAgainstOrderAddress(
$gatewayCustomer->getShippingAddress(),
$order->getShippingAddress()
);
$shippingAddress = $order->getShippingAddress();
$shippingAddressValid = true;
if($shippingAddress !== null) {
$shippingAddressValid = $this->validateGatewayAddressAgainstOrderAddress(
$gatewayCustomer->getShippingAddress(),
$shippingAddress
);
}

return $nameValid && $companyValid && $billingAddressValid && $shippingAddressValid;
return $nameValid && $companyValid && $billingAddressValid && $shippingAddressValid && $emailValid;
}

/**
Expand Down Expand Up @@ -320,7 +374,17 @@ private function validateGatewayAddressAgainstOrderAddress(
*/
protected function getSalutationFromQuote(Quote $quote): string
{
switch ($quote->getCustomer()->getGender()) {
return $this->getSalutationFromGender($quote->getCustomer()->getGender());
}

/**
* @param float | int $gender
*
* @return string
*/
protected function getSalutationFromGender($gender): string
{
switch ($gender) {
case self::GENDER_MALE:
$salutation = Salutations::MR;
break;
Expand All @@ -331,5 +395,5 @@ protected function getSalutationFromQuote(Quote $quote): string
$salutation = Salutations::UNKNOWN;
}
return $salutation;
}
}
}
9 changes: 9 additions & 0 deletions Model/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ protected function _getCustomerId(InfoInterface $payment, \Magento\Sales\Model\O
/** @var string|null $customerId */
$customerId = $payment->getAdditionalInformation(BaseDataAssignObserver::KEY_CUSTOMER_ID);

if (empty($customerId)) {
try {
$papiCustomer = $this->_orderHelper->createCustomerFromOrder($order, $order->getCustomerEmail(), true);
$customerId = $papiCustomer->getId();
} catch (\Exception $exception) {
$customerId = null;
}
}

if (empty($customerId)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Heidelpay_MGW" schema_version="1.1.1" setup_version="1.1.1">
<module name="Heidelpay_MGW" schema_version="1.1.2" setup_version="1.1.2">
<sequence>
<module name="Magento_Checkout"/>
<module name="Magento_Config" />
Expand Down
40 changes: 20 additions & 20 deletions view/frontend/web/js/view/payment/method-renderer/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,12 @@ define(
_initializeCustomerFormForB2bCustomer: function (fieldId, errorFieldId, customer) {
this.customerProvider = this.sdk.B2BCustomer();
this.customerProvider.initFormFields(customer);
this.customerProvider.update(
customer.id,
{
containerId: fieldId,
errorHolderId: errorFieldId,
fields: ['companyInfo'],
showHeader: false
}
);
this.customerProvider.create({
containerId: fieldId,
errorHolderId: errorFieldId,
fields: ['companyInfo'],
showHeader: false
});

// The SDK currently always shows these fields, although we don't specify them in the options above.
// Hide them manually since users are not allowed to change them anyways.
Expand All @@ -108,16 +105,16 @@ define(
_initializeCustomerFormForB2cCustomer: function (fieldId, errorFieldId, customer) {
this.customerProvider = this.sdk.Customer();
this.customerProvider.initFormFields(customer);
this.customerProvider.update(
customer.id,
{
infoBoxText: $t('Your date of birth'),
containerId: fieldId,
errorHolderId: errorFieldId,
fields: ['birthdate'],
showHeader: false
}
);
this.customerProvider.create({
containerId: fieldId,
errorHolderId: errorFieldId,
showHeader: false
});

var field = $('#' + fieldId);
field.find('.field').filter('.city, .company, :has(.country), .street, .zip, .firstname, .lastname').hide();
field.find('.heidelpayUI.divider-horizontal:eq(0)').hide();
field.find('.heidelpayUI.message.downArrow').hide();
},

initializeForm: function () {
Expand Down Expand Up @@ -145,7 +142,7 @@ define(
self = this;

if (this.customerProvider) {
promises = [this.resourceProvider.createResource(), this.customerProvider.updateCustomer()];
promises = [this.resourceProvider.createResource(), this.customerProvider.createCustomer()];
} else {
promises = [this.resourceProvider.createResource()];
}
Expand All @@ -160,6 +157,9 @@ define(
Promise.all(promises).then(
function (values) {
self.resourceId = values[0].id;
if (self.customer() && values[1]) {
self.customer().id = values[1].id
}

placeOrderAction(self.getData(), self.messageContainer)
.done(function () {
Expand Down

0 comments on commit 352b2d9

Please sign in to comment.