Skip to content

Commit

Permalink
PAYOSWXP-132: always order-line items if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Jun 17, 2024
1 parent bd02f8e commit f0fd63b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 40 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 @@ -22,6 +22,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 Down Expand Up @@ -68,33 +69,39 @@ public function getRequestParameter(AbstractRequestParameterStruct $arguments):

public function supports(AbstractRequestParameterStruct $arguments): bool
{
if (!($arguments instanceof FinancialTransactionStruct)) {
return false;
}

$paymentMethod = $arguments->getPaymentMethod();
if ($arguments instanceof PaymentTransactionStruct) {
$config = $this->serviceAccessor->configReader->read($arguments->getPaymentTransaction()->getOrder()->getSalesChannelId());

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:
if (\in_array($arguments->getAction(), [self::REQUEST_ACTION_AUTHORIZE, self::REQUEST_ACTION_PREAUTHORIZE], true)
&& $config->get('submitOrderLineItems', false)
) {
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;
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
) {
}
}
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 DMS\PHPUnitExtensions\ArraySubset\Assert;
use PayonePayment\Components\ConfigReader\ConfigReader;
use PayonePayment\Components\Hydrator\LineItemHydrator\LineItemHydrator;
use PayonePayment\Constants;
use PayonePayment\PaymentHandler\PayoneBancontactPaymentHandler;
Expand All @@ -22,6 +23,8 @@
use PayonePayment\TestCaseBase\PayoneTestBehavior;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use stdClass;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
Expand Down Expand Up @@ -65,19 +68,6 @@ public function testItNotSupportsInvalidPaymentMethods($paymentHandler): void
static::assertFalse($builder->supports($struct));
}

public function testItNotSupportsPaymentRequests(): void
{
$struct = $this->getPaymentTransactionStruct(
new RequestDataBag([]),
PayonePayolutionDebitPaymentHandler::class,
AbstractRequestParameterBuilder::REQUEST_ACTION_AUTHORIZE
);

$builder = $this->getContainer()->get(OrderLinesRequestParameterBuilder::class);

static::assertFalse($builder->supports($struct));
}

public function testItAddsCorrectOrderLineParametersForFullCapture(): void
{
$dataBag = new ParameterBag();
Expand Down Expand Up @@ -254,6 +244,60 @@ public function testItAddsCorrectOrderLineParametersForPartialRefund(): void
static::assertArrayNotHasKey('it[2]', $parameters);
}

public function testItNotSendItemsIfDisabledForOptionalMethods(): void
{
$configService = $this->getContainer()->get(SystemConfigService::class);
$builder = $this->getContainer()->get(OrderLinesRequestParameterBuilder::class);

$struct = $this->getPaymentTransactionStruct(
new RequestDataBag(),
stdClass::class,
AbstractRequestParameterBuilder::REQUEST_ACTION_PREAUTHORIZE
);

$configService->set(ConfigReader::SYSTEM_CONFIG_DOMAIN . 'submitOrderLineItems', false, $struct->getPaymentTransaction()->getOrder()->getSalesChannelId());
static::assertFalse($builder->supports($struct), 'builder should not supports stdclass if configuration (send order items) is disabled.');
}

public function testItSendItemsIfEnabledForOptionalMethods(): void
{
$configService = $this->getContainer()->get(SystemConfigService::class);
$builder = $this->getContainer()->get(OrderLinesRequestParameterBuilder::class);

$allowedActions = [
AbstractRequestParameterBuilder::REQUEST_ACTION_PREAUTHORIZE,
AbstractRequestParameterBuilder::REQUEST_ACTION_AUTHORIZE,
];

foreach ($allowedActions as $allowedAction) {
$struct = $this->getPaymentTransactionStruct(new RequestDataBag(), stdClass::class, $allowedAction);
$configService->set(ConfigReader::SYSTEM_CONFIG_DOMAIN . 'submitOrderLineItems', true, $struct->getPaymentTransaction()->getOrder()->getSalesChannelId());
static::assertTrue($builder->supports($struct), sprintf('builder should supports stdclass if configuration (send order items) is enabled and action %s is given.', $allowedAction));
}
}

public function testItNotSendItemsIfEnabledWithDisallowedActionForOptionalMethods(): void
{
$configService = $this->getContainer()->get(SystemConfigService::class);
$builder = $this->getContainer()->get(OrderLinesRequestParameterBuilder::class);

$disallowedActions = [
AbstractRequestParameterBuilder::REQUEST_ACTION_CAPTURE,
AbstractRequestParameterBuilder::REQUEST_ACTION_REFUND,
AbstractRequestParameterBuilder::REQUEST_ACTION_GENERIC_PAYMENT,
'something-else',
];

$salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();
$order = $this->getRandomOrder($salesChannelContext);

foreach ($disallowedActions as $disallowedAction) {
$struct = $this->getPaymentTransactionStruct(new RequestDataBag(), stdClass::class, $disallowedAction);
$configService->set(ConfigReader::SYSTEM_CONFIG_DOMAIN . 'submitOrderLineItems', true, $struct->getPaymentTransaction()->getOrder()->getSalesChannelId());
static::assertFalse($builder->supports($struct), sprintf('builder should not supports stdclass if configuration (send order items) is enabled and not allowed action %s is given.', $disallowedAction));
}
}

public function getValidPaymentHandler(): array
{
return [
Expand Down

0 comments on commit f0fd63b

Please sign in to comment.