Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion Block/Checkout/Success/AdditionalPaymentInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Unzer\PAPI\Model\Command\AbstractCommand;
use Unzer\PAPI\Model\Config;
use Unzer\PAPI\Model\Method\Base;

/**
Expand All @@ -26,18 +28,25 @@ class AdditionalPaymentInformation extends Template
*/
protected ?Session $_checkoutSession = null;

/**
* @var Config
*/
protected Config $_config;

/**
* AdditionalPaymentInformation constructor.
*
* @param Context $context
* @param Session $checkoutSession
* @param Config $config
* @param array $data
*/
public function __construct(Context $context, Session $checkoutSession, array $data = [])
public function __construct(Context $context, Session $checkoutSession, Config $config, array $data = [])
{
parent::__construct($context, $data);

$this->_checkoutSession = $checkoutSession;
$this->_config = $config;
}

/**
Expand All @@ -60,4 +69,40 @@ public function getAdditionalPaymentInformation(): ?string

return $methodInstance->getAdditionalPaymentInformation($order);
}

/**
* Returns the Unzer Payment ID of the placed order, only in sandbox (test) mode.
*
* @return string|null
* @throws LocalizedException
*/
public function getUnzerPaymentId(): ?string
{
$order = $this->_checkoutSession->getLastRealOrder();
$payment = $order->getPayment();

if ($payment === null) {
return null;
}

$methodInstance = $payment->getMethodInstance();

if (!$methodInstance instanceof Base) {
return null;
}

$storeId = $order->getStoreId() !== null ? (string)$order->getStoreId() : null;

if (!$this->_config->isSandboxMode($storeId, $methodInstance)) {
return null;
}

$paymentId = $payment->getAdditionalInformation(AbstractCommand::KEY_PAYMENT_ID);

if (!is_string($paymentId) || $paymentId === '') {
return null;
}

return $paymentId;
}
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ 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.1.0](https://github.com/unzerdev/magento2/compare/4.0.6..4.1.0)
### Changed
* Add compatibility with Magento 2.4.9 and PHP 8.5
* Refactor the order status logic for Direct Bank Transfer
* Update metadata payloads
* Fix B2B customer creation for non-UPL payment methods
* Add state mapping for billing and shipping addresses
* Fix Unzer customer resolution for saved COF payments
* Enhance UI Components v2
* Update the company name after editing billing address
* Fetch merchant configuration using the public key
* Improve whenDefined usage

## [4.0.6](https://github.com/unzerdev/magento2/compare/4.0.5..4.0.6)
### Changed
* Handle rounding differences in basket calculations using 4-decimal precision
Expand Down
100 changes: 80 additions & 20 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Tax\Model\Config as MagentoTaxConfig;
use Unzer\PAPI\Block\System\Config\Form\Field\BirthDateFactory;
use Unzer\PAPI\Model\PluginResolver;
use Unzer\PAPI\Model\Config;
use Unzer\PAPI\Model\Source\CreateThreatMetrixId;
use Unzer\PAPI\Model\Source\Customer as CustomerResource;
use UnzerSDK\Constants\BasketItemTypes;
use UnzerSDK\Constants\CompanyCommercialSectorItems;
use UnzerSDK\Constants\CompanyRegistrationTypes;
use UnzerSDK\Constants\CompanyTypes;
use UnzerSDK\Constants\Salutations;
use UnzerSDK\Constants\ShippingTypes;
use UnzerSDK\Exceptions\UnzerApiException;
Expand Down Expand Up @@ -91,6 +95,12 @@ class Order
*/
private ResolverInterface $localeResolver;

/**
* @var PluginResolver
*/
private PluginResolver $pluginResolver;


/**
* Constructor
*
Expand All @@ -103,6 +113,7 @@ class Order
* @param BirthDateFactory $birthDateFactory
* @param CreateThreatMetrixId $createThreatMetrixId
* @param ResolverInterface $localeResolver
* @param PluginResolver $pluginResolver
*/
public function __construct(
Config $moduleConfig,
Expand All @@ -113,7 +124,8 @@ public function __construct(
BasketItemFactory $basketItemFactory,
BirthDateFactory $birthDateFactory,
CreateThreatMetrixId $createThreatMetrixId,
ResolverInterface $localeResolver
ResolverInterface $localeResolver,
PluginResolver $pluginResolver
) {
$this->_moduleConfig = $moduleConfig;
$this->_moduleList = $moduleList;
Expand All @@ -124,6 +136,7 @@ public function __construct(
$this->birthDateFactory = $birthDateFactory;
$this->createThreatMetrixId = $createThreatMetrixId;
$this->localeResolver = $localeResolver;
$this->pluginResolver = $pluginResolver;
}

/**
Expand Down Expand Up @@ -306,10 +319,17 @@ public function createMetadataForOrder(OrderModel $order): Metadata
{
$metaData = new Metadata();

$module = $this->pluginResolver->resolve(
(int) $order->getStoreId()
);

$metaData->setShopType('Magento 2')
->setShopVersion($this->_productMetadata->getVersion())
->addMetadata('pluginType', 'unzerdev/magento2')
->addMetadata('pluginVersion', $this->_moduleList->getOne('Unzer_PAPI')['setup_version']);
->addMetadata('pluginType', $module['type']);

if (isset($module['version'])) {
$metaData->addMetadata('pluginVersion', $module['version']);
}

return $metaData;
}
Expand Down Expand Up @@ -447,19 +467,8 @@ public function createCustomerFromOrder(
$this->updateGatewayAddressFromMagento($customer->getShippingAddress(), $shippingAddress, $shippingType);
}

if ($customerType && $customerType !== 'b2c') {
$companyInfo = new CompanyInfo();
$companyInfo->setCompanyType($customerType);
$companyInfo->setRegistrationType('not_registered');
$companyInfo->setFunction('OWNER');
$owner = new CompanyOwner();
$owner->setFirstname($customer->getFirstname());
$owner->setLastname($customer->getLastname());
$birthDate && $owner->setBirthdate($birthDate);
$companyInfo->setOwner($owner);

$customer->setCompanyInfo($companyInfo);
}
$company = $billingAddress !== null ? $billingAddress->getCompany() : null;
$this->applyCompanyInfo($customer, $company, $customerType, $birthDate);

return $createResource ? $client->createOrUpdateCustomer($customer) : $customer;
}
Expand Down Expand Up @@ -504,13 +513,52 @@ private function updateGatewayAddressFromMagento(
$gatewayAddress->setName($magentoAddress->getFirstname() . ' ' . $magentoAddress->getLastname());
$gatewayAddress->setCity($magentoAddress->getCity());
$gatewayAddress->setCountry($magentoAddress->getCountryId());
$state = $magentoAddress->getRegion();
if (!empty($state)) {
$gatewayAddress->setState($state);
}
$gatewayAddress->setStreet($street);
$gatewayAddress->setZip($magentoAddress->getPostcode());
$gatewayAddress->setCompany($magentoAddress->getCompany() ?: null);
if ($magentoAddress->getAddressType() === Quote\Address::ADDRESS_TYPE_SHIPPING) {
$gatewayAddress->setShippingType($shippingType);
}
}

/**
* @param Customer $customer
* @param string|null $company
* @param string|null $customerType
* @param string|null $birthDate
*/
private function applyCompanyInfo(
Customer $customer,
?string $company,
?string $customerType = null,
?string $birthDate = null
): void {
$isExplicitB2b = !empty($customerType) && $customerType !== 'b2c';

if (empty($company) && !$isExplicitB2b) {
$customer->setCompanyInfo(null);
return;
}

$companyInfo = new CompanyInfo();
$companyInfo->setCompanyType(CompanyTypes::OTHER);
$companyInfo->setRegistrationType(CompanyRegistrationTypes::REGISTRATION_TYPE_NOT_REGISTERED);
$companyInfo->setFunction('OWNER');
$companyInfo->setCommercialSector(CompanyCommercialSectorItems::OTHER);

$owner = new CompanyOwner();
$owner->setFirstname($customer->getFirstname());
$owner->setLastname($customer->getLastname());
$birthDate && $owner->setBirthdate($birthDate);
$companyInfo->setOwner($owner);

$customer->setCompanyInfo($companyInfo);
}

/**
* Get Shipping Type
*
Expand Down Expand Up @@ -577,9 +625,12 @@ public function updateGatewayCustomerFromOrder(OrderModel $order, Customer $gate
?? Salutations::UNKNOWN
);

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

$this->applyCompanyInfo($gatewayCustomer, $company, null, $this->getBirthdateFromPayment($order->getPayment()));

$this->updateGatewayAddressFromMagento(
$gatewayCustomer->getBillingAddress(),
$billingAddress
Expand Down Expand Up @@ -617,7 +668,10 @@ public function validateGatewayCustomerAgainstOrder(OrderModel $order, Customer

// Magento's getCompany() always returns a string, but the Unzer Customer Address does not, so we must make
// sure that both have the same type.
$companyValid = ($order->getBillingAddress()->getCompany() ?? '') === ($gatewayCustomer->getCompany() ?? '');
$company = $order->getBillingAddress()->getCompany() ?? '';
$companyValid = $company === ($gatewayCustomer->getCompany() ?? '');

$companyInfoValid = ($company !== '') === ($gatewayCustomer->getCompanyInfo() !== null);
$emailValid = $order->getCustomerEmail() === $gatewayCustomer->getEmail();

$billingAddressValid = $this->validateGatewayAddressAgainstOrderAddress(
Expand All @@ -634,7 +688,12 @@ public function validateGatewayCustomerAgainstOrder(OrderModel $order, Customer
);
}

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

/**
Expand All @@ -654,7 +713,8 @@ private function validateGatewayAddressAgainstOrderAddress(
return $gatewayAddress->getCity() === $magentoAddress->getCity()
&& $gatewayAddress->getCountry() === $magentoAddress->getCountryId()
&& $gatewayAddress->getStreet() === $street
&& $gatewayAddress->getZip() === $magentoAddress->getPostcode();
&& $gatewayAddress->getZip() === $magentoAddress->getPostcode()
&& ($gatewayAddress->getCompany() ?? '') === ($magentoAddress->getCompany() ?? '');
}

/**
Expand Down
Loading
Loading