Skip to content

Commit

Permalink
Do not show Pagolight payment methods on some circumstances
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Feb 7, 2024
1 parent 47aa195 commit af1dd4a
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 24 deletions.
25 changes: 13 additions & 12 deletions config/services/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Webgriffe\SyliusPagolightPlugin\Payum\Action\ConvertPaymentToContractAction;
use Webgriffe\SyliusPagolightPlugin\Payum\Action\NotifyAction;
use Webgriffe\SyliusPagolightPlugin\Payum\Action\StatusAction;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();
Expand All @@ -22,8 +23,8 @@
service('twig'),
service('router'),
])
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.capture'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.capture'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.capture'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.capture'])
;

$services->set('webgriffe_sylius_pagolight.payum.action.status', StatusAction::class)
Expand All @@ -32,23 +33,23 @@

$services->set('webgriffe_sylius_pagolight.payum.action.cancel', CancelAction::class)
->public()
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.cancel'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.cancel'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.cancel'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.cancel'])
;

$services->set('webgriffe_sylius_pagolight.payum.action.notify', NotifyAction::class)
->public()
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.notify'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.notify'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.notify'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.notify'])
;

$services->set('webgriffe_sylius_pagolight.payum.action.convert_payment_to_contract', ConvertPaymentToContractAction::class)
->public()
->args([
service('webgriffe_sylius_pagolight.converter.contract'),
])
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.convert_payment_to_contract'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.convert_payment_to_contract'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.convert_payment_to_contract'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.convert_payment_to_contract'])
;

$services->set('webgriffe_sylius_pagolight.payum.action.api.auth', AuthAction::class)
Expand All @@ -57,16 +58,16 @@
service('webgriffe_sylius_pagolight.client'),
service('webgriffe_sylius_pagolight.cache'),
])
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.api.auth'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.api.auth'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.api.auth'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.api.auth'])
;

$services->set('webgriffe_sylius_pagolight.payum.action.api.create_contract', CreateContractAction::class)
->public()
->args([
service('webgriffe_sylius_pagolight.client'),
])
->tag('payum.action', ['factory' => 'pagolight', 'alias' => 'payum.action.api.create_contract'])
->tag('payum.action', ['factory' => 'pagolight_pro', 'alias' => 'payum.action.api.create_contract'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.api.create_contract'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.api.create_contract'])
;
};
5 changes: 3 additions & 2 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Webgriffe\SyliusPagolightPlugin\Form\Type\SyliusPagolightGatewayConfigurationType;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe_sylius_pagolight.form.type.gateway_configuration', SyliusPagolightGatewayConfigurationType::class)
->tag('sylius.gateway_configuration_type', ['type' => 'pagolight', 'label' => 'Pagolight'])
->tag('sylius.gateway_configuration_type', ['type' => 'pagolight_pro', 'label' => 'Pagolight Pro'])
->tag('sylius.gateway_configuration_type', ['type' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'label' => 'Pagolight'])
->tag('sylius.gateway_configuration_type', ['type' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'label' => 'Pagolight Pro'])
->tag('form.type')
;
};
5 changes: 3 additions & 2 deletions config/services/gateway_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightGatewayFactory;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightProGatewayFactory;

Expand All @@ -16,8 +17,8 @@
PagolightGatewayFactory::class,
PagolightProGatewayFactory::class,
])
->tag('payum.gateway_factory_builder', ['factory' => 'pagolight'])
->tag('payum.gateway_factory_builder', ['factory' => 'pagolight_pro'])
->tag('payum.gateway_factory_builder', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE])
->tag('payum.gateway_factory_builder', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE])
;

};
22 changes: 22 additions & 0 deletions config/services/resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Webgriffe\SyliusPagolightPlugin\Resolver\PagolightPaymentMethodsResolver;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe_sylius_pagolight.payment_methods_resolver.pagolight', PagolightPaymentMethodsResolver::class)
->args([
service('sylius.repository.payment_method'),
])
->tag('sylius.payment_method_resolver', [
'type' => 'pagolight',
'label' => 'Pagolight',
'priority' => 2,
])
;
};
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ TODO
- [x] Se annullo il checkout lo stato è pending?? Come gestiamo il caso? Annulliamo il pagamento?
- [x] Riprendendo la URL ripartiamo dall'ultimo stato? No
- [x] Il webhook di successo parte subito? O dopo un tot di tempo?
- [ ] Fare in modo che il pagamento sia visibile solo in italia o svizzera e solo per euro o franchi svizzeri
- [x] Fare in modo che il pagamento sia visibile solo in italia o svizzera e solo per euro o franchi svizzeri

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Paying with Pagolight PRO during checkout
And the store ships to "Italy"
And there is a user "[email protected]" identified by "password123"
And the store has a payment method "Pagolight PRO" with a code "PAGOLIGHT_PRO_PAYMENT_METHOD" and Pagolight Payment Checkout gateway
And the store has a product "PHP T-Shirt" priced at "$19.99"
And the store has a product "PHP T-Shirt" priced at "€109.99"
And the store ships everywhere for free
And I am logged in as "[email protected]"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@paying_for_order
Feature: Preventing not available payment method selection
In order to pay for my order properly
As a Customer
I want to be prevented from selecting not available payment methods

Background:
Given the store operates on a single channel in "EUR" currency
And there is a zone "The Rest of the World" containing all other countries
And the store ships to "Italy"
And there is a user "[email protected]" identified by "password123"
And the store has a payment method "Pagolight" with a code "PAGOLIGHT_PAYMENT_METHOD" and Pagolight Payment Checkout gateway
And the store has a payment method "Pagolight PRO" with a code "PAGOLIGHT_PRO_PAYMENT_METHOD" and Pagolight Payment Checkout gateway
And the store has a product "PHP T-Shirt" priced at "€19.99"
And the store ships everywhere for free
And I am logged in as "[email protected]"

@ui
Scenario: Not being able to select Pagolight PRO payment method if order total is less than 100 EUR
Given I have product "PHP T-Shirt" in the cart
And I am at the checkout addressing step
When I specify the billing address as "Via Franceschini 3", "Casalgrande", "42013", "Italy" for "Mario Rossi"
And I complete the addressing step
And I select "Free" shipping method
And I complete the shipping step
Then I should not be able to select "Pagolight PRO" payment method
136 changes: 136 additions & 0 deletions spec/Resolver/PagolightPaymentMethodsResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

namespace spec\Webgriffe\SyliusPagolightPlugin\Resolver;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;
use Webgriffe\SyliusPagolightPlugin\Resolver\PagolightPaymentMethodsResolver;

final class PagolightPaymentMethodsResolverSpec extends ObjectBehavior
{
public function let(
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentInterface $payment,
PaymentMethodInterface $pagolightPaymentMethod,
PaymentMethodInterface $pagolightProPaymentMethod,
PaymentMethodInterface $otherPaymentMethod,
OrderInterface $order,
ChannelInterface $channel,
AddressInterface $billingAddress,
AddressInterface $shippingAddress,
GatewayConfigInterface $pagolightGatewayConfig,
GatewayConfigInterface $pagolightProGatewayConfig,
GatewayConfigInterface $otherGatewayConfig,
): void {
$billingAddress->getCountryCode()->willReturn('IT');
$shippingAddress->getCountryCode()->willReturn('IT');

$order->getChannel()->willReturn($channel);
$order->getBillingAddress()->willReturn($billingAddress);
$order->getShippingAddress()->willReturn($shippingAddress);
$order->getCurrencyCode()->willReturn('EUR');
$order->getTotal()->willReturn(19000);

$payment->getOrder()->willReturn($order);
$payment->getMethod()->willReturn($pagolightPaymentMethod);

$pagolightGatewayConfig->getGatewayName()->willReturn(PagolightApi::PAGOLIGHT_GATEWAY_CODE);
$pagolightProGatewayConfig->getGatewayName()->willReturn(PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE);
$otherGatewayConfig->getGatewayName()->willReturn('other');

$pagolightPaymentMethod->getCode()->willReturn('PAGOLIGHT_PAYMENT_METHOD_CODE');
$pagolightPaymentMethod->getGatewayConfig()->willReturn($pagolightGatewayConfig);
$pagolightProPaymentMethod->getCode()->willReturn('PAGOLIGHT_PRO_PAYMENT_METHOD_CODE');
$pagolightProPaymentMethod->getGatewayConfig()->willReturn($pagolightProGatewayConfig);
$otherPaymentMethod->getCode()->willReturn('other_payment_method');
$otherPaymentMethod->getGatewayConfig()->willReturn($otherGatewayConfig);

$paymentMethodRepository->findEnabledForChannel($channel)->willReturn([
$pagolightPaymentMethod,
$pagolightProPaymentMethod,
$otherPaymentMethod,
]);

$this->beConstructedWith($paymentMethodRepository);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(PagolightPaymentMethodsResolver::class);
}

public function it_implements_payment_methods_resolver_interface(): void
{
$this->shouldImplement(PaymentMethodsResolverInterface::class);
}

public function it_resolves_pagolight_payment_methods_if_eligible(
PaymentInterface $payment,
PaymentMethodInterface $pagolightPaymentMethod,
PaymentMethodInterface $pagolightProPaymentMethod,
PaymentMethodInterface $otherPaymentMethod,
): void {
$this->getSupportedMethods($payment)->shouldReturn([
0 => $pagolightPaymentMethod,
1 => $pagolightProPaymentMethod,
2 => $otherPaymentMethod,
]);
}

public function it_does_not_resolve_pagolight_pro_payment_method_if_order_amount_is_equal_or_under_100(
PaymentInterface $payment,
PaymentMethodInterface $pagolightPaymentMethod,
PaymentMethodInterface $otherPaymentMethod,
OrderInterface $order,
): void {
$order->getTotal()->willReturn(10000);
$this->getSupportedMethods($payment)->shouldReturn([
0 => $pagolightPaymentMethod,
2 => $otherPaymentMethod,
]);

$order->getTotal()->willReturn(9900);
$this->getSupportedMethods($payment)->shouldReturn([
0 => $pagolightPaymentMethod,
2 => $otherPaymentMethod,
]);
}

public function it_does_not_resolve_pagolight_payment_methods_if_order_currency_is_not_supported(
PaymentInterface $payment,
PaymentMethodInterface $otherPaymentMethod,
OrderInterface $order,
): void {
$order->getCurrencyCode()->willReturn('USD');
$this->getSupportedMethods($payment)->shouldReturn([
2 => $otherPaymentMethod,
]);
}

public function it_does_not_resolve_pagolight_payment_methods_if_country_code_is_not_supported(
PaymentInterface $payment,
PaymentMethodInterface $otherPaymentMethod,
AddressInterface $billingAddress,
AddressInterface $shippingAddress,
): void {
$billingAddress->getCountryCode()->willReturn('US');
$this->getSupportedMethods($payment)->shouldReturn([
2 => $otherPaymentMethod,
]);

$shippingAddress->getCountryCode()->willReturn('US');
$this->getSupportedMethods($payment)->shouldReturn([
2 => $otherPaymentMethod,
]);
}
}
2 changes: 2 additions & 0 deletions src/Client/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ final class Config
self::EN_GB_LANGUAGE_CODE,
self::IT_LANGUAGE_CODE,
];

public const PAGOLIGHT_PRO_MINIMUM_AMOUNT = 100;
}
3 changes: 2 additions & 1 deletion src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Webgriffe\SyliusPagolightPlugin\PaymentDetailsHelper;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;
use Webmozart\Assert\Assert;

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ public function statusAction(mixed $paymentId): Response
if (!$paymentGatewayConfig instanceof GatewayConfigInterface) {
throw $this->createAccessDeniedException();
}
if (!in_array($paymentGatewayConfig->getGatewayName(), ['pagolight', 'pagolight_pro'])) {
if (!in_array($paymentGatewayConfig->getGatewayName(), [PagolightApi::PAGOLIGHT_GATEWAY_CODE, PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE], true)) {
throw $this->createAccessDeniedException();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Webgriffe\SyliusPagolightPlugin\Client\ValueObject\Contract;
use Webgriffe\SyliusPagolightPlugin\Client\ValueObject\Response\ContractCreateResult;
use Webgriffe\SyliusPagolightPlugin\PaymentDetailsHelper;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;
use Webgriffe\SyliusPagolightPlugin\Payum\Request\Api\CreateContract;
use Webgriffe\SyliusPagolightPlugin\Payum\Request\ConvertPaymentToContract;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function execute($request): void
$notifyUrl = $notifyToken->getTargetUrl();

$additionalData = [];
if ($payment->getMethod()?->getCode() === 'pagolight_pro') {
if ($payment->getMethod()?->getCode() === PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE) {
$additionalData['pricing_structure_code'] = 'PC6';
}
$convertPaymentToContract = new ConvertPaymentToContract(
Expand Down
4 changes: 4 additions & 0 deletions src/Payum/PagolightApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

final class PagolightApi
{
public const PAGOLIGHT_GATEWAY_CODE = 'pagolight';

public const PAGOLIGHT_PRO_GATEWAY_CODE = 'pagolight_pro';

/**
* @param array{sandbox: bool, merchant_key: string} $config
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Payum/PagolightGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class PagolightGatewayFactory extends GatewayFactory
protected function populateConfig(PayumArrayObject $config): void
{
$config->defaults([
'payum.factory_name' => 'pagolight',
'payum.factory_name' => PagolightApi::PAGOLIGHT_GATEWAY_CODE,
'payum.factory_title' => 'Pagolight',
'payum.action.status' => '@webgriffe_sylius_pagolight.payum.action.status',
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Payum/PagolightProGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class PagolightProGatewayFactory extends GatewayFactory
protected function populateConfig(PayumArrayObject $config): void
{
$config->defaults([
'payum.factory_name' => 'pagolight_pro',
'payum.factory_name' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE,
'payum.factory_title' => 'Pagolight Pro',
'payum.action.status' => '@webgriffe_sylius_pagolight.payum.action.status',
]);
Expand Down
Loading

0 comments on commit af1dd4a

Please sign in to comment.