Skip to content

Commit

Permalink
Merge pull request #52 from collector-bank/feature/oath
Browse files Browse the repository at this point in the history
Preset
  • Loading branch information
kristoffer124 authored Nov 23, 2023
2 parents 85926e0 + bea1774 commit 1bf9b6d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
117 changes: 114 additions & 3 deletions src/QuoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Webbhuset\CollectorCheckout;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Webbhuset\CollectorCheckout\Config\Source\Customer\DefaultType;
use Webbhuset\CollectorCheckoutSDK\Checkout\Cart;
use Webbhuset\CollectorCheckoutSDK\Checkout\Cart\Item;
use Webbhuset\CollectorCheckoutSDK\Checkout\Customer\InitializeCustomer;
Expand All @@ -15,11 +20,26 @@ class QuoteConverter
protected $scopeConfig;
protected $configurationHelper;
protected $config;
/**
* @var CustomerRepositoryInterface
*/
private CustomerRepositoryInterface $customerRepository;
/**
* @var AddressRepositoryInterface
*/
private AddressRepositoryInterface $addressRepository;
/**
* @var Data\QuoteHandler
*/
private Data\QuoteHandler $quoteHandler;

public function __construct(
\Magento\Tax\Model\Config $taxConfig,
\Magento\Tax\Model\Calculation $taxCalculator,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Webbhuset\CollectorCheckout\Data\QuoteHandler $quoteHandler,
CustomerRepositoryInterface $customerRepository,
AddressRepositoryInterface $addressRepository,
\Magento\Catalog\Helper\Product\Configuration $configurationHelper,
\Webbhuset\CollectorCheckout\Config\QuoteConfigFactory $config
) {
Expand All @@ -28,6 +48,9 @@ public function __construct(
$this->scopeConfig = $scopeConfig;
$this->configurationHelper = $configurationHelper;
$this->config = $config;
$this->customerRepository = $customerRepository;
$this->addressRepository = $addressRepository;
$this->quoteHandler = $quoteHandler;
}

public function getCart(\Magento\Quote\Model\Quote $quote) : Cart
Expand Down Expand Up @@ -314,14 +337,18 @@ public function getInitializeCustomer(\Magento\Quote\Model\Quote $quote)
$mobilePhoneNumber = (string) $this->getMobilePhoneNumber($quote);
$nationalIdentificationNumber = (string) $this->getNationalIdentificationNumber($quote);
$postalCode = (string) $this->getPostalCode($quote);
$deliveryAddress = $this->getDeliveryAddress($quote);
$customerType = $this->quoteHandler->getCustomerType($quote) ? $this->quoteHandler->getCustomerType($quote): DefaultType::PRIVATE_CUSTOMERS;

// Email and mobile phone number are required. If we don't have both, we return null
if ($email && $mobilePhoneNumber) {
$customer = new InitializeCustomer(
$email,
$mobilePhoneNumber,
$nationalIdentificationNumber,
$postalCode
$postalCode,
$deliveryAddress,
$customerType
);

return $customer;
Expand All @@ -341,8 +368,66 @@ public function getEmail(\Magento\Quote\Model\Quote $quote)
public function getMobilePhoneNumber(\Magento\Quote\Model\Quote $quote)
{
$shippingAddress = $quote->getShippingAddress();
$quotePhoneNumber = $shippingAddress->getTelephone();
if ($quotePhoneNumber) {
return $quotePhoneNumber;
}
$defaultShippingAddressPhoneNumber = $this->getDefaultShippingAddressPhoneNumber($quote);
if (!$defaultShippingAddressPhoneNumber) {
return "";
}

return $defaultShippingAddressPhoneNumber;
}

private function getDefaultShippingAddressId(\Magento\Quote\Model\Quote $quote):?int
{
$customerId = (int) $quote->getCustomerId();
if (!$customerId) {
return null;
}
try {
$customer = $this->customerRepository->getById($customerId);
} catch (\Exception $e) {
return null;
}

return (int) $customer->getDefaultShipping();
}

private function getDefaultShippingAddress(\Magento\Quote\Model\Quote $quote)
{
$defaultAddressId = $this->getDefaultShippingAddressId($quote);
if (!$defaultAddressId) {
return null;
}
try {
$address = $this->addressRepository->getById($defaultAddressId);
} catch (LocalizedException $e) {
return null;
}

return $shippingAddress->getTelephone();
return $address;
}

private function getDefaultShippingAddressPhoneNumber(\Magento\Quote\Model\Quote $quote):?string
{
$address = $this->getDefaultShippingAddress($quote);
if (!$address) {
return null;
}

return (string) $address->getTelephone();
}

private function getDefaultShippingAddressPostCode(\Magento\Quote\Model\Quote $quote):?string
{
$address = $this->getDefaultShippingAddress($quote);
if (!$address) {
return null;
}

return (string) $address->getPostcode();
}

public function getNationalIdentificationNumber(\Magento\Quote\Model\Quote $quote)
Expand All @@ -353,8 +438,16 @@ public function getNationalIdentificationNumber(\Magento\Quote\Model\Quote $quot
public function getPostalCode(\Magento\Quote\Model\Quote $quote)
{
$shippingAddress = $quote->getShippingAddress();
$postCode = $shippingAddress->getPostcode();
if ($postCode) {
return $postCode;
}
$defaultAddressPostalCode = $this->getDefaultShippingAddressPostCode($quote);
if (!$defaultAddressPostalCode) {
return "";
}

return $shippingAddress->getPostcode();
return $defaultAddressPostalCode;
}

public function getReference(\Magento\Quote\Model\Quote $quote)
Expand Down Expand Up @@ -416,4 +509,22 @@ private function getSelectedOptionsOfQuoteItem(\Magento\Catalog\Model\Product\Co
{
return $this->configurationHelper->getCustomOptions($item);
}

public function getDeliveryAddress(\Magento\Quote\Model\Quote $quote)
{
$shippingAddress = $this->getDefaultShippingAddress($quote);
if (!$shippingAddress) {
return [];
}
return [
'firstName' => $shippingAddress->getFirstname(),
'lastName' => $shippingAddress->getLastname(),
'companyName' => $shippingAddress->getCompany(),
'address' => $shippingAddress->getStreet()[0] ?? '',
'address2' => $shippingAddress->getStreet()[1] ?? '',
'postalCode' => $shippingAddress->getPostcode(),
'city' => $shippingAddress->getCity(),
];

}
}
8 changes: 6 additions & 2 deletions src/QuoteUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ public function setCustomerData(
) : Quote {
$customer = $checkoutData->getCustomer();
$customerAddress = $customer->getInvoiceAddress();
$firstname = $customerAddress->getFirstName() ?? $customer->getFirstName();
$lastname = $customerAddress->getLastName() ?? $customer->getLastName();
$firstname = is_string($customerAddress->getFirstName()) && strlen($customerAddress->getFirstName()) > 0
? $customerAddress->getFirstName()
: $customer->getFirstName();
$lastname = is_string($customerAddress->getLastName()) && strlen($customerAddress->getLastName()) > 0
? $customerAddress->getLastName()
: $customer->getLastName();
$email = $customer->getEmail();
$countryCode = $checkoutData->getCountryCode();
$basicAddress = $quote->getShippingAddress()->setCountryId($countryCode);
Expand Down
1 change: 0 additions & 1 deletion src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@
<depends>
<field id='active'>1</field>
<field id='test_mode'>1</field>
<field id='test_mode_activeoath'>1</field>
</depends>
</field>

Expand Down

0 comments on commit 1bf9b6d

Please sign in to comment.