Revert payment method fee calculators

This commit is contained in:
Magdalena Banasiak 2016-01-18 18:25:43 +01:00
parent d3006ebd28
commit 347347093e
63 changed files with 76 additions and 2050 deletions

View file

@ -0,0 +1,34 @@
<?php
namespace Sylius\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160118174656 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE sylius_payment_method DROP fee_calculator, DROP fee_calculator_configuration');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE sylius_payment_method ADD fee_calculator VARCHAR(255) DEFAULT NULL COLLATE utf8_unicode_ci, ADD fee_calculator_configuration LONGTEXT DEFAULT NULL COLLATE utf8_unicode_ci COMMENT \'(DC2Type:array)\'');
}
}

View file

@ -7,9 +7,9 @@ Feature: Payment methods
Background:
Given store has default configuration
And the following payment methods exist:
| code | name | gateway | calculator | calculator_configuration |
| PM1 | Credit Card | stripe | fixed | amount: 10 |
| PM2 | PayPal | paypal | percent | percent: 5 |
| code | name | gateway |
| PM1 | Credit Card | stripe |
| PM2 | PayPal | paypal |
And I am logged in as administrator
Scenario: Seeing index of all payment methods
@ -40,7 +40,6 @@ Feature: Payment methods
Given I am on the payment method creation page
When I fill in "Code" with "PM3"
And I fill in "Name" with "Google Checkout"
And I fill in "Amount" with "10"
And I press "Create"
Then I should be on the payment method index page
And I should see "Payment method has been successfully created"
@ -50,7 +49,6 @@ Feature: Payment methods
When I fill in "Code" with "PM3"
And I fill in "Name" with "Google Checkout"
And I fill in "Description" with "Flexible checkout by Google!"
And I fill in "Amount" with "10"
And I press "Create"
Then I should be on the payment method index page
And I should see "Payment method has been successfully created"
@ -59,7 +57,6 @@ Feature: Payment methods
Given I am on the payment method creation page
When I fill in "Code" with "PM3"
And I fill in "Name" with "PayU"
And I fill in "Amount" with "10"
And I press "Create"
Then I should be on the payment method index page
And I should see payment method with name "PayU" in the list
@ -72,51 +69,10 @@ Feature: Payment methods
Scenario: Updating the payment method
Given I am editing payment method "PayPal"
When I fill in "Name" with "PayPal PRO"
And I fill in "%" with "10"
And I press "Save changes"
Then I should be on the payment method index page
And I should see payment method with name "PayPal PRO" in the list
Scenario: Submitting invalid form without percent
Given I am editing payment method "PayPal"
When I fill in "Name" with "PayPal PRO"
And I leave "%" empty
And I press "Save changes"
Then I should be editing payment method "PayPal"
And I should see "Please enter the fee percent"
Scenario: Submitting invalid form with negative percent
Given I am editing payment method "PayPal"
When I fill in "Name" with "PayPal PRO"
And I fill in "%" with "-1"
And I press "Save changes"
Then I should be editing payment method "PayPal"
And I should see "Percent fee cannot be lower than 0"
Scenario: Submitting invalid form with percent over 100
Given I am editing payment method "PayPal"
When I fill in "Name" with "PayPal PRO"
And I fill in "%" with "120"
And I press "Save changes"
Then I should be editing payment method "PayPal"
And I should see "Percent fee cannot be greater than 100"
Scenario: Submitting invalid form without amount
Given I am editing payment method "Credit Card"
When I fill in "Name" with "Master Card"
And I leave "Amount" empty
And I press "Save changes"
Then I should be editing payment method "Credit Card"
And I should see "Please enter the fee amount"
Scenario: Submitting invalid form with negative amount
Given I am editing payment method "Credit Card"
When I fill in "Name" with "Master Card"
And I fill in "Amount" with "-1"
And I press "Save changes"
Then I should be editing payment method "Credit Card"
And I should see "The fee cannot be lower than 0"
@javascript
Scenario: Deleting payment method
Given I am on the payment method index page

View file

@ -24,8 +24,8 @@ Feature: Checkout finalization
| code | zone | name |
| SM1 | UK | DHL Express |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Dummy | dummy | yes | fixed | amount: 0 |
| code | name | gateway | enabled |
| PM1 | Dummy | dummy | yes |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |

View file

@ -22,8 +22,8 @@ Feature: Checkout inventory
| code | zone | name |
| SM1 | UK | DHL Express |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Credit Card | dummy | yes | fixed | amount: 0 |
| code | name | gateway | enabled |
| PM1 | Credit Card | dummy | yes |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |

View file

@ -21,15 +21,14 @@ Feature: Checkout Payment
| code | zone | name |
| SM1 | UK | DHL Express |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Credit Card | stripe | yes | fixed | amount: 0 |
| PM2 | Credit Card PRO | stripe | yes | percent | percent: 0 |
| PM3 | PayPal | paypal | yes | fixed | amount: 50 |
| PM4 | PayPal PRO | paypal_pro | no | percent | percent: 10 |
| code | name | gateway | enabled |
| PM1 | Credit Card | stripe | yes |
| PM2 | PayPal | paypal | yes |
| PM3 | PayPal PRO | paypal_pro | no |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |
| Category | PayPal, PayPal PRO, Credit Card, Credit Card PRO | DHL Express |
| taxonomy | payment | shipping |
| Category | PayPal, PayPal PRO, Credit Card | DHL Express |
And I am logged in user
And I added product "PHP Top" to cart
And I go to the checkout start page
@ -45,30 +44,10 @@ Feature: Checkout Payment
Given I press "Continue"
Then I should be on the checkout payment step
And I should see "PayPal"
And I should see "0.50"
But I should not see "PayPal PRO"
And I should not see "3.1"
Scenario: Selecting one of payment methods
Given I press "Continue"
When I select the "PayPal" radio button
And I press "Continue"
Then I should be on the checkout finalize step
Scenario: Showing payment fee charge
Given I press "Continue"
When I select the "PayPal" radio button
And I press "Continue"
Then I should be on the checkout finalize step
And "Payment total: 0.50" should appear on the page
And "Total: 31.49" should appear on the page
Scenario: No fee is added if amount 0 is set in calculator
Given I press "Continue"
When I select the "Credit Card" radio button
And I press "Continue"
Then "Payment total: 0.00" should appear on the page
When I click "Back"
And I select the "Credit Card PRO" radio button
And I press "Continue"
Then I should see "Payment total: 0.00"

View file

@ -25,8 +25,8 @@ Feature: Checkout security
| code | zone | name |
| SM1 | UK | DHL Express |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Dummy | dummy | yes | fixed | amount: 0 |
| code | name | gateway | enabled |
| PM1 | Dummy | dummy | yes |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |

View file

@ -31,8 +31,8 @@ Feature: Checkout shipping
| SM3 | USA | FedEx Premium | Flat rate | Amount: 10000 | yes |
| SM4 | UK + Germany | UPS Ground | Flat rate | Amount: 20000 | no |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Dummy | dummy | yes | fixed | amount: 0 |
| code | name | gateway | enabled |
| PM1 | Dummy | dummy | yes |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |

View file

@ -27,8 +27,8 @@ Feature: Checkout taxation
| code | zone | name |
| SM1 | UK | DHL Express |
And the following payment methods exist:
| code | name | gateway | enabled | calculator | calculator_configuration |
| PM1 | Dummy | dummy | yes | fixed | amount: 0 |
| code | name | gateway | enabled |
| PM1 | Dummy | dummy | yes |
And all products are assigned to the default channel
And the default channel has following configuration:
| taxonomy | payment | shipping |

View file

@ -13,7 +13,6 @@ namespace Sylius\Bundle\CoreBundle\EventListener;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderProcessing\PaymentChargesProcessorInterface;
use Sylius\Component\Core\OrderProcessing\PaymentProcessorInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
@ -24,7 +23,6 @@ use Symfony\Component\EventDispatcher\GenericEvent;
* Order payment listener.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class OrderPaymentListener
{
@ -35,11 +33,6 @@ class OrderPaymentListener
*/
protected $paymentProcessor;
/**
* @var PaymentChargesProcessorInterface
*/
protected $paymentChargesProcessor;
/**
* @var EventDispatcherInterface
*/
@ -54,18 +47,15 @@ class OrderPaymentListener
* Constructor.
*
* @param PaymentProcessorInterface $paymentProcessor
* @param PaymentChargesProcessorInterface $paymentChargesProcessor
* @param EventDispatcherInterface $dispatcher
* @param FactoryInterface $factory
*/
public function __construct(
PaymentProcessorInterface $paymentProcessor,
PaymentChargesProcessorInterface $paymentChargesProcessor,
EventDispatcherInterface $dispatcher,
FactoryInterface $factory
) {
$this->paymentProcessor = $paymentProcessor;
$this->paymentChargesProcessor = $paymentChargesProcessor;
$this->dispatcher = $dispatcher;
$this->factory = $factory;
}
@ -103,16 +93,6 @@ class OrderPaymentListener
$payment->setAmount($order->getTotal());
}
/**
* @param GenericEvent $event
*/
public function processOrderPaymentCharges(GenericEvent $event)
{
$this->paymentChargesProcessor->applyPaymentCharges(
$this->getOrder($event)
);
}
/**
* @param GenericEvent $event
*

View file

@ -36,7 +36,6 @@
<parameter key="sylius.order_processing.state_resolver.class">Sylius\Component\Core\OrderProcessing\StateResolver</parameter>
<parameter key="sylius.order_processing.shipment_factory.class">Sylius\Component\Core\OrderProcessing\OrderShipmentFactory</parameter>
<parameter key="sylius.order_processing.payment_processor.class">Sylius\Component\Core\OrderProcessing\PaymentProcessor</parameter>
<parameter key="sylius.order_processing.payment_charges_processor.class">Sylius\Component\Core\OrderProcessing\PaymentChargesProcessor</parameter>
<parameter key="sylius.order_processing.shipping_processor.class">Sylius\Component\Core\OrderProcessing\ShippingChargesProcessor</parameter>
<parameter key="sylius.order_processing.taxation_processor.class">Sylius\Bundle\CoreBundle\OrderProcessing\TaxationProcessor</parameter>
@ -196,11 +195,6 @@
</service>
<service id="sylius.order_processing.payment_processor" class="%sylius.order_processing.payment_processor.class%">
<argument type="service" id="sylius.factory.payment" />
<argument type="service" id="sylius.manager.payment" />
</service>
<service id="sylius.order_processing.payment_charges_processor" class="%sylius.order_processing.payment_charges_processor.class%">
<argument type="service" id="sylius.factory.adjustment" />
<argument type="service" id="sylius.payment.delegating_fee_calculator" />
</service>
<service id="sylius.order_processing.taxation_processor" class="%sylius.order_processing.taxation_processor.class%">
<argument type="service" id="sylius.factory.adjustment" />
@ -282,11 +276,9 @@
</service>
<service id="sylius.listener.order_payment" class="%sylius.listener.order_payment.class%">
<argument type="service" id="sylius.order_processing.payment_processor" />
<argument type="service" id="sylius.order_processing.payment_charges_processor" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="sm.factory" />
<tag name="kernel.event_listener" event="sylius.checkout.payment.initialize" method="createOrderPayment" />
<tag name="kernel.event_listener" event="sylius.checkout.payment.pre_complete" method="processOrderPaymentCharges" />
<tag name="kernel.event_listener" event="sylius.checkout.finalize.pre_complete" method="updateOrderPayment" />
</service>
<service id="sylius.listener.order_channel" class="%sylius.listener.order_channel.class%">

View file

@ -16,7 +16,6 @@ use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderProcessing\PaymentChargesProcessorInterface;
use Sylius\Component\Core\OrderProcessing\PaymentProcessorInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -27,9 +26,9 @@ use Symfony\Component\EventDispatcher\GenericEvent;
*/
class OrderPaymentListenerSpec extends ObjectBehavior
{
function let(PaymentProcessorInterface $processor, PaymentChargesProcessorInterface $paymentChargesProcessor, EventDispatcherInterface $dispatcher, FactoryInterface $factory)
function let(PaymentProcessorInterface $processor, EventDispatcherInterface $dispatcher, FactoryInterface $factory)
{
$this->beConstructedWith($processor, $paymentChargesProcessor, $dispatcher, $factory);
$this->beConstructedWith($processor, $dispatcher, $factory);
}
function it_is_initializable()
@ -94,13 +93,4 @@ class OrderPaymentListenerSpec extends ObjectBehavior
$this->updateOrderPayment($event);
}
function it_processes_order_payment_charges($paymentChargesProcessor, GenericEvent $event, OrderInterface $order)
{
$event->getSubject()->willReturn($order);
$paymentChargesProcessor->applyPaymentCharges($order)->shouldBeCalled();
$this->processOrderPaymentCharges($event);
}
}

View file

@ -27,12 +27,12 @@ class LoadPaymentMethodsData extends DataFixture
*/
public function load(ObjectManager $manager)
{
$manager->persist($this->createPaymentMethod('PM1', 'Dummy', 'dummy', 'fixed', array('amount' => 0)));
$manager->persist($this->createPaymentMethod('PM2', 'PaypalExpressCheckout', 'paypal_express_checkout', 'fixed', array('amount' => 1000)));
$manager->persist($this->createPaymentMethod('PM3', 'Be2bill', 'be2bill_direct', 'fixed', array('amount' => 100)));
$manager->persist($this->createPaymentMethod('PM4', 'Be2billOffsite', 'be2bill_offsite', 'percent', array('percent' => 7)));
$manager->persist($this->createPaymentMethod('PM5', 'StripeCheckout', 'stripe_checkout', 'percent', array('percent' => 5)));
$manager->persist($this->createPaymentMethod('PM6', 'Offline', 'offline', 'fixed', array('amount' => 500)));
$manager->persist($this->createPaymentMethod('PM1', 'Dummy', 'dummy'));
$manager->persist($this->createPaymentMethod('PM2', 'PaypalExpressCheckout', 'paypal_express_checkout'));
$manager->persist($this->createPaymentMethod('PM3', 'Be2bill', 'be2bill_direct'));
$manager->persist($this->createPaymentMethod('PM4', 'Be2billOffsite', 'be2bill_offsite'));
$manager->persist($this->createPaymentMethod('PM5', 'StripeCheckout', 'stripe_checkout'));
$manager->persist($this->createPaymentMethod('PM6', 'Offline', 'offline', 'fixed'));
$manager->flush();
}
@ -50,13 +50,11 @@ class LoadPaymentMethodsData extends DataFixture
* @param string $code
* @param string $name
* @param string $gateway
* @param string $feeCalculator
* @param array $feeCalculatorConfiguration
* @param boolean $enabled
*
* @return PaymentMethodInterface
*/
protected function createPaymentMethod($code, $name, $gateway, $feeCalculator, array $feeCalculatorConfiguration, $enabled = true)
protected function createPaymentMethod($code, $name, $gateway, $enabled = true)
{
/* @var $method PaymentMethodInterface */
$method = $this->getPaymentMethodFactory()->createNew();
@ -69,8 +67,6 @@ class LoadPaymentMethodsData extends DataFixture
$method->setGateway($gateway);
$method->setEnabled($enabled);
$method->setFeeCalculator($feeCalculator);
$method->setFeeCalculatorConfiguration($feeCalculatorConfiguration);
$method->setCode($code);
$this->setReference('Sylius.PaymentMethod.'.$name, $method);

View file

@ -38,8 +38,6 @@ class PaymentContext extends DefaultContext
$method->setCode(trim($data['code']));
$method->setName(trim($data['name']));
$method->setGateway(trim($data['gateway']));
$method->setFeeCalculator($data['calculator']);
$method->setFeeCalculatorConfiguration($this->getConfiguration($data['calculator_configuration']));
$method->setEnabled(isset($data['enabled']) ? 'yes' === trim($data['enabled']) : true);

View file

@ -1,71 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PaymentBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class RegisterFeeCalculatorsPass implements CompilerPassInterface
{
/**
* Array of registered fee calculators
*
* @var array
*/
private $calculators = array();
/**
* @var Definition
*/
private $registry;
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sylius.registry.payment.fee_calculator')) {
return;
}
$this->registry = $container->getDefinition('sylius.registry.payment.fee_calculator');
foreach ($container->findTaggedServiceIds('sylius.payment.fee_calculator') as $id => $attributes) {
$this->fillCalculatorsArray($id, $attributes);
}
$container->setParameter('sylius.payment.fee_calculators', $this->calculators);
}
/**
* @param integer $id
* @param array $attributes
*
* @throws \InvalidArgumentException if fee calculator is improperly tagged
*/
private function fillCalculatorsArray($id, array $attributes)
{
if (!isset($attributes[0]['calculator']) || !isset($attributes[0]['label'])) {
throw new \InvalidArgumentException('Tagged fee calculators needs to have `fee_calculator` and `label` attributes.');
}
$name = $attributes[0]['calculator'];
$this->calculators[$name] = $attributes[0]['label'];
$this->registry->addMethodCall('register', array($name, new Reference($id)));
}
}

View file

@ -15,7 +15,6 @@ use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceE
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
/**
* Payments extension.
@ -43,16 +42,5 @@ class SyliusPaymentExtension extends AbstractResourceExtension
}
$container->setParameter('sylius.payment_gateways', $config['gateways']);
$container
->getDefinition('sylius.form.type.payment_method')
->addArgument(new Reference('sylius.registry.payment.fee_calculator'))
;
$container
->getDefinition('sylius.form.type.payment_method_choice')
->addArgument(new Reference('sylius.registry.payment.fee_calculator'))
->addArgument(new Reference('sylius.repository.payment'))
;
}
}

View file

@ -1,64 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class FeeCalculatorChoiceType extends AbstractType
{
/**
* @var array
*/
protected $feeCalculators;
/**
* Constructor.
*
* @param array $feeCalculators
*/
public function __construct(array $feeCalculators)
{
$this->feeCalculators = $feeCalculators;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults(array(
'choices' => $this->feeCalculators,
))
;
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'choice';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_fee_calculator_choice';
}
}

View file

@ -1,42 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use Sylius\Component\Currency\Context\CurrencyContext;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class FixedFeeCalculatorConfigurationType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('amount', 'sylius_money', array(
'label' => 'sylius.form.payment_method.fee_calculator.fixed.amount',
))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_fee_calculator_fixed';
}
}

View file

@ -1,41 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class PercentFeeCalculatorConfigurationType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('percent', 'number', array(
'label' => 'sylius.form.payment_method.fee_calculator.percent.percent',
))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_fee_calculator_percent';
}
}

View file

@ -1,116 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PaymentBundle\Form\Type\EventListener;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class BuildPaymentMethodFeeCalculatorFormSubscriber implements EventSubscriberInterface
{
/**
* @var ServiceRegistryInterface
*/
private $feeCalculatorRegistry;
/**
* @var FormFactoryInterface
*/
private $factory;
/**
* Constructor.
*
* @param ServiceRegistryInterface $feeCalculatorRegistry
* @param FormFactoryInterface $factory
*/
public function __construct(ServiceRegistryInterface $feeCalculatorRegistry, FormFactoryInterface $factory)
{
$this->feeCalculatorRegistry = $feeCalculatorRegistry;
$this->factory = $factory;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preBind',
);
}
/**
* @param FormEvent $event
*/
public function preSetData(FormEvent $event)
{
$paymentMethod = $event->getData();
if (null === $paymentMethod) {
return;
}
if (!$paymentMethod instanceof PaymentMethodInterface) {
throw new UnexpectedTypeException($paymentMethod, PaymentMethodInterface::class);
}
$this->addConfigurationFields($event->getForm(), $paymentMethod->getFeeCalculator(), $paymentMethod->getFeeCalculatorConfiguration());
}
/**
* @param FormEvent $event
*/
public function preBind(FormEvent $event)
{
$data = $event->getData();
if (empty($data) || !array_key_exists('feeCalculator', $data)) {
return;
}
$this->addConfigurationFields($event->getForm(), $data['feeCalculator']);
}
/**
* @param FormInterface $form
* @param string $feeCalculatorType
* @param array $data
*/
private function addConfigurationFields(FormInterface $form, $feeCalculatorType, $data = array())
{
$feeCalculator = $this->feeCalculatorRegistry->get($feeCalculatorType);
$formType = sprintf('sylius_fee_calculator_%s', $feeCalculator->getType());
try {
$configurationField = $this->factory->createNamed(
'feeCalculatorConfiguration',
$formType,
$data,
array('auto_initialize' => false)
);
} catch (\InvalidArgumentException $exception) {
return;
}
$form->add($configurationField);
}
}

View file

@ -32,31 +32,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
class PaymentMethodChoiceType extends ResourceChoiceType
{
/**
* @var ServiceRegistryInterface
*/
private $feeCalculatorRegistry;
/**
* @var RepositoryInterface
*/
private $paymentRepository;
/**
* @param string $className
* @param string $driver
* @param string $name
* @param ServiceRegistryInterface $feeCalculatorRegistry
* @param RepositoryInterface $paymentRepository
*/
public function __construct($className, $driver, $name, ServiceRegistryInterface $feeCalculatorRegistry, RepositoryInterface $paymentRepository)
{
parent::__construct($className, $driver, $name);
$this->feeCalculatorRegistry = $feeCalculatorRegistry;
$this->paymentRepository = $paymentRepository;
}
/**
* {@inheritdoc}
*/
@ -82,35 +57,6 @@ class PaymentMethodChoiceType extends ResourceChoiceType
;
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (!$parent = $view->parent->vars['value'] instanceof Order) {
return;
}
$paymentCosts = array();
$payment = $view->parent->vars['value']->getPayments()->last();
foreach ($view->vars['choices'] as $choiceView) {
$method = $choiceView->data;
if (!$method instanceof PaymentMethodInterface) {
throw new UnexpectedTypeException($method, PaymentMethodInterface::class);
}
$feeCalculator = $this->feeCalculatorRegistry->get($method->getFeeCalculator());
$payment->setMethod($method);
$paymentCosts[$choiceView->value] = $feeCalculator->calculate($payment, $method->getFeeCalculatorConfiguration());
}
$view->vars['paymentCosts'] = $paymentCosts;
}
/**
* @return string
*/

View file

@ -23,34 +23,9 @@ use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
* Payment method form type.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class PaymentMethodType extends AbstractResourceType
{
/**
* Fee calculator registry.
*
* @var ServiceRegistryInterface
*/
protected $feeCalculatorRegistry;
/**
* Constructor.
*
* @param string $dataClass
* @param array $validationGroups
* @param ServiceRegistryInterface $feeCalculatorRegistry
*/
public function __construct(
$dataClass,
array $validationGroups,
ServiceRegistryInterface $feeCalculatorRegistry
) {
parent::__construct($dataClass, $validationGroups);
$this->feeCalculatorRegistry = $feeCalculatorRegistry;
}
/**
* {@inheritdoc}
*/
@ -68,46 +43,8 @@ class PaymentMethodType extends AbstractResourceType
'required' => false,
'label' => 'sylius.form.payment_method.enabled',
))
->add('feeCalculator', 'sylius_fee_calculator_choice', array(
'label' => 'sylius.form.payment.fee_calculator',
))
->addEventSubscriber(new AddCodeFormSubscriber())
->addEventSubscriber(new BuildPaymentMethodFeeCalculatorFormSubscriber($this->feeCalculatorRegistry, $builder->getFormFactory()))
;
$this->setBuilderFeeCalculatorsConfigurationsAttribute($builder);
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['feeCalculatorsConfigurations'] = array();
foreach ($form->getConfig()->getAttribute('feeCalculatorsConfigurations') as $type => $feeCalculatorConfiguration) {
$view->vars['feeCalculatorsConfigurations'][$type] = $feeCalculatorConfiguration->createView($view);
}
}
/**
* @param FormBuilderInterface $builder
*/
private function setBuilderFeeCalculatorsConfigurationsAttribute(FormBuilderInterface $builder)
{
$feeCalculatorsConfigurations = array();
foreach ($this->feeCalculatorRegistry->all() as $type => $feeCalculator) {
$formType = sprintf('sylius_fee_calculator_%s', $feeCalculator->getType());
try {
$feeCalculatorsConfigurations[$type] = $builder->create('feeCalculatorConfiguration', $formType)->getForm();
} catch (\InvalidArgumentException $exception) {
continue;
}
}
$builder->setAttribute('feeCalculatorsConfigurations', $feeCalculatorsConfigurations);
}
/**

View file

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
@ -22,8 +21,6 @@
<field name="gateway" column="gateway" type="string" />
<field name="environment" column="environment" type="string" nullable="true" />
<field name="enabled" column="is_enabled" type="boolean" />
<field name="feeCalculator" column="fee_calculator" nullable="true" type="string" />
<field name="feeCalculatorConfiguration" column="fee_calculator_configuration" type="array" nullable="true" />
<field name="createdAt" column="created_at" type="datetime">
<gedmo:timestampable on="create"/>

View file

@ -18,19 +18,6 @@
<parameters>
<parameter key="sylius.form.type.payment_gateway_choice.class">Sylius\Bundle\PaymentBundle\Form\Type\PaymentGatewayChoiceType</parameter>
<parameter key="sylius.registry.fee_calculator.class">Sylius\Component\Registry\ServiceRegistry</parameter>
<parameter key="sylius.form.type.payment_method.class">Sylius\Bundle\PaymentBundle\Form\Type\PaymentMethodType</parameter>
<parameter key="sylius.form.type.fee_calculator_choice.class">Sylius\Bundle\PaymentBundle\Form\Type\Calculator\FeeCalculatorChoiceType</parameter>
<parameter key="sylius.payment.delegating_fee_calculator.class">Sylius\Component\Payment\Calculator\DelegatingFeeCalculator</parameter>
<parameter key="sylius.form.type.payment.fee_calculator.fixed.class">Sylius\Component\Payment\Calculator\FixedFeeCalculator</parameter>
<parameter key="sylius.form.type.payment.fee_calculator.fixed_configuration.class">Sylius\Bundle\PaymentBundle\Form\Type\Calculator\FixedFeeCalculatorConfigurationType</parameter>
<parameter key="sylius.form.type.payment.fee_calculator.percent.class">Sylius\Component\Payment\Calculator\PercentFeeCalculator</parameter>
<parameter key="sylius.form.type.payment.fee_calculator.percent_configuration.class">Sylius\Bundle\PaymentBundle\Form\Type\Calculator\PercentFeeCalculatorConfigurationType</parameter>
</parameters>
<services>
@ -38,34 +25,6 @@
<argument>%sylius.payment_gateways%</argument>
<tag name="form.type" alias="sylius_payment_gateway_choice" />
</service>
<service id="sylius.registry.payment.fee_calculator" class="%sylius.registry.fee_calculator.class%">
<argument>Sylius\Component\Payment\Calculator\FeeCalculatorInterface</argument>
</service>
<service id="sylius.payment.delegating_fee_calculator" class="%sylius.payment.delegating_fee_calculator.class%">
<argument type="service" id="sylius.registry.payment.fee_calculator" />
</service>
<service id="sylius.form.type.fee_calculator_choice" class="%sylius.form.type.fee_calculator_choice.class%">
<argument>%sylius.payment.fee_calculators%</argument>
<tag name="form.type" alias="sylius_fee_calculator_choice" />
</service>
<service id="sylius.form.type.payment.fee_calculator.fixed" class="%sylius.form.type.payment.fee_calculator.fixed.class%">
<tag name="sylius.payment.fee_calculator" calculator="fixed" label="Fixed fee" />
</service>
<service id="sylius.form.type.payment.fee_calculator.fixed_configuration" class="%sylius.form.type.payment.fee_calculator.fixed_configuration.class%">
<argument type="service" id="sylius.context.currency" />
<tag name="form.type" alias="sylius_fee_calculator_fixed" />
</service>
<service id="sylius.form.type.payment.fee_calculator.percent" class="%sylius.form.type.payment.fee_calculator.percent.class%">
<tag name="sylius.payment.fee_calculator" calculator="percent" label="Percent fee" />
</service>
<service id="sylius.form.type.payment.fee_calculator.percent_configuration" class="%sylius.form.type.payment.fee_calculator.percent_configuration.class%">
<tag name="form.type" alias="sylius_fee_calculator_percent" />
</service>
</services>
</container>

View file

@ -38,47 +38,6 @@
<option name="groups">sylius</option>
</constraint>
</property>
<property name="feeCalculatorConfiguration">
<constraint name="Collection">
<option name="allowMissingFields">true</option>
<option name="fields">
<value key="amount">
<constraint name="NotBlank">
<option name="message">
sylius.payment_method.fee_calculator_configuration.amount.not_blank
</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="GreaterThanOrEqual">
<option name="value">0</option>
<option name="message">
sylius.payment_method.fee_calculator_configuration.amount.greater_than_or_equal
</option>
<option name="groups">sylius</option>
</constraint>
</value>
<value key="percent">
<constraint name="NotBlank">
<option name="message">
sylius.payment_method.fee_calculator_configuration.percent.not_blank
</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Range">
<option name="min">0</option>
<option name="minMessage">
sylius.payment_method.fee_calculator_configuration.percent.min_range
</option>
<option name="max">100</option>
<option name="maxMessage">
sylius.payment_method.fee_calculator_configuration.percent.max_range
</option>
<option name="groups">sylius</option>
</constraint>
</value>
</option>
</constraint>
</property>
</class>
<class name="Sylius\Component\Payment\Model\PaymentMethodTranslation">

View file

@ -1,61 +0,0 @@
(function ($) {
'use strict';
var methods = {
init: function(options) {
var settings = $.extend({
'prototypePrefix': false,
'prototypeElementPrefix': '<hr />',
'containerSelector': false
}, options);
return this.each(function() {
show($(this), false);
$(this).change(function() {
show($(this), true);
});
function show(element, replace) {
var id = element.attr('id');
var selectedValue = element.val();
var prototypePrefix = id;
if (false != settings.prototypePrefix) {
prototypePrefix = settings.prototypePrefix;
}
var prototypeElement = $('#' + prototypePrefix + '_' + selectedValue);
var container;
if (settings.containerSelector) {
container = $(settings.containerSelector);
} else {
container = $(prototypeElement.data('container'));
}
if (!container.length) {
return;
}
if (!prototypeElement.length) {
container.empty();
return;
}
if (replace || !container.html().trim()) {
container.html(settings.prototypeElementPrefix + prototypeElement.data('prototype'));
}
}
});
}
};
$.fn.handlePrototypes = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.handlePrototypes' );
}
};
})(jQuery);

View file

@ -1,18 +0,0 @@
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
(function ($) {
'use strict';
$(document).ready(function() {
$('#sylius_payment_method_feeCalculator').handlePrototypes({
'prototypePrefix': 'sylius_payment_method_feeCalculator',
'prototypeElementPrefix': '',
});
});
})( jQuery );

View file

@ -13,7 +13,6 @@ sylius:
payment:
amount: Payment amount
method: Payment method
fee_calculator: Fee calculator
state:
header: Payment state
checkout: Checkout
@ -32,8 +31,3 @@ sylius:
enabled: Enabled?
gateway: Gateway
name: Name
fee_calculator:
fixed:
amount: Amount
percent:
percent: %

View file

@ -11,13 +11,11 @@
namespace Sylius\Bundle\PaymentBundle;
use Sylius\Bundle\PaymentBundle\DependencyInjection\Compiler\RegisterFeeCalculatorsPass;
use Sylius\Bundle\ResourceBundle\AbstractResourceBundle;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Payment\Model\CreditCardInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Payments component for Symfony2 applications.
@ -36,15 +34,6 @@ class SyliusPaymentBundle extends AbstractResourceBundle
);
}
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RegisterFeeCalculatorsPass());
}
/**
* {@inheritdoc}

View file

@ -1,75 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PaymentBundle\DependencyInjection\Compiler;
use PhpSpec\ObjectBehavior;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class RegisterFeeCalculatorsPassSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\PaymentBundle\DependencyInjection\Compiler\RegisterFeeCalculatorsPass');
}
function it_implements_compiler_pass_interface()
{
$this->shouldImplement(CompilerPassInterface::class);
}
function it_processes_with_given_container(ContainerBuilder $container, Definition $feeCalculatorDefinition)
{
$container->hasDefinition('sylius.registry.payment.fee_calculator')->willReturn(true)->shouldBeCalled();
$container->getDefinition('sylius.registry.payment.fee_calculator')->willReturn($feeCalculatorDefinition)->shouldBeCalled();
$feeCalculatorServices = array(
'sylius.form.type.fee_calculator.test' => array(
array('calculator' => 'test', 'label' => 'Test fee calculator'),
),
);
$container->findTaggedServiceIds('sylius.payment.fee_calculator')->willReturn($feeCalculatorServices);
$feeCalculatorDefinition->addMethodCall('register', array('test', new Reference('sylius.form.type.fee_calculator.test')))->shouldBeCalled();
$container->setParameter('sylius.payment.fee_calculators', array('test' => 'Test fee calculator'))->shouldBeCalled();
$this->process($container);
}
function it_does_not_process_if_container_has_no_proper_definition(ContainerBuilder $container)
{
$container->hasDefinition('sylius.registry.payment.fee_calculator')->willReturn(false)->shouldBeCalled();
$container->getDefinition('sylius.registry.payment.fee_calculator')->shouldNotBeCalled();
$this->process($container);
}
function it_throws_exception_if_any_fee_calculator_has_improper_attributes(ContainerBuilder $container, Definition $feeCalculatorDefinition)
{
$container->hasDefinition('sylius.registry.payment.fee_calculator')->willReturn(true)->shouldBeCalled();
$container->getDefinition('sylius.registry.payment.fee_calculator')->willReturn($feeCalculatorDefinition)->shouldBeCalled();
$feeCalculatorServices = array(
'sylius.form.type.fee_calculator.test' => array(
array('calculator' => 'test'),
),
);
$container->findTaggedServiceIds('sylius.payment.fee_calculator')->willReturn($feeCalculatorServices);
$this->shouldThrow(new \InvalidArgumentException('Tagged fee calculators needs to have `fee_calculator` and `label` attributes.'))->during('process', array($container));
}
}

View file

@ -1,54 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class FeeCalculatorChoiceTypeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(array());
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\PaymentBundle\Form\Type\Calculator\FeeCalculatorChoiceType');
}
function it_is_abstract_type_object()
{
$this->shouldHaveType(AbstractType::class);
}
function it_sets_default_options(OptionsResolver $resolver)
{
$resolver->setDefaults(array('choices' => array()))->shouldBeCalled();
$this->configureOptions($resolver);
}
function it_has_parent()
{
$this->getParent()->shouldReturn('choice');
}
function it_has_name()
{
$this->getName()->shouldReturn('sylius_fee_calculator_choice');
}
}

View file

@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class FixedFeeCalculatorConfigurationTypeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\PaymentBundle\Form\Type\Calculator\FixedFeeCalculatorConfigurationType');
}
function it_is_abstract_type_object()
{
$this->shouldHaveType(AbstractType::class);
}
function it_builds_form(FormBuilderInterface $builder)
{
$builder
->add('amount', 'sylius_money', array(
'label' => 'sylius.form.payment_method.fee_calculator.fixed.amount',
))
->willReturn($builder)
->shouldBeCalled()
;
$this->buildForm($builder, array());
}
function it_has_name()
{
$this->getName()->shouldReturn('sylius_fee_calculator_fixed');
}
}

View file

@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PaymentBundle\Form\Type\Calculator;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class PercentFeeCalculatorConfigurationTypeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\PaymentBundle\Form\Type\Calculator\PercentFeeCalculatorConfigurationType');
}
function it_is_abstract_type_object()
{
$this->shouldHaveType(AbstractType::class);
}
function it_builds_form(FormBuilderInterface $builder)
{
$builder
->add('percent', 'number', array(
'label' => 'sylius.form.payment_method.fee_calculator.percent.percent',
))
->willReturn($builder)
->shouldBeCalled()
;
$this->buildForm($builder, array());
}
function it_has_name()
{
$this->getName()->shouldReturn('sylius_fee_calculator_percent');
}
}

View file

@ -1,136 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PaymentBundle\Form\Type\EventListener;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Payment\Calculator\FeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class BuildPaymentMethodFeeCalculatorFormSubscriberSpec extends ObjectBehavior
{
function let(ServiceRegistryInterface $feeCalculatorRegistry, FormFactoryInterface $factory)
{
$this->beConstructedWith($feeCalculatorRegistry, $factory);
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Bundle\PaymentBundle\Form\Type\EventListener\BuildPaymentMethodFeeCalculatorFormSubscriber');
}
function it_implements_event_subscriber_interface()
{
$this->shouldImplement(EventSubscriberInterface::class);
}
function it_pre_sets_data(
$factory,
$feeCalculatorRegistry,
FeeCalculatorInterface $feeCalculator,
FormEvent $event,
FormInterface $configurationForm,
FormInterface $form,
PaymentMethodInterface $paymentMethod
) {
$event->getData()->willReturn($paymentMethod)->shouldBeCalled();
$event->getForm()->willReturn($form)->shouldBeCalled();
$paymentMethod->getFeeCalculator()->willReturn('test');
$paymentMethod->getFeeCalculatorConfiguration()->willReturn(array('amount' => 100));
$feeCalculatorRegistry->get('test')->willReturn($feeCalculator)->shouldBeCalled();
$feeCalculator->getType()->willReturn('test');
$factory
->createNamed(
'feeCalculatorConfiguration',
'sylius_fee_calculator_test',
array('amount' => 100),
array('auto_initialize' => false)
)
->willReturn($configurationForm)
->shouldBeCalled()
;
$form->add($configurationForm)->shouldBeCalled();
$this->preSetData($event);
}
function it_does_not_process_if_event_has_no_data_while_pre_set(FormEvent $event)
{
$event->getData()->willReturn(null)->shouldBeCalled();
$this->preSetData($event)->shouldReturn(null);
}
function it_throws_exception_if_event_data_is_not_payment_method_object_while_pre_set(FormEvent $event)
{
$event->getData()->willReturn('badObject')->shouldBeCalled();
$this->shouldThrow(new UnexpectedTypeException('badObject', PaymentMethodInterface::class))->during('preSetData', array($event));
}
function it_is_triggered_pre_bind(
$factory,
$feeCalculatorRegistry,
FeeCalculatorInterface $feeCalculator,
FormEvent $event,
FormInterface $configurationForm,
FormInterface $form,
PaymentMethodInterface $paymentMethod
) {
$event->getData()->willReturn(array('feeCalculator' => 'test'))->shouldBeCalled();
$event->getForm()->willReturn($form)->shouldBeCalled();
$paymentMethod->getFeeCalculator()->willReturn('test');
$paymentMethod->getFeeCalculatorConfiguration()->willReturn(array());
$feeCalculatorRegistry->get('test')->willReturn($feeCalculator)->shouldBeCalled();
$feeCalculator->getType()->willReturn('test');
$factory
->createNamed(
'feeCalculatorConfiguration',
'sylius_fee_calculator_test',
array(),
array('auto_initialize' => false)
)
->willReturn($configurationForm)
->shouldBeCalled()
;
$form->add($configurationForm)->shouldBeCalled();
$this->preBind($event);
}
function it_does_not_process_if_event_data_is_empty_or_has_no_fee_calculator_field_while_pre_bind(FormEvent $event)
{
$event->getData()->willReturn(array())->shouldBeCalled();
$this->preBind($event)->shouldReturn(null);
$event->getData()->willReturn(array('badKeys' => ''))->shouldBeCalled();
$this->preBind($event)->shouldReturn(null);
}
}

View file

@ -13,13 +13,8 @@ namespace spec\Sylius\Bundle\PaymentBundle\Form\Type;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\PaymentBundle\Form\Type\EventListener\BuildPaymentMethodFeeCalculatorFormSubscriber;
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
use Sylius\Component\Payment\Calculator\FeeCalculatorInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -29,9 +24,9 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
class PaymentMethodTypeSpec extends ObjectBehavior
{
function let(ServiceRegistryInterface $feeCalculatorRegistry)
function let()
{
$this->beConstructedWith('PaymentMethod', array('sylius'), $feeCalculatorRegistry);
$this->beConstructedWith('PaymentMethod', array('sylius'));
}
function it_is_a_form_type()
@ -40,40 +35,21 @@ class PaymentMethodTypeSpec extends ObjectBehavior
}
function it_builds_form_with_proper_fields(
$feeCalculatorRegistry,
FeeCalculatorInterface $feeCalculatorTest,
Form $form,
FormBuilder $builder,
FormFactoryInterface $formFactory
FormBuilder $builder
) {
$builder->getFormFactory()->willReturn($formFactory)->shouldBeCalled();
$builder
->add('translations', 'a2lix_translationsForms', Argument::any())
->shouldBeCalled()
->willReturn($builder);
$builder
->add('enabled', 'checkbox', Argument::type('array'))
->add('enabled', 'checkbox', Argument::any())
->willReturn($builder)
->shouldBeCalled()
;
$builder
->add('gateway', 'sylius_payment_gateway_choice', Argument::type('array'))
->add('gateway', 'sylius_payment_gateway_choice', Argument::any())
->willReturn($builder)
->shouldBeCalled()
;
$builder
->add('feeCalculator', 'sylius_fee_calculator_choice', Argument::type('array'))
->willReturn($builder)
->shouldBeCalled()
;
$builder
->addEventSubscriber(Argument::type(BuildPaymentMethodFeeCalculatorFormSubscriber::class))
->shouldBeCalled()
;
$builder
@ -81,14 +57,6 @@ class PaymentMethodTypeSpec extends ObjectBehavior
->willReturn($builder)
;
$feeCalculatorRegistry->all()->willReturn(array('test' => $feeCalculatorTest))->shouldBeCalled();
$feeCalculatorTest->getType()->willReturn('test')->shouldBeCalled();
$builder->create('feeCalculatorConfiguration', 'sylius_fee_calculator_test')->willReturn($builder)->shouldBeCalled();
$builder->getForm()->willReturn($form)->shouldBeCalled();
$builder->setAttribute('feeCalculatorsConfigurations', array('test' => $form))->willReturn($builder)->shouldBeCalled();
$this->buildForm($builder, array());
}

View file

@ -313,8 +313,6 @@ sylius:
no_promotion: No promotion applied
no_shipping_charges: No shipping charges applied
no_taxes: No taxes applied
payment_charges: Payment charges
payment_total: Payment total
promotion_discount: Promotion discounts
promotion_total: Promotion total
shipping_address: Shipping address

View file

@ -2,16 +2,6 @@
{% from 'SyliusResourceBundle:Macros:actions.html.twig' import update %}
{% block javascripts %}
{{ parent() }}
{% javascripts output='assets/compiled/backend_payment.js'
'bundles/syliuspayment/js/prototype-handler.js'
'bundles/syliuspayment/js/sylius-payment-method.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
{% block topbar %}
<ol class="breadcrumb">
<li>{{ 'sylius.breadcrumb.sales'|trans }}</li>

View file

@ -5,20 +5,6 @@
{{ form_row(form.code) }}
{{ form_row(form.gateway) }}
{{ form_row(form.enabled) }}
<div class="col-xs-12">
{{ form_row(form.feeCalculator) }}
{% for name, feeCalculatorConfiguration in form.vars.feeCalculatorsConfigurations%}
<div id="{{ form.feeCalculator.vars.id }}_{{ name }}" data-container="#fee_calculator.configuration" data-prototype="{{ form_widget(feeCalculatorConfiguration)|e }}">
</div>
{% endfor %}
<div id="fee_calculator" class="configuration">
{% if form.feeCalculatorConfiguration is defined %}
{% for field in form.feeCalculatorConfiguration %}
{{ form_row(field) }}
{% endfor %}
{% endif %}
</div>
</div>
</fieldset>
{{ form_rest(form) }}

View file

@ -2,16 +2,6 @@
{% from 'SyliusResourceBundle:Macros:actions.html.twig' import create %}
{% block javascripts %}
{{ parent() }}
{% javascripts output='assets/compiled/backend_payment.js'
'bundles/syliuspayment/js/prototype-handler.js'
'bundles/syliuspayment/js/sylius-payment-method.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
{% block topbar %}
<ol class="breadcrumb">
<li>{{ 'sylius.breadcrumb.configuration'|trans }}</li>

View file

@ -2,16 +2,6 @@
{% from 'SyliusResourceBundle:Macros:actions.html.twig' import update %}
{% block javascripts %}
{{ parent() }}
{% javascripts output='assets/compiled/backend_payment.js'
'bundles/syliuspayment/js/prototype-handler.js'
'bundles/syliuspayment/js/sylius-payment-method.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
{% block topbar %}
<ol class="breadcrumb">
<li>{{ 'sylius.breadcrumb.configuration'|trans }}</li>

View file

@ -163,25 +163,3 @@
{% endif %}
{% endspaceless %}
{% endblock %}
{% block sylius_payment_method_choice_widget %}
{% spaceless %}
{% if expanded %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
<div>
{% if paymentCosts[child.vars.value] is defined %}
<span class="pull-right text-muted">{{ paymentCosts[child.vars.value]|sylius_price }}</span>
{% endif %}
<label{% for attrname, attrvalue in child.vars.label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{{ form_widget(child) }}
{{ child.vars.label|trans({}, translation_domain) }}
</label>
</div>
{% endfor %}
</div>
{% else %}
{{- block('choice_widget_collapsed') -}}
{% endif %}
{% endspaceless %}
{% endblock %}

View file

@ -3,7 +3,6 @@
{% set taxAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::TAX_ADJUSTMENT') %}
{% set shippingAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::SHIPPING_ADJUSTMENT') %}
{% set promotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::PROMOTION_ADJUSTMENT') %}
{% set paymentAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::PAYMENT_ADJUSTMENT') %}
{% block content %}
<div class="page-header">
@ -104,23 +103,6 @@
</td>
</tr>
{% endif %}
<tr>
<td colspan="4">
<p><strong>{{ 'sylius.checkout.finalize.order.payment_charges'|trans }}</strong></p>
<ul>
{% for adjustment in order.adjustments(paymentAdjustment) %}
<li>{{ adjustment.description }} {{ adjustment.amount|sylius_price }} </li>
{% else %}
<li><span class="label label-info">{{ 'sylius.checkout.finalize.order.no_payment_charges'|trans }}</span></li>
{% endfor %}
</ul>
</td>
<td colspan="2">
<span class="pull-right">
<strong>{{ 'sylius.checkout.finalize.order.payment_total'|trans }}</strong>: {{ order.adjustmentsTotal(paymentAdjustment)|sylius_price }}
</span>
</td>
</tr>
<tr style="background-color: #f1f1f1;">
<td colspan="6">
<span class="pull-right">

View file

@ -11,9 +11,12 @@
{{ form_errors(form) }}
<fieldset>
{{ form_row(form.paymentMethod) }}
</fieldset>
{{ form_label(form.paymentMethod) }}
<fieldset>
{% for paymentMethodForm in form.paymentMethod %}
{{ form_row(paymentMethodForm) }}
{% endfor %}
</fieldset>
{{ form_widget(form._token) }}

View file

@ -15,9 +15,8 @@ use Sylius\Component\Order\Model\AdjustmentInterface as BaseAdjustmentInterface;
interface AdjustmentInterface extends BaseAdjustmentInterface
{
// Labels for tax, shipping, payment and promotion adjustments.
// Labels for tax, shipping and promotion adjustments.
const TAX_ADJUSTMENT = 'tax';
const SHIPPING_ADJUSTMENT = 'shipping';
const PROMOTION_ADJUSTMENT = 'promotion';
const PAYMENT_ADJUSTMENT = "payment";
}

View file

@ -1,87 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Core\OrderProcessing;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\Calculator\DelegatingFeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class PaymentChargesProcessor implements PaymentChargesProcessorInterface
{
/**
* @var FactoryInterface
*/
protected $adjustmentFactory;
/**
* @var DelegatingFeeCalculatorInterface
*/
protected $feeCalculator;
/**
* Constructor.
*
* @param FactoryInterface $adjustmentFactory
* @param DelegatingFeeCalculatorInterface $feeCalculator
*/
public function __construct(FactoryInterface $adjustmentFactory, DelegatingFeeCalculatorInterface $feeCalculator)
{
$this->adjustmentFactory = $adjustmentFactory;
$this->feeCalculator = $feeCalculator;
}
/**
* @param OrderInterface $order
*/
public function applyPaymentCharges(OrderInterface $order)
{
$order->removeAdjustments(AdjustmentInterface::PAYMENT_ADJUSTMENT);
$order->calculateTotal();
foreach ($order->getPayments() as $payment) {
$this->addAdjustmentIfForNotCancelled($order, $payment);
}
}
/**
* @param OrderInterface $order
* @param PaymentSubjectInterface $payment
*/
private function addAdjustmentIfForNotCancelled(OrderInterface $order, PaymentSubjectInterface $payment)
{
if (PaymentInterface::STATE_CANCELLED !== $payment->getState())
{
$order->addAdjustment($this->prepareAdjustmentForOrder($payment));
}
}
/**
* @param PaymentSubjectInterface $payment
*
* @return AdjustmentInterface
*/
private function prepareAdjustmentForOrder(PaymentSubjectInterface $payment)
{
$adjustment = $this->adjustmentFactory->createNew();
$adjustment->setType(AdjustmentInterface::PAYMENT_ADJUSTMENT);
$adjustment->setAmount($this->feeCalculator->calculate($payment));
$adjustment->setDescription($payment->getMethod()->getName());
return $adjustment;
}
}

View file

@ -1,25 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Core\OrderProcessing;
use Sylius\Component\Core\Model\OrderInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
interface PaymentChargesProcessorInterface
{
/**
* @param OrderInterface $order
*/
public function applyPaymentCharges(OrderInterface $order);
}

View file

@ -11,7 +11,6 @@
namespace Sylius\Component\Core\OrderProcessing;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
@ -31,22 +30,14 @@ class PaymentProcessor implements PaymentProcessorInterface
*/
protected $paymentFactory;
/**
* Payment Manager
*
* @var ObjectManager
*/
protected $paymentManager;
/**
* Constructor.
*
* @param FactoryInterface $paymentFactory
*/
public function __construct(FactoryInterface $paymentFactory, ObjectManager $paymentManager)
public function __construct(FactoryInterface $paymentFactory)
{
$this->paymentFactory = $paymentFactory;
$this->paymentManager = $paymentManager;
}
/**
@ -54,8 +45,6 @@ class PaymentProcessor implements PaymentProcessorInterface
*/
public function createPayment(OrderInterface $order)
{
$this->updateExistingPaymentsStates($order);
/** @var $payment PaymentInterface */
$payment = $this->paymentFactory->createNew();
$payment->setCurrency($order->getCurrency());
@ -65,26 +54,4 @@ class PaymentProcessor implements PaymentProcessorInterface
return $payment;
}
/**
* @param OrderInterface $order
*/
private function updateExistingPaymentsStates(OrderInterface $order)
{
foreach ($order->getPayments() as $payment) {
$this->cancelPaymentStateIfNotStarted($payment);
}
$this->paymentManager->flush();
}
/**
* @param PaymentInterface $payment
*/
private function cancelPaymentStateIfNotStarted(PaymentInterface $payment)
{
if (PaymentInterface::STATE_NEW === $payment->getState()) {
$payment->setState(PaymentInterface::STATE_CANCELLED);
}
}
}

View file

@ -1,74 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Core\OrderProcessing;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderProcessing\PaymentChargesProcessorInterface;
use Sylius\Component\Payment\Calculator\DelegatingFeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
/**
* @mixin \Sylius\Component\Core\OrderProcessing\PaymentChargesProcessor
*
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
class PaymentChargesProcessorSpec extends ObjectBehavior
{
function let(FactoryInterface $adjustmentFactory, DelegatingFeeCalculatorInterface $delegatingFeeCalculator)
{
$this->beConstructedWith($adjustmentFactory, $delegatingFeeCalculator);
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Core\OrderProcessing\PaymentChargesProcessor');
}
function it_implements_payment_charges_processor_interface()
{
$this->shouldImplement(PaymentChargesProcessorInterface::class);
}
function it_applies_payment_charges(
$adjustmentFactory,
$delegatingFeeCalculator,
AdjustmentInterface $adjustment,
OrderInterface $order,
PaymentSubjectInterface $payment,
PaymentMethodInterface $paymentMethod
) {
$order->removeAdjustments('payment')->shouldBeCalled();
$order->getPayments()->willReturn(array($payment))->shouldBeCalled();
$order->calculateTotal()->shouldBeCalled();
$payment->getState()->willReturn('new')->shouldBeCalled();
$payment->getMethod()->willReturn($paymentMethod);
$paymentMethod->getName()->willReturn('testPaymentMethod');
$delegatingFeeCalculator->calculate($payment)->willReturn(50);
$adjustmentFactory->createNew()->willReturn($adjustment)->shouldBeCalled();
$adjustment->setType('payment')->shouldBeCalled();
$adjustment->setAmount(50)->shouldBeCalled();
$adjustment->setDescription('testPaymentMethod')->shouldBeCalled();
$order->addAdjustment($adjustment)->shouldBeCalled();
$this->applyPaymentCharges($order);
}
}

View file

@ -1,86 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Core\OrderProcessing;
use Doctrine\Common\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\OrderProcessing\PaymentProcessorInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
/**
* @mixin \Sylius\Component\Core\OrderProcessing\PaymentProcessor
*
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class PaymentProcessorSpec extends ObjectBehavior
{
function let(FactoryInterface $paymentFactory, ObjectManager $paymentManager)
{
$this->beConstructedWith($paymentFactory, $paymentManager);
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Core\OrderProcessing\PaymentProcessor');
}
function it_implements_payment_processor_interface()
{
$this->shouldImplement(PaymentProcessorInterface::class);
}
function it_creates_payment(
$paymentFactory,
OrderInterface $order,
PaymentInterface $payment
) {
$order->getPayments()->willReturn(array())->shouldBeCalled();
$order->getCurrency()->willReturn('EUR')->shouldBeCalled();
$order->getTotal()->willReturn(100)->shouldBeCalled();
$paymentFactory->createNew()->willReturn($payment)->shouldBeCalled();
$payment->setCurrency('EUR')->shouldBeCalled();
$payment->setAmount(100)->shouldBeCalled();
$order->addPayment($payment)->shouldBeCalled();
$this->createPayment($order)->shouldReturn($payment);
}
function it_sets_not_started_payments_as_cancelled_while_creating_payment(
$paymentManager,
$paymentFactory,
OrderInterface $order,
PaymentInterface $existingPayment,
PaymentInterface $payment
) {
$existingPayment->getState()->willReturn('new');
$order->getPayments()->willReturn(array($existingPayment))->shouldBeCalled();
$existingPayment->setState('cancelled')->shouldBeCalled();
$paymentManager->flush()->shouldBeCalled();
$order->getCurrency()->willReturn('EUR')->shouldBeCalled();
$order->getTotal()->willReturn(100)->shouldBeCalled();
$paymentFactory->createNew()->willReturn($payment)->shouldBeCalled();
$payment->setCurrency('EUR')->shouldBeCalled();
$payment->setAmount(100)->shouldBeCalled();
$order->addPayment($payment)->shouldBeCalled();
$this->createPayment($order)->shouldReturn($payment);
}
}

View file

@ -1,22 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class DefaultFeeCalculators
{
const FIXED = 'fixed';
const PERCENT = 'percent';
}

View file

@ -1,49 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class DelegatingFeeCalculator implements DelegatingFeeCalculatorInterface
{
/**
* @var ServiceRegistryInterface
*/
private $serviceRegistry;
/**
* @param ServiceRegistryInterface $serviceRegistry
*/
public function __construct(ServiceRegistryInterface $serviceRegistry)
{
$this->serviceRegistry = $serviceRegistry;
}
/**
* {@inheritdoc}
*/
public function calculate(PaymentSubjectInterface $payment)
{
if (null === $payment->getMethod()) {
throw new \InvalidArgumentException("Cannot calculate fee for payment without payment method configured.");
}
/** @var FeeCalculatorInterface $feeCalculator */
$feeCalculator = $this->serviceRegistry->get($payment->getMethod()->getFeeCalculator());
return $feeCalculator->calculate($payment, $payment->getMethod()->getFeeCalculatorConfiguration());
}
}

View file

@ -1,27 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
interface DelegatingFeeCalculatorInterface
{
/**
* @param PaymentSubjectInterface $payment
*
* @return int
*/
public function calculate(PaymentSubjectInterface $payment);
}

View file

@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
interface FeeCalculatorInterface
{
/**
* @param PaymentSubjectInterface $payment
* @param array $configuration
*
* @return integer
*/
public function calculate(PaymentSubjectInterface $payment, array $configuration);
/**
* @return string
*/
public function getType();
}

View file

@ -1,36 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class FixedFeeCalculator implements FeeCalculatorInterface
{
/**
* {@inheritdoc}
*/
public function calculate(PaymentSubjectInterface $payment, array $configuration)
{
return (int) $configuration['amount'];
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'fixed';
}
}

View file

@ -1,36 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Calculator;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.p.zalewski@gmail.com>
*/
class PercentFeeCalculator implements FeeCalculatorInterface
{
/**
* {@inheritdoc}
*/
public function calculate(PaymentSubjectInterface $payment, array $configuration)
{
return (int) round(($configuration['percent'])/100 * $payment->getAmount());
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'percent';
}
}

View file

@ -16,7 +16,7 @@ use Sylius\Component\Resource\Exception\UnexpectedTypeException;
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
class Payment implements PaymentInterface, PaymentSubjectInterface
class Payment implements PaymentInterface
{
/**
* @var mixed

View file

@ -11,7 +11,6 @@
namespace Sylius\Component\Payment\Model;
use Sylius\Component\Payment\Calculator\DefaultFeeCalculators;
use Sylius\Component\Translation\Model\AbstractTranslatable;
/**
@ -54,16 +53,6 @@ class PaymentMethod extends AbstractTranslatable implements PaymentMethodInterfa
*/
protected $environment;
/**
* @var string
*/
protected $feeCalculator = DefaultFeeCalculators::FIXED;
/**
* @var array
*/
protected $feeCalculatorConfiguration = array();
/**
* @var \DateTime
*/
@ -197,38 +186,6 @@ class PaymentMethod extends AbstractTranslatable implements PaymentMethodInterfa
$this->environment = $environment;
}
/**
* {@inheritdoc}
*/
public function getFeeCalculator()
{
return $this->feeCalculator;
}
/**
* {@inheritdoc}
*/
public function setFeeCalculator($feeCalculator)
{
$this->feeCalculator = $feeCalculator;
}
/**
* {@inheritdoc}
*/
public function getFeeCalculatorConfiguration()
{
return $this->feeCalculatorConfiguration;
}
/**
* {@inheritdoc}
*/
public function setFeeCalculatorConfiguration(array $feeCalculatorConfiguration)
{
$this->feeCalculatorConfiguration = $feeCalculatorConfiguration;
}
/**
* {@inheritdoc}
*/

View file

@ -72,24 +72,4 @@ interface PaymentMethodInterface extends CodeAwareInterface, TimestampableInterf
* @param string $environment
*/
public function setEnvironment($environment);
/**
* @return string
*/
public function getFeeCalculator();
/**
* @param string $feeCalculator
*/
public function setFeeCalculator($feeCalculator);
/**
* @return array
*/
public function getFeeCalculatorConfiguration();
/**
* @param array $feeCalculatorConfiguration
*/
public function setFeeCalculatorConfiguration(array $feeCalculatorConfiguration);
}

View file

@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Component\Payment\Model;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
interface PaymentSubjectInterface
{
/**
* @return PaymentMethodInterface
*/
public function getMethod();
/**
* @return integer
*/
public function getAmount();
/**
* @return string
*/
public function getState();
}

View file

@ -1,65 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Payment\Calculator;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Payment\Calculator\DelegatingFeeCalculatorInterface;
use Sylius\Component\Payment\Calculator\FeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class DelegatingFeeCalculatorSpec extends ObjectBehavior
{
function let(ServiceRegistryInterface $serviceRegistry)
{
$this->beConstructedWith($serviceRegistry);
}
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Payment\Calculator\DelegatingFeeCalculator');
}
function it_implements_delegating_fee_calculator_interface()
{
$this->shouldImplement(DelegatingFeeCalculatorInterface::class);
}
function it_delegates_calculation_to_proper_calculator(
PaymentSubjectInterface $payment,
PaymentMethodInterface $paymentMethod,
FeeCalculatorInterface $feeCalculator,
$serviceRegistry
) {
$payment->getMethod()->willReturn($paymentMethod)->shouldBeCalled();
$paymentMethod->getFeeCalculator()->willReturn('fee_calculator')->shouldBeCalled();
$paymentMethod->getFeeCalculatorConfiguration()->willReturn(array('amount' => 150))->shouldBeCalled();
$serviceRegistry->get('fee_calculator')->willReturn($feeCalculator)->shouldBeCalled();
$feeCalculator->calculate($payment, array('amount' => 150))->willReturn(150)->shouldBeCalled();
$this->calculate($payment)->shouldReturn(150);
}
function it_throws_exception_if_passed_payment_has_no_payment_method_defined(PaymentSubjectInterface $payment)
{
$payment->getMethod()->willReturn(null);
$this->shouldThrow(new \InvalidArgumentException("Cannot calculate fee for payment without payment method configured."))->during("calculate", array($payment));
}
}

View file

@ -1,42 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Payment\Calculator;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Payment\Calculator\FeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class FixedFeeCalculatorSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Payment\Calculator\FixedFeeCalculator');
}
function it_implements_sylius_fee_calculator_interface()
{
$this->shouldImplement(FeeCalculatorInterface::class);
}
function it_calculates_fee(PaymentSubjectInterface $payment)
{
$this->calculate($payment, array('amount' => 15))->shouldReturn(15);
}
function it_has_type()
{
$this->getType()->shouldReturn('fixed');
}
}

View file

@ -1,44 +0,0 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Payment\Calculator;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Payment\Calculator\FeeCalculatorInterface;
use Sylius\Component\Payment\Model\PaymentSubjectInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class PercentFeeCalculatorSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Sylius\Component\Payment\Calculator\PercentFeeCalculator');
}
function it_implement_fee_calculator_interface()
{
$this->shouldImplement(FeeCalculatorInterface::class);
}
function it_calculates_fee_for_given_payment_with_given_configuration(PaymentSubjectInterface $payment)
{
$payment->getAmount()->willReturn(1000);
$this->calculate($payment, array('percent' => 20))->shouldReturn(200);
}
function it_has_type()
{
$this->getType()->shouldReturn('percent');
}
}

View file

@ -116,18 +116,4 @@ class PaymentMethodSpec extends ObjectBehavior
{
$this->getUpdatedAt()->shouldReturn(null);
}
function its_fee_calculator_is_mutable()
{
$this->getFeeCalculator()->shouldReturn('fixed');
$this->setFeeCalculator('test_calculator');
$this->getFeeCalculator()->shouldReturn('test_calculator');
}
function its_fee_calculator_configuration_is_mutable()
{
$this->setFeeCalculatorConfiguration(array('amount' => 10));
$this->getFeeCalculatorConfiguration()->shouldReturn(array('amount' => 10));
}
}

View file

@ -31,11 +31,6 @@ class PaymentSpec extends ObjectBehavior
$this->shouldImplement(PaymentInterface::class);
}
function it_implements_sylius_subject_interface()
{
$this->shouldImplement('Sylius\Component\Payment\Model\PaymentSubjectInterface');
}
function it_has_no_id_by_default()
{
$this->getId()->shouldReturn(null);