Skip to content

Commit

Permalink
[Payment] Fixes after PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jul 27, 2016
1 parent ef79fcd commit bdf1b88
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ winzou_state_machine:
on: ["complete"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object", "event", "'create'", "'sylius_order'"]
sylius_create_payment:
on: ["complete"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object.getPayments()", "event", "'create'", "'sylius_payment'"]
sylius_create_payment:
on: ["complete"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object.getPayments()", "event", "'request_payment'", "'sylius_order_payment'"]

sylius_order_shipping:
class: "%sylius.model.order.class%"
Expand Down Expand Up @@ -136,6 +128,14 @@ winzou_state_machine:
on: ["fulfill"]
do: ["@sylius.email_manager.order", "sendConfirmationEmail"]
args: ["object"]
sylius_create_payment:
on: ["create"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object.getPayments()", "event", "'create'", "'sylius_payment'"]
sylius_create_payment:
on: ["create"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object.getPayments()", "event", "'request_payment'", "'sylius_order_payment'"]
sylius_cancel_payment:
on: ["cancel"]
do: ["@sm.callback.cascade_transition", "apply"]
Expand Down
4 changes: 3 additions & 1 deletion src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@
<argument type="service" id="sylius.factory.order_item_unit" />
<argument type="service" id="sm.factory" />
</service>
<service id="sylius.order_processing.state_resolver" class="%sylius.order_processing.state_resolver.class%" />
<service id="sylius.order_processing.state_resolver" class="%sylius.order_processing.state_resolver.class%">
<argument type="service" id="sm.factory" />
</service>
<service id="sylius.order_processing.shipment_processor" class="%sylius.order_processing.shipment_processor.class%">
<argument type="service" id="sylius.resolver.default_shipping_method" />
<argument type="service" id="sylius.factory.shipment" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @author Alexandre Bacco <alexandre.bacco@gmail.com>
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
class OrderPaymentCallback
{
Expand All @@ -42,22 +43,16 @@ public function updateOrderOnPayment(PaymentInterface $payment)
throw new \RuntimeException(sprintf('Cannot retrieve Order from Payment with id %s', $payment->getId()));
}

$total = 0;
if (PaymentInterface::STATE_COMPLETED === $payment->getState()) {
$payments = $order->getPayments()->filter(function (PaymentInterface $payment) {
return PaymentInterface::STATE_COMPLETED === $payment->getState();
});
$payments = $order->getPayments()->filter(function (PaymentInterface $payment) {
return PaymentInterface::STATE_COMPLETED === $payment->getState();
});

if ($payments->count() === $order->getPayments()->count()) {
$order->setPaymentState(PaymentInterface::STATE_COMPLETED);
}

$total += $payment->getAmount();
} else {
$order->setPaymentState($payment->getState());
$completedPaymentTotal = 0;
foreach ($payments as $payment) {
$completedPaymentTotal += $payment->getAmount();
}

if ($total >= $order->getTotal()) {
if ($completedPaymentTotal >= $order->getTotal()) {
$this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::TRANSITION_FULFILL, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace spec\Sylius\Bundle\CoreBundle\StateMachineCallback;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
Expand All @@ -22,12 +21,13 @@

/**
* @author Alexandre Bacco <alexandre.bacco@gmail.com>
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
final class OrderPaymentCallbackSpec extends ObjectBehavior
{
function let(FactoryInterface $factory)
function let(FactoryInterface $stateMachineFactory)
{
$this->beConstructedWith($factory);
$this->beConstructedWith($stateMachineFactory);
}

function it_is_initializable()
Expand All @@ -36,46 +36,21 @@ function it_is_initializable()
}

function it_dispatches_event_on_payment_update_and_will_update_order_state(
$factory,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
PaymentInterface $payment,
OrderInterface $order,
StateMachineInterface $sm
) {
$payment->getOrder()->willReturn($order);
$payment->getState()->willReturn(PaymentInterface::STATE_CANCELLED);

$order->getTotal()->willReturn(0);
$order->setPaymentState(PaymentInterface::STATE_CANCELLED)->shouldBeCalled();

$factory->get($order, OrderTransitions::GRAPH)->willReturn($sm);
$sm->apply(OrderTransitions::TRANSITION_FULFILL, true)->shouldBeCalled();

$this->updateOrderOnPayment($payment);
}

function it_dispatches_event_on_payment_update(
$factory,
PaymentInterface $payment,
OrderInterface $order,
StateMachineInterface $sm,
Collection $payments,
Collection $filteredPayments
OrderInterface $order
) {
$payment->getOrder()->willReturn($order);
$payment->getState()->willReturn(PaymentInterface::STATE_COMPLETED);
$payment->getAmount()->willReturn(1000);
$payment->getAmount()->willReturn(100);
$payments = new ArrayCollection([$payment->getWrappedObject()]);

$order->getPayments()->willReturn($payments);
$order->getTotal()->willReturn(1000);
$order->setPaymentState(PaymentInterface::STATE_COMPLETED)->shouldBeCalled();

$payments->filter(Argument::any())->willReturn($filteredPayments);
$payments->count()->willReturn(1);

$filteredPayments->count()->willReturn(1);
$order->getTotal()->willReturn(100);

$factory->get($order, OrderTransitions::GRAPH)->willReturn($sm);
$sm->apply(OrderTransitions::TRANSITION_FULFILL, true)->shouldBeCalled();
$stateMachineFactory->get($order, OrderTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(OrderTransitions::TRANSITION_FULFILL, true)->shouldBeCalled();

$this->updateOrderOnPayment($payment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ sylius:
calculator: Calculator
cancel: Cancel
cancelled: Cancelled
cart: Cart
cart_locked: 'Cart locked'
cart_summary: 'Cart summary'
categorization: Categorization
Expand Down Expand Up @@ -318,6 +319,7 @@ sylius:
forgot_password: 'Forgot password'
forgot_password: Forgot password
fulfill: Fulfill
fulfilled: Fulfilled
gateway: Gateway
general_info: 'General Info'
general_settings: 'General Settings'
Expand Down
2 changes: 0 additions & 2 deletions src/Sylius/Component/Core/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ public function addPayment(BasePaymentInterface $payment)
if (!$this->hasPayment($payment)) {
$this->payments->add($payment);
$payment->setOrder($this);

$this->setPaymentState($payment->getState());
}
}

Expand Down
39 changes: 34 additions & 5 deletions src/Sylius/Component/Core/OrderProcessing/StateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,40 @@

namespace Sylius\Component\Core\OrderProcessing;

use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderShippingStates;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;

/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
class StateResolver implements StateResolverInterface
{
/**
* @var FactoryInterface
*/
private $stateMachineFactory;

/**
* @param FactoryInterface $stateMachineFactory
*/
public function __construct(FactoryInterface $stateMachineFactory)
{
$this->stateMachineFactory = $stateMachineFactory;
}

/**
* {@inheritdoc}
*/
public function resolvePaymentState(OrderInterface $order)
{
$stateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);

if ($order->hasPayments()) {
$payments = $order->getPayments();
$completedPaymentTotal = 0;
Expand All @@ -38,25 +56,25 @@ public function resolvePaymentState(OrderInterface $order)
}

if (OrderInterface::STATE_CANCELLED === $order->getState()) {
$order->setPaymentState(OrderPaymentStates::STATE_CANCELLED);
$this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_CANCEL);

return;
}

if (OrderInterface::STATE_FULFILLED === $order->getState() && $completedPaymentTotal >= $order->getTotal()) {
$order->setPaymentState(OrderPaymentStates::STATE_PAID);
$this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_PAY);

return;
}

if ($completedPaymentTotal < $order->getTotal() && 0 < $completedPaymentTotal) {
$order->setPaymentState(OrderPaymentStates::STATE_PARTIALLY_PAID);
$this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_PARTIALLY_PAY);

return;
}
}

$order->setPaymentState(OrderPaymentStates::STATE_AWAITING_PAYMENT);
$this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_REQUEST_PAYMENT);
}

/**
Expand Down Expand Up @@ -102,4 +120,15 @@ protected function getShippingState(OrderInterface $order)

return OrderShippingStates::PARTIALLY_SHIPPED;
}

/**
* @param StateMachineInterface $stateMachine
* @param string $transition
*/
private function applyTransition(StateMachineInterface $stateMachine, $transition)
{
if ($stateMachine->can($transition)) {
$stateMachine->apply($transition);
}
}
}
Loading

0 comments on commit bdf1b88

Please sign in to comment.