Skip to content

Commit

Permalink
PAYOSWXP-132: always add order-line items if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Jul 10, 2024
1 parent b0a071f commit 3beee8b
Show file tree
Hide file tree
Showing 30 changed files with 215 additions and 148 deletions.
4 changes: 1 addition & 3 deletions src/DependencyInjection/requestParameter/builder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
<argument key="$serviceAccessor" type="service" id="PayonePayment\Payone\RequestParameter\Builder\RequestBuilderServiceAccessor" />
</service>

<service id="PayonePayment\Payone\RequestParameter\Builder\RequestBuilderServiceAccessor">
<service id="PayonePayment\Payone\RequestParameter\Builder\RequestBuilderServiceAccessor" autowire="true">
<argument key="$customerRepository" type="service" id="customer.repository"/>
<argument key="$currencyRepository" type="service" id="currency.repository"/>
<argument key="$customerAddressRepository" type="service" id="customer_address.repository"/>
<argument key="$orderAddressRepository" type="service" id="order_address.repository"/>
<argument key="$currencyPrecision" type="service" id="PayonePayment\Components\Currency\CurrencyPrecisionInterface"/>
<argument key="$lineItemHydrator" type="service" id="PayonePayment\Components\Hydrator\LineItemHydrator\LineItemHydratorInterface"/>
</service>

<service id="PayonePayment\Payone\RequestParameter\Builder\GeneralTransactionRequestParameterBuilder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
{
$dataBag = $arguments->getRequestData();

$parameter = [
return [
'request' => $arguments->getAction(),
'clearingtype' => self::CLEARING_TYPE_FINANCING,
'add_paydata[authorization_token]' => $dataBag->get('payoneKlarnaAuthorizationToken'),
];

$context = $arguments->getSalesChannelContext()->getContext();
$order = $arguments->getPaymentTransaction()->getOrder();
$currency = $this->getOrderCurrency($order, $context);
$lineItems = $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context);

return array_merge($parameter, $lineItems);
}

public function supports(AbstractRequestParameterStruct $arguments): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,10 @@ class AuthorizeRequestParameterBuilder extends AbstractRequestParameterBuilder
*/
public function getRequestParameter(AbstractRequestParameterStruct $arguments): array
{
$paymentTransaction = $arguments->getPaymentTransaction();
$salesChannelContext = $arguments->getSalesChannelContext();
$context = $salesChannelContext->getContext();
$order = $paymentTransaction->getOrder();
$currency = $this->getOrderCurrency($order, $salesChannelContext->getContext());

$parameters = [
return [
'clearingtype' => self::CLEARING_TYPE_INVOICE,
'request' => self::REQUEST_ACTION_AUTHORIZE,
'request' => $arguments->getAction(),
];

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

return $parameters;
}

public function supports(AbstractRequestParameterStruct $arguments): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PayonePayment\Payone\RequestParameter\Builder;

use PayonePayment\PaymentHandler\AbstractKlarnaPaymentHandler;
use PayonePayment\PaymentHandler\AbstractPostfinancePaymentHandler;
use PayonePayment\PaymentHandler\PayoneAlipayPaymentHandler;
use PayonePayment\PaymentHandler\PayoneBancontactPaymentHandler;
Expand All @@ -22,6 +23,7 @@
use PayonePayment\PaymentHandler\PayoneWeChatPayPaymentHandler;
use PayonePayment\Payone\RequestParameter\Struct\AbstractRequestParameterStruct;
use PayonePayment\Payone\RequestParameter\Struct\FinancialTransactionStruct;
use PayonePayment\Payone\RequestParameter\Struct\PaymentTransactionStruct;

class OrderLinesRequestParameterBuilder extends AbstractRequestParameterBuilder
{
Expand All @@ -41,12 +43,16 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
return [];
}

$parameters = $this->serviceAccessor->lineItemHydrator->mapPayoneOrderLinesByRequest(
$currency,
$paymentTransaction->getOrder(),
$orderLines,
$isCompleted ? true : $includeShippingCosts
);
if ($arguments instanceof PaymentTransactionStruct && $this->isAuthorizeAction($arguments) && $orderLines === []) {
$parameters = $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $paymentTransaction->getOrder(), $arguments->getSalesChannelContext()->getContext());
} else {
$parameters = $this->serviceAccessor->lineItemHydrator->mapPayoneOrderLinesByRequest(
$currency,
$paymentTransaction->getOrder(),
$orderLines,
$isCompleted ? true : $includeShippingCosts
);
}

// For specific payment methods the "pr" parameter must be negative on refunds
$paymentMethodsThatRequireNegativePriceForRefunds = [
Expand All @@ -68,35 +74,65 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):

public function supports(AbstractRequestParameterStruct $arguments): bool
{
if (!($arguments instanceof FinancialTransactionStruct)) {
return false;
}
if ($arguments instanceof PaymentTransactionStruct) {
$config = $this->serviceAccessor->configReader->read($arguments->getPaymentTransaction()->getOrder()->getSalesChannelId());

if ($this->isAuthorizeAction($arguments) && $config->get('submitOrderLineItems', false)) {
return true;
}

$paymentMethod = $arguments->getPaymentMethod();

switch ($paymentMethod) {
case PayonePayolutionDebitPaymentHandler::class:
case PayonePayolutionInstallmentPaymentHandler::class:
case PayonePayolutionInvoicingPaymentHandler::class:
case PayoneSecureInvoicePaymentHandler::class:
case PayoneOpenInvoicePaymentHandler::class:
case PayoneBancontactPaymentHandler::class:
case PayoneRatepayDebitPaymentHandler::class:
case PayoneRatepayInstallmentPaymentHandler::class:
case PayoneRatepayInvoicingPaymentHandler::class:
case PayonePrzelewy24PaymentHandler::class:
case PayoneWeChatPayPaymentHandler::class:
case PayoneAlipayPaymentHandler::class:
case PayoneSecuredInvoicePaymentHandler::class:
case PayoneSecuredInstallmentPaymentHandler::class:
case PayoneSecuredDirectDebitPaymentHandler::class:
// for some methods the line items are always required
if (is_subclass_of($arguments->getPaymentMethod(), AbstractKlarnaPaymentHandler::class)) {
return true;
}

// for some methods the line items are always required
switch ($arguments->getPaymentMethod()) {
case PayoneOpenInvoicePaymentHandler::class:
case PayoneRatepayDebitPaymentHandler::class:
case PayoneRatepayInstallmentPaymentHandler::class:
case PayoneRatepayInvoicingPaymentHandler::class:
case PayoneSecuredDirectDebitPaymentHandler::class:
case PayoneSecuredInstallmentPaymentHandler::class:
case PayoneSecuredInvoicePaymentHandler::class:
case PayoneSecureInvoicePaymentHandler::class:
case PayonePayolutionDebitPaymentHandler::class:
case PayonePayolutionInstallmentPaymentHandler::class:
case PayonePayolutionInvoicingPaymentHandler::class:
return true;
}
}

if (is_subclass_of($arguments->getPaymentMethod(), AbstractPostfinancePaymentHandler::class)) {
return true;
if ($arguments instanceof FinancialTransactionStruct) {
switch ($arguments->getPaymentMethod()) {
case PayonePayolutionDebitPaymentHandler::class:
case PayonePayolutionInstallmentPaymentHandler::class:
case PayonePayolutionInvoicingPaymentHandler::class:
case PayoneSecureInvoicePaymentHandler::class:
case PayoneOpenInvoicePaymentHandler::class:
case PayoneBancontactPaymentHandler::class:
case PayoneRatepayDebitPaymentHandler::class:
case PayoneRatepayInstallmentPaymentHandler::class:
case PayoneRatepayInvoicingPaymentHandler::class:
case PayonePrzelewy24PaymentHandler::class:
case PayoneWeChatPayPaymentHandler::class:
case PayoneAlipayPaymentHandler::class:
case PayoneSecuredInvoicePaymentHandler::class:
case PayoneSecuredInstallmentPaymentHandler::class:
case PayoneSecuredDirectDebitPaymentHandler::class:
return true;
}

if (is_subclass_of($arguments->getPaymentMethod(), AbstractPostfinancePaymentHandler::class)) {
return true;
}
}

return false;
}

private function isAuthorizeAction(AbstractRequestParameterStruct $arguments): bool
{
return \in_array($arguments->getAction(), [self::REQUEST_ACTION_AUTHORIZE, self::REQUEST_ACTION_PREAUTHORIZE], true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
$context = $salesChannelContext->getContext();
$paymentTransaction = $arguments->getPaymentTransaction();
$order = $this->getOrder($paymentTransaction->getOrder()->getId(), $context);
$currency = $this->getOrderCurrency($order, $context);
$profile = $this->getProfile($order, PayoneRatepayDebitPaymentHandler::class);

$parameters = [
Expand All @@ -54,10 +53,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

return $parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

return $parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
$context = $salesChannelContext->getContext();
$paymentTransaction = $arguments->getPaymentTransaction();
$order = $this->getOrder($paymentTransaction->getOrder()->getId(), $context);
$currency = $this->getOrderCurrency($order, $context);
$profile = $this->getProfile($order, PayoneRatepayInvoicingPaymentHandler::class);

$parameters = [
Expand All @@ -37,10 +36,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

return $parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PayonePayment\Payone\RequestParameter\Builder;

use PayonePayment\Components\ConfigReader\ConfigReader;
use PayonePayment\Components\Currency\CurrencyPrecisionInterface;
use PayonePayment\Components\Hydrator\LineItemHydrator\LineItemHydratorInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand All @@ -16,7 +17,8 @@ public function __construct(
public readonly EntityRepository $customerAddressRepository,
public readonly EntityRepository $currencyRepository,
public readonly CurrencyPrecisionInterface $currencyPrecision,
public readonly LineItemHydratorInterface $lineItemHydrator
public readonly LineItemHydratorInterface $lineItemHydrator,
public readonly ConfigReader $configReader
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,11 @@ class AuthorizeRequestParameterBuilder extends AbstractRequestParameterBuilder
*/
public function getRequestParameter(AbstractRequestParameterStruct $arguments): array
{
$paymentTransaction = $arguments->getPaymentTransaction();
$salesChannelContext = $arguments->getSalesChannelContext();
$context = $salesChannelContext->getContext();
$order = $paymentTransaction->getOrder();
$currency = $this->getOrderCurrency($order, $context);

$parameters = [
return [
'clearingtype' => self::CLEARING_TYPE_INVOICE,
'clearingsubtype' => 'POV',
'request' => self::REQUEST_ACTION_AUTHORIZE,
];

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

return $parameters;
}

public function supports(AbstractRequestParameterStruct $arguments): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
'iban' => $dataBag->get('securedDirectDebitIban'),
];

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
'add_paydata[installment_option_id]' => $dataBag->get('securedInstallmentOptionId'),
];

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):
'currency' => $currency->getIsoCode(),
];

if ($order->getLineItems() !== null) {
$parameters = array_merge($parameters, $this->serviceAccessor->lineItemHydrator->mapOrderLines($currency, $order, $context));
}

$this->applyPhoneParameter($order, $parameters, $dataBag, $context);
$this->applyBirthdayParameter($order, $parameters, $dataBag, $context);
$this->applyB2bParameters($order, $parameters);
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
<helpText>When activated, the data entered by the customer into the fields provided by PAYONE will be saved in the customer account, allowing them to be directly reused for future orders.</helpText>
<helpText lang="de-DE">Wenn aktiviert, werden die Daten, welche der Kunde in die von PAYONE bereitgestellten Felder, eingibt, in den Kundenaccount gespeichert, sodass diese bei weiteren Bestellungen direkt wiederverwendet werden können.</helpText>
</input-field>

<input-field type="bool">
<name>submitOrderLineItems</name>
<label>Send order line-items to gateway</label>
<label lang="de-DE">Bestellpositionen übermitteln</label>
<helpText>If activated, all order line-items are submitted to the PAYONE gateway. This enables the generation of invoices through PAYONE.</helpText>
<helpText lang="de-DE">Wenn aktiviert, werden alle Bestellpositionen an das PAYONE-Gateway übermittelt. Dadurch ist es möglich, Rechnungen direkt über PAYONE zu erstellen.</helpText>
</input-field>
</card>

<card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PayonePayment\Payone\RequestParameter\Builder;

use PayonePayment\Components\CartHasher\CartHasherInterface;
use PayonePayment\Components\ConfigReader\ConfigReader;
use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Components\Currency\CurrencyPrecisionInterface;
use PayonePayment\Components\Hydrator\LineItemHydrator\LineItemHydratorInterface;
Expand Down Expand Up @@ -88,7 +89,8 @@ public function testItAddsCorrectAmountAndCurrency(): void
$this->getContainer()->get('customer_address.repository'),
$this->getContainer()->get('currency.repository'),
$currencyPrecision,
$this->getContainer()->get(LineItemHydratorInterface::class)
$this->getContainer()->get(LineItemHydratorInterface::class),
$this->getContainer()->get(ConfigReader::class)
),
$this->createMock(CartHasherInterface::class),
$this->createMock(ConfigReaderInterface::class)
Expand Down
13 changes: 0 additions & 13 deletions tests/Payone/RequestParameter/Builder/Klarna/AbstractKlarna.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PayonePayment\Payone\RequestParameter\Builder\Klarna;

use PayonePayment\Components\Hydrator\LineItemHydrator\LineItemHydrator;
use PayonePayment\PaymentHandler\PayoneKlarnaDirectDebitPaymentHandler;
use PayonePayment\PaymentHandler\PayoneKlarnaInstallmentPaymentHandler;
use PayonePayment\PaymentHandler\PayoneKlarnaInvoicePaymentHandler;
Expand Down Expand Up @@ -37,18 +36,6 @@ final public function testItSupportsOnlyKlarnaMethods(): void

abstract protected function getStructForTestingSupportMethod(string $paymentHandler): AbstractRequestParameterStruct;

protected function assertLineItemHasBeenSet(array $parameters, int $index = 1): void
{
// just verify if the keys exists. Tests for the contents, will be performed by testing the line-item-hydrator
$indexStr = "[$index]";
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_NAME . $indexStr, $parameters);
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_NUMBER . $indexStr, $parameters);
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_PRICE . $indexStr, $parameters);
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_QTY . $indexStr, $parameters);
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_TAX_RATE . $indexStr, $parameters);
static::assertArrayHasKey(LineItemHydrator::PAYONE_ARRAY_KEY_TYPE . $indexStr, $parameters);
}

private function getClassToTest(): string
{
$class = preg_replace('/Test$/', '', static::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function testItAddsCorrectAuthorizeParameters(): void
static::assertEquals('test-token', $parameters['add_paydata[authorization_token]']);

static::assertArrayHasKey('clearingtype', $parameters);

$this->assertLineItemHasBeenSet($parameters);
}

protected function getStructForTestingSupportMethod(string $paymentHandler): AbstractRequestParameterStruct
Expand Down
Loading

0 comments on commit 3beee8b

Please sign in to comment.