Skip to content

Commit

Permalink
[PAYOSWXP-113] Add B2B handling for secured invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Müller authored and rommelfreddy committed Jan 24, 2024
1 parent 0790e47 commit d392502
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DependencyInjection/payment_method_filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
<argument>AT</argument>
</argument>
<argument key="$allowedB2bCountries" type="collection">
<argument>DE</argument>
<argument>AT</argument>
</argument>
<argument key="$allowedCurrencies" type="collection">
<argument>EUR</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d392502

Please sign in to comment.