Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring supported gateways instead of hardcoding them #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### UPGRADE FROM 0.10.1 TO 1.0.0-rc.1

1. `OfflineRefundPaymentMethodsProvider` renamed to `SupportedRefundPaymentMethodsProvider` with the supported gateways array as the 2nd argument
(by default only `offline` gateway is passed and therefore supported).

### UPGRADE FROM 0.8.0 TO 0.9.0

1. Removed ``CreditMemoChannel`` and replaced by ``Sylius\Component\Core\Model\ChannelInterface``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface;

final class OfflineRefundPaymentMethodsProviderSpec extends ObjectBehavior
final class SupportedRefundPaymentMethodsProviderSpec extends ObjectBehavior
{
function let(PaymentMethodRepositoryInterface $paymentMethodRepository): void
{
$this->beConstructedWith($paymentMethodRepository);
$this->beConstructedWith($paymentMethodRepository, ['offline', 'stripe']);
}

function it_implements_refund_payment_methods_provider_interface(): void
{
$this->shouldImplement(RefundPaymentMethodsProviderInterface::class);
}

function it_provides_only_offline_payment_methods(
function it_provides_only_supported_payment_methods(
PaymentMethodRepositoryInterface $paymentMethodRepository,
ChannelInterface $channel,
PaymentMethodInterface $offlinePaymentMethod,
Expand All @@ -48,6 +48,6 @@ function it_provides_only_offline_payment_methods(
$stripePaymentMethod->getGatewayConfig()->willReturn($stripeGatewayConfig);
$stripeGatewayConfig->getFactoryName()->willReturn('stripe');

$this->findForChannel($channel)->shouldReturn([$offlinePaymentMethod]);
$this->findForChannel($channel)->shouldReturn([$offlinePaymentMethod, $stripePaymentMethod]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Webmozart\Assert\Assert;

final class OfflineRefundPaymentMethodsProvider implements RefundPaymentMethodsProviderInterface
final class SupportedRefundPaymentMethodsProvider implements RefundPaymentMethodsProviderInterface
{
/** @var PaymentMethodRepositoryInterface */
private $paymentMethodRepository;

public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository)
/** @var array|string[] */
private $supportedGateways;

public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository, array $supportedGateways)
{
$this->paymentMethodRepository = $paymentMethodRepository;
$this->supportedGateways = $supportedGateways;
}

public function findForChannel(ChannelInterface $channel): array
{
return array_filter(
return array_values(array_filter(
$this->paymentMethodRepository->findEnabledForChannel($channel),
function (PaymentMethodInterface $paymentMethod): bool {
Assert::notNull($paymentMethod->getGatewayConfig());
$gatewayConfig = $paymentMethod->getGatewayConfig();
Assert::notNull($gatewayConfig);

return $paymentMethod->getGatewayConfig()->getFactoryName() === 'offline';
return in_array($gatewayConfig->getFactoryName(), $this->supportedGateways, true);
}
);
));
}
}
16 changes: 14 additions & 2 deletions src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="sylius_refund.supported_gateways" type="collection">
<parameter>offline</parameter>
</parameter>
</parameters>
<services>
<defaults autowire="false" autoconfigure="false" public="true" />

Expand Down Expand Up @@ -28,12 +33,19 @@
<argument type="service" id="sylius_refund.repository.refund" />
</service>

<service id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface" class="Sylius\RefundPlugin\Provider\DefaultRelatedPaymentIdProvider">
<service
id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface"
class="Sylius\RefundPlugin\Provider\DefaultRelatedPaymentIdProvider"
>
<argument type="service" id="sylius.repository.order" />
</service>

<service id="Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface" class="Sylius\RefundPlugin\Provider\OfflineRefundPaymentMethodsProvider">
<service
id="Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface"
class="Sylius\RefundPlugin\Provider\SupportedRefundPaymentMethodsProvider"
>
<argument type="service" id="sylius.repository.payment_method" />
<argument>%sylius_refund.supported_gateways%</argument>
</service>
</services>
</container>