Skip to content

Commit

Permalink
Use messenger to be able to make Stripe API call in the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jan 10, 2024
1 parent 50c3f36 commit 0daaa63
Show file tree
Hide file tree
Showing 21 changed files with 449 additions and 250 deletions.
24 changes: 24 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# UPGRADE FROM `v2.0.8` to `v2.0.9`

This class has been renamed :

- `\FluxSE\SyliusPayumStripePlugin\StateMachine\CompleteAuthorizedOrderProcessor`
to `\FluxSE\SyliusPayumStripePlugin\StateMachine\CaptureAuthorizedOrderProcessor`

This service has been renamed :

- `flux_se.sylius_payum_stripe.state_machine.complete_authorized`
to `flux_se.sylius_payum_stripe.state_machine.capture_authorized`

# UPGRADE FROM `v2.0.7` to `v2.0.8`

This class has been renamed :

- `\FluxSE\SyliusPayumStripePlugin\StateMachine\CancelAuthorizedOrderProcessor`
to `\FluxSE\SyliusPayumStripePlugin\StateMachine\CancelOrderProcessor`

This service has been renamed :

- `flux_se.sylius_payum_stripe.state_machine.cancel_authorized`
to `flux_se.sylius_payum_stripe.state_machine.cancel`

# UPGRADE FROM `v1.2` TO `v2.0.0`

You will have to create or edit the configuration file :
Expand Down
6 changes: 3 additions & 3 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@

<DeprecatedClass>
<errorLevel type="info">
<file name="src/StateMachine/AbstractOrderProcessor.php"/>
<file name="src/CommandHandler/AbstractPayumPaymentHandler.php"/>
</errorLevel>
</DeprecatedClass>

<DeprecatedMethod>
<errorLevel type="info">
<file name="src/StateMachine/AbstractOrderProcessor.php"/>
<file name="src/CommandHandler/AbstractPayumPaymentHandler.php"/>
</errorLevel>
</DeprecatedMethod>

Expand All @@ -59,7 +59,7 @@
</MixedArgumentTypeCoercion>
<DeprecatedInterface>
<errorLevel type="info">
<file name="src/StateMachine/AbstractOrderProcessor.php" />
<file name="src/CommandHandler/AbstractPayumPaymentHandler.php" />
</errorLevel>
</DeprecatedInterface>
</issueHandlers>
Expand Down
74 changes: 35 additions & 39 deletions spec/StateMachine/CancelOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,64 @@

namespace spec\FluxSE\SyliusPayumStripePlugin\StateMachine;

use FluxSE\SyliusPayumStripePlugin\Factory\CancelRequestFactoryInterface;
use Payum\Core\GatewayInterface;
use Payum\Core\Model\ModelAggregateInterface;
use Payum\Core\Payum;
use Payum\Core\Security\TokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use FluxSE\SyliusPayumStripePlugin\Command\CancelPayment;
use PhpSpec\ObjectBehavior;
use SM\Event\TransitionEvent;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class CancelOrderProcessorSpec extends ObjectBehavior
{
public function let(
CancelRequestFactoryInterface $cancelRequestFactory,
Payum $payum
public function let(MessageBusInterface $commandBus): void {
$this->beConstructedWith($commandBus);
}

public function it_is_invokable_when_it_is_new(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus,
): void {
$this->beConstructedWith($cancelRequestFactory, $payum);
$event->getState()->willReturn(PaymentInterface::STATE_NEW);

$payment->getId()->willReturn(1);

$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
}

public function it_is_invokable(
Payum $payum,
public function it_is_invokable_when_is_authorized(
PaymentInterface $payment,
TransitionEvent $event,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig,
GatewayInterface $gateway,
TokenFactoryInterface $tokenFactory,
TokenInterface $token,
CancelRequestFactoryInterface $cancelRequestFactory,
ModelAggregateInterface $request
MessageBusInterface $commandBus
): void {

$payment->getMethod()->willReturn($paymentMethod);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['factory' => 'stripe_checkout_session']);
$gatewayName = 'stripe_checkout_session_with_sca';
$gatewayConfig->getGatewayName()->willReturn($gatewayName);
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$payum->getGateway($gatewayName)->willReturn($gateway);
$payment->getId()->willReturn(1);

$payum->getTokenFactory()->willReturn($tokenFactory);
$tokenFactory->createToken($gatewayName, $payment, 'payum_notify_do')->willReturn($token);
$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$request->beConstructedWith([$token]);
$cancelRequestFactory->createNewWithToken($token)->willReturn($request);
$this->__invoke($payment, $event);
}

$gateway->execute($request)->shouldBeCalled();
public function it_do_nothing_when_it_is_completed(
PaymentInterface $payment,
TransitionEvent $event
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
}

public function it_do_nothing_when_gateway_is_unknown(
public function it_do_nothing_when_it_is_refunded(
PaymentInterface $payment,
TransitionEvent $event,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig
TransitionEvent $event
): void {
$payment->getMethod()->willReturn($paymentMethod);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['factory' => 'foo']);
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
}
Expand Down
52 changes: 52 additions & 0 deletions spec/StateMachine/CaptureAuthorizedOrderProcessorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace spec\FluxSE\SyliusPayumStripePlugin\StateMachine;

use FluxSE\SyliusPayumStripePlugin\Command\CaptureAuthorizedPayment;
use PhpSpec\ObjectBehavior;
use SM\Event\TransitionEvent;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class CaptureAuthorizedOrderProcessorSpec extends ObjectBehavior
{
public function let(MessageBusInterface $commandBus): void {
$this->beConstructedWith($commandBus);
}

public function it_is_invokable(
PaymentInterface $payment,
TransitionEvent $event,
MessageBusInterface $commandBus
): void {
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$payment->getId()->willReturn(1);

$command = new CaptureAuthorizedPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
}

public function it_do_nothing_when_it_is_completed(
PaymentInterface $payment,
TransitionEvent $event
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
}

public function it_do_nothing_when_it_is_refunded(
PaymentInterface $payment,
TransitionEvent $event
): void {
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
}
}
84 changes: 0 additions & 84 deletions spec/StateMachine/CompleteAuthorizedOrderProcessorSpec.php

This file was deleted.

61 changes: 10 additions & 51 deletions spec/StateMachine/RefundOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,40 @@

namespace spec\FluxSE\SyliusPayumStripePlugin\StateMachine;

use FluxSE\SyliusPayumStripePlugin\Factory\RefundRequestFactoryInterface;
use Payum\Core\GatewayInterface;
use Payum\Core\Model\ModelAggregateInterface;
use Payum\Core\Payum;
use Payum\Core\Security\TokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use FluxSE\SyliusPayumStripePlugin\Command\RefundPayment;
use PhpSpec\ObjectBehavior;
use SM\Event\TransitionEvent;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class RefundOrderProcessorSpec extends ObjectBehavior
{
public function let(
RefundRequestFactoryInterface $refundRequestFactory,
Payum $payum
): void {
$this->beConstructedWith($refundRequestFactory, $payum);
public function let(MessageBusInterface $commandBus): void {
$this->beConstructedWith($commandBus);
}

public function it_is_invokable(
Payum $payum,
PaymentInterface $payment,
TransitionEvent $event,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig,
GatewayInterface $gateway,
TokenFactoryInterface $tokenFactory,
TokenInterface $token,
RefundRequestFactoryInterface $refundRequestFactory,
ModelAggregateInterface $request
MessageBusInterface $commandBus
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$payment->getMethod()->willReturn($paymentMethod);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['factory' => 'stripe_checkout_session']);
$gatewayName = 'stripe_checkout_session_with_sca';
$gatewayConfig->getGatewayName()->willReturn($gatewayName);

$payum->getGateway($gatewayName)->willReturn($gateway);

$payum->getTokenFactory()->willReturn($tokenFactory);
$tokenFactory->createToken($gatewayName, $payment, 'payum_notify_do')->willReturn($token);

$request->beConstructedWith([$token]);
$refundRequestFactory->createNewWithToken($token)->willReturn($request);
$payment->getId()->willReturn(1);

$gateway->execute($request)->shouldBeCalled();
$command = new RefundPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
}

public function it_do_nothing_when_it_is_not_a_completed_state(
public function it_do_nothing_when_it_is_authorized(
PaymentInterface $payment,
TransitionEvent $event
): void {
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$this->__invoke($payment, $event);
}

public function it_do_nothing_when_gateway_is_unknown(
PaymentInterface $payment,
TransitionEvent $event,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$payment->getMethod()->willReturn($paymentMethod);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['factory' => 'foo']);

$this->__invoke($payment, $event);
}
}
Loading

0 comments on commit 0daaa63

Please sign in to comment.