Skip to content

Commit

Permalink
Release 4.3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldlineConnect authored and jenkins committed Aug 28, 2024
1 parent c5b7275 commit 4e05fe4
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 160 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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).

## 4.3.0 - 2024-08-28

- Use billing address instead or order for customer information

## 4.2.0 - 2024-07-26

- Add support for the following payment products:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order;

use Magento\Sales\Api\Data\OrderAddressInterface;
use Worldline\Connect\Helper\Format;
use Worldline\Connect\Sdk\V1\Domain\AddressPersonal;
use Worldline\Connect\Sdk\V1\Domain\AddressPersonalFactory;

use function array_key_exists;

class AddressPersonalBuilder
{
public function __construct(
private readonly AddressPersonalFactory $addressPersonalFactory,
private readonly Format $format,
) {
}

public function build(OrderAddressInterface $orderAddress): AddressPersonal
{
$addressPersonal = $this->addressPersonalFactory->create();
$addressPersonal->city = $this->format->limit($orderAddress->getCity(), 40);
$addressPersonal->countryCode = $orderAddress->getCountryId();
$addressPersonal->state = $orderAddress->getRegion();
$addressPersonal->zip = $orderAddress->getPostcode();

$street = $orderAddress->getStreet();
if ($street !== null) {
$addressPersonal->street = $this->format->limit(array_key_exists(0, $street) ? $street[0] : '', 50);
$addressPersonal->houseNumber = $this->format->limit(array_key_exists(1, $street) ? $street[1] : '', 15);
$addressPersonal->additionalInfo = $this->format->limit(array_key_exists(2, $street) ? $street[2] : '', 50);
}

return $addressPersonal;
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
<?php

declare(strict_types=1);

namespace Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer;

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Worldline\Connect\Helper\Format;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\AbstractAddressBuilder;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\AddressPersonalBuilder;
use Worldline\Connect\Sdk\V1\Domain\AddressPersonal;
use Worldline\Connect\Sdk\V1\Domain\AddressPersonalFactory;

class AddressBuilder extends AbstractAddressBuilder
{
/**
* @var AddressPersonalFactory
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $addressPersonalFactory;

public function __construct(Format $format, AddressPersonalFactory $addressPersonalFactory)
{
parent::__construct($format);

$this->addressPersonalFactory = $addressPersonalFactory;
}

public function create(OrderInterface $order): AddressPersonal
{
$addressPersonal = $this->addressPersonalFactory->create();

try {
$billingAddress = $this->getBillingAddressFromOrder($order);
$this->populateAddress($addressPersonal, $billingAddress);
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
} catch (LocalizedException $e) {
//Do nothing
}
use function __;

return $addressPersonal;
class AddressBuilder
{
public function __construct(
private readonly AddressPersonalBuilder $addressPersonalBuilder,
) {
}

/**
* @throws LocalizedException
*/
public function getBillingAddressFromOrder(OrderInterface $order): OrderAddressInterface
public function create(OrderInterface $order): AddressPersonal
{
$billingAddress = $order->getBillingAddress();
if ($billingAddress === null) {
// phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
throw new LocalizedException(__('No billing address available for this order'));
throw new LocalizedException(__('No shipping address available for this order'));
}
return $billingAddress;

return $this->addressPersonalBuilder->build($billingAddress);
}
}
87 changes: 24 additions & 63 deletions Model/Worldline/RequestBuilder/Common/Order/CustomerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address;
use Worldline\Connect\Helper\Format;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer\AccountBuilder;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer\AddressBuilder;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer\AddressBuilder as BillingAddressBuilder;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer\CompanyInformationBuilder;
use Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Customer\DeviceBuilder;
use Worldline\Connect\Sdk\V1\Domain\CompanyInformationFactory;
Expand All @@ -18,7 +18,6 @@
use Worldline\Connect\Sdk\V1\Domain\CustomerFactory;
use Worldline\Connect\Sdk\V1\Domain\PersonalInformation;
use Worldline\Connect\Sdk\V1\Domain\PersonalInformationFactory;
use Worldline\Connect\Sdk\V1\Domain\PersonalNameFactory;

use function rand;

Expand All @@ -28,8 +27,6 @@
class CustomerBuilder
{
public const EMAIL_MESSAGE_TYPE = 'html';
public const GENDER_MALE = 0;
public const GENDER_FEMALE = 1;
public const ACCOUNT_TYPE_NONE = 'none';
public const ACCOUNT_TYPE_EXISTING = 'existing';

Expand Down Expand Up @@ -57,18 +54,6 @@ class CustomerBuilder
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $contactDetailsFactory;

/**
* @var PersonalNameFactory
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $personalNameFactory;

/**
* @var TimezoneInterface
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $timezone;

/**
* @var AccountBuilder
*/
Expand All @@ -88,17 +73,22 @@ class CustomerBuilder
private $companyInformationBuilder;

/**
* @var AddressBuilder
* @var BillingAddressBuilder
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $billingAddressBuilder;

/**
* @var PersonalNameBuilder
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $addressBuilder;
private $nameBuilder;

/**
* @var Format
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
private $format;

/**
* @var ResolverInterface
*/
Expand All @@ -110,35 +100,35 @@ public function __construct(
PersonalInformationFactory $personalInformationFactory,
CompanyInformationFactory $companyInformationFactory,
ContactDetailsFactory $contactDetailsFactory,
PersonalNameFactory $personalNameFactory,
AddressBuilder $addressBuilder,
BillingAddressBuilder $billingAddressBuilder,
AccountBuilder $accountBuilder,
DeviceBuilder $deviceBuilder,
CompanyInformationBuilder $companyInformationBuilder,
TimezoneInterface $timezone,
PersonalNameBuilder $nameBuilder,
Format $format,
ResolverInterface $resolver
) {
$this->customerFactory = $customerFactory;
$this->personalInformationFactory = $personalInformationFactory;
$this->companyInformationFactory = $companyInformationFactory;
$this->contactDetailsFactory = $contactDetailsFactory;
$this->personalNameFactory = $personalNameFactory;
$this->accountBuilder = $accountBuilder;
$this->deviceBuilder = $deviceBuilder;
$this->timezone = $timezone;
$this->companyInformationBuilder = $companyInformationBuilder;
$this->addressBuilder = $addressBuilder;
$this->nameBuilder = $nameBuilder;
$this->billingAddressBuilder = $billingAddressBuilder;
$this->format = $format;
$this->resolver = $resolver;
}

/**
* @throws LocalizedException
*/
public function create(Order $order): Customer
{
$worldlineCustomer = $this->customerFactory->create();
$worldlineCustomer->locale = $this->resolver->getLocale();

$worldlineCustomer->personalInformation = $this->getPersonalInformation($order);
$worldlineCustomer->merchantCustomerId = $this->format->limit(
(string) $order->getCustomerId() ?: rand(100000, 999999),
15
Expand All @@ -151,60 +141,31 @@ public function create(Order $order): Customer
$companyInformation = $this->companyInformationFactory->create();
$companyInformation->name = $billing->getCompany();
$worldlineCustomer->companyInformation = $companyInformation;

$worldlineCustomer->contactDetails = $this->getContactDetails($order, $billing);
$worldlineCustomer->personalInformation = $this->getPersonalInformation($billing);
$worldlineCustomer->contactDetails = $this->getContactDetails($billing);
}

$worldlineCustomer->account = $this->accountBuilder->create($order);
$worldlineCustomer->device = $this->deviceBuilder->create($order);
$worldlineCustomer->accountType = $this->getAccountType($order);
$worldlineCustomer->companyInformation = $this->companyInformationBuilder->create($order);
$worldlineCustomer->billingAddress = $this->addressBuilder->create($order);
$worldlineCustomer->billingAddress = $this->billingAddressBuilder->create($order);

return $worldlineCustomer;
}

private function getPersonalInformation(Order $order): PersonalInformation
private function getPersonalInformation(Address $billing): PersonalInformation
{
$personalInformation = $this->personalInformationFactory->create();

$personalName = $this->personalNameFactory->create();
$personalName->title = $order->getCustomerPrefix();
$personalName->firstName = $this->format->limit($order->getCustomerFirstname(), 15);
$personalName->surnamePrefix = $order->getCustomerMiddlename();
$personalName->surname = $this->format->limit($order->getCustomerLastname(), 35);

$personalInformation->name = $personalName;
$personalInformation->gender = $this->getCustomerGender($order);
$personalInformation->dateOfBirth = $this->getDateOfBirth($order);
$personalInformation->name = $this->nameBuilder->create($billing);

return $personalInformation;
}

private function getCustomerGender(Order $order): string
{
return match ($order->getCustomerGender()) {
self::GENDER_MALE => 'male',
self::GENDER_FEMALE => 'female',
default => 'unknown',
};
}

private function getDateOfBirth(Order $order): string
{
$dateOfBirth = '';
if ($order->getCustomerDob()) {
$doBObject = $this->timezone->date($order->getCustomerDob());
$dateOfBirth = $doBObject->format('Ymd');
}

return $dateOfBirth;
}

private function getContactDetails(Order $order, Address $billing): ContactDetails
private function getContactDetails(Address $billing): ContactDetails
{
$contactDetails = $this->contactDetailsFactory->create();
$contactDetails->emailAddress = $this->format->limit($order->getCustomerEmail(), 70);
$contactDetails->emailAddress = $this->format->limit($billing->getEmail(), 70);
$contactDetails->emailMessageType = self::EMAIL_MESSAGE_TYPE;
$contactDetails->phoneNumber = $billing->getTelephone();
$contactDetails->faxNumber = $billing->getFax();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order\Shipping\Address;
namespace Worldline\Connect\Model\Worldline\RequestBuilder\Common\Order;

use Magento\Sales\Api\Data\OrderAddressInterface;
use Worldline\Connect\Helper\Format;
use Worldline\Connect\Sdk\V1\Domain\PersonalName;
use Worldline\Connect\Sdk\V1\Domain\PersonalNameFactory;

class NameBuilder
class PersonalNameBuilder
{
public function __construct(
private readonly PersonalNameFactory $personalNameFactory,
Expand All @@ -24,6 +24,7 @@ public function create(OrderAddressInterface $address): PersonalName
$personalName->surname = $this->format->limit($address->getLastname(), 70);
$personalName->surnamePrefix = $address->getMiddlename();
$personalName->title = $address->getPrefix();

return $personalName;
}
}
Loading

0 comments on commit 4e05fe4

Please sign in to comment.