Skip to content

Commit

Permalink
fix: reorder CREATE transitions between order and payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Atala committed Apr 16, 2018
1 parent 0220c07 commit d192144
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/AppBundle/Service/OrderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function pay(OrderInterface $order, $stripeToken)
public function create(OrderInterface $order)
{
$orderStateMachine = $this->stateMachineFactory->get($order, OrderTransitions::GRAPH);
$stripePayment = $order->getLastPayment(PaymentInterface::STATE_CART);
$paymentStateMachine = $this->stateMachineFactory->get($stripePayment, PaymentTransitions::GRAPH);
$paymentStateMachine->apply(PaymentTransitions::TRANSITION_CREATE);

// apply the order transition after because we need the payment in state new in the create order hook

This comment has been minimized.

Copy link
@alexsegura

alexsegura Apr 16, 2018

Member

I was about to write a test that verifies that transitions are applied in the expected order, but this is considered as a code smell. So we'd better manage this in another way.

I checked how it is done in Sylius Core.

When the checkout is completed, a transition is applied, and it cascades to create the payment.

Also, there a 2 OrderProcessor configured, one with each of the target states
The OrderPaymentProvider class is responsible for applying the necessary transitions

Well all of this is maybe king of too much complex for our use case (Sylius needs to decouple everything to allow customizing the checkout), but at least I'd prefer to use the state machine to apply the create transition on the payment.

I'm doing this right away, & also I'll introduce a functional test for the state machine.

This comment has been minimized.

Copy link
@alexsegura

alexsegura Apr 16, 2018

Member

See f558b06

I was about to try to write a functional state for the state machine, but then I realized that Stripe is involved.
Which means I would need to mock the service container.
Which is kind of deprecated since Symfony 3.2 😐

See #312 & #313

$orderStateMachine->apply(OrderTransitions::TRANSITION_CREATE);

$stripePayment = $order->getLastPayment(PaymentInterface::STATE_CART);
Expand Down Expand Up @@ -132,7 +137,7 @@ public function authorizePayment(OrderInterface $order)
{
Stripe\Stripe::setApiKey($this->settingsManager->get('stripe_secret_key'));

$stripePayment = $order->getLastPayment(PaymentInterface::STATE_CART);
$stripePayment = $order->getLastPayment(PaymentInterface::STATE_NEW);
$stripeToken = $stripePayment->getStripeToken();

if (null === $stripeToken) {
Expand Down

0 comments on commit d192144

Please sign in to comment.