From d39250295dcbba777f70dc20501b8b60088511eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20M=C3=BCller?= Date: Fri, 19 Jan 2024 19:43:55 +0100 Subject: [PATCH] [PAYOSWXP-113] Add B2B handling for secured invoice --- .../payment_method_filter.xml | 2 + .../AuthorizeRequestParameterBuilder.php | 33 +++++++++++++ .../AuthorizeRequestParameterBuilderTest.php | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/src/DependencyInjection/payment_method_filter.xml b/src/DependencyInjection/payment_method_filter.xml index 3262a75df..93bcc75a8 100644 --- a/src/DependencyInjection/payment_method_filter.xml +++ b/src/DependencyInjection/payment_method_filter.xml @@ -97,6 +97,8 @@ AT + DE + AT EUR diff --git a/src/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilder.php b/src/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilder.php index a8e954d1c..ef985fbf3 100644 --- a/src/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilder.php +++ b/src/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilder.php @@ -13,6 +13,8 @@ use PayonePayment\Payone\RequestParameter\Builder\AbstractRequestParameterBuilder; use PayonePayment\Payone\RequestParameter\Struct\AbstractRequestParameterStruct; use PayonePayment\Payone\RequestParameter\Struct\PaymentTransactionStruct; +use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressCollection; +use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity; use Shopware\Core\Checkout\Order\OrderEntity; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; @@ -55,6 +57,7 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments): $this->applyPhoneParameter($order, $parameters, $dataBag->get('securedInvoicePhone') ?? '', $context); $this->applyBirthdayParameter($order, $parameters, $dataBag->get('payoneInvoiceBirthday') ?? '', $context); + $this->applyB2bParameters($order, $parameters); return $parameters; } @@ -82,4 +85,34 @@ protected function getOrder(string $orderId, Context $context): OrderEntity return $order; } + + protected function applyB2bParameters(OrderEntity $order, array &$parameters): void + { + /** @var OrderAddressCollection $addresses */ + $addresses = $order->getAddresses(); + /** @var OrderAddressEntity $billingAddress */ + $billingAddress = $addresses->get($order->getBillingAddressId()); + + if ($billingAddress === null) { + return; + } + + $company = $billingAddress->getCompany(); + if ($company === null) { + return; + } + + $parameters['businessrelation'] = 'b2b'; + $parameters['company'] = $company; + + if ($billingAddress->getVatId()) { + $parameters['vatid'] = $billingAddress->getVatId(); + } else { + $vatIds = $order->getOrderCustomer()?->getVatIds(); + + if (\is_array($vatIds) && isset($vatIds[0])) { + $parameters['vatid'] = $vatIds[0]; + } + } + } } diff --git a/tests/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilderTest.php index c98dd989d..13fd61376 100644 --- a/tests/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/SecuredInvoice/AuthorizeRequestParameterBuilderTest.php @@ -199,6 +199,53 @@ public function testItAddsCorrectAuthorizeParametersWithSavedBirthday(): void ); } + public function testItAddsCorrectAuthorizeParametersForB2b(): void + { + $request = $this->getRequestWithSession([ + PayoneBNPLDeviceFingerprintService::SESSION_VAR_NAME => 'the-device-ident-token', + ]); + $this->getContainer()->get(RequestStack::class)->push($request); + + $dataBag = new RequestDataBag([ + 'securedInvoicePhone' => '0123456789', + 'payoneInvoiceBirthday' => '2000-01-01', + ]); + + $struct = $this->getPaymentTransactionStruct( + $dataBag, + $this->getValidPaymentHandler(), + $this->getValidRequestAction() + ); + + $builder = $this->getContainer()->get($this->getParameterBuilder()); + + // Save company and vat id on customer billing address + $this->getContainer()->get('order_address.repository')->update([ + [ + 'id' => $struct->getPaymentTransaction()->getOrder()->getBillingAddressId(), + 'company' => 'The Company', + 'vatId' => 'DE123456789', + ], + ], $struct->getSalesChannelContext()->getContext()); + + $parameters = $builder->getRequestParameter($struct); + + Assert::assertArraySubset( + [ + 'request' => $this->getValidRequestAction(), + 'clearingtype' => AbstractRequestParameterBuilder::CLEARING_TYPE_FINANCING, + 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PIV, + 'telephonenumber' => '0123456789', + 'birthday' => '20000101', + 'it[1]' => LineItemHydrator::TYPE_GOODS, + 'businessrelation' => 'b2b', + 'company' => 'The Company', + 'vatid' => 'DE123456789', + ], + $parameters + ); + } + protected function getParameterBuilder(): string { return AuthorizeRequestParameterBuilder::class;