Skip to content

Commit

Permalink
Merge pull request #43 from FLUX-SE/fix-already-canceled
Browse files Browse the repository at this point in the history
Cancel cleaning
  • Loading branch information
Prometee authored Jun 11, 2024
2 parents 3a84831 + d135928 commit 23bbc54
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FluxSE\PayumStripe\Token\TokenHashKeysInterface;
use Stripe\Event;
use Stripe\PaymentIntent;

final class AuthorizedPaymentIntentCanceledAction extends AbstractPaymentIntentAction
{
Expand All @@ -18,7 +19,7 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'manual';
return PaymentIntent::CAPTURE_METHOD_MANUAL;
}

public function getTokenHashMetadataKeyName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FluxSE\PayumStripe\Token\TokenHashKeysInterface;
use Stripe\Event;
use Stripe\PaymentIntent;

final class AuthorizedPaymentIntentManuallyCanceledAction extends AbstractPaymentIntentAction
{
Expand All @@ -18,7 +19,7 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'manual';
return PaymentIntent::CAPTURE_METHOD_MANUAL;
}

public function getTokenHashMetadataKeyName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FluxSE\PayumStripe\Token\TokenHashKeysInterface;
use Stripe\Event;
use Stripe\PaymentIntent;

final class AuthorizedPaymentIntentSucceededAction extends AbstractPaymentIntentAction
{
Expand All @@ -18,7 +19,7 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'manual';
return PaymentIntent::CAPTURE_METHOD_MANUAL;
}

public function getTokenHashMetadataKeyName(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Action/Api/WebhookEvent/PaymentIntentCanceledAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FluxSE\PayumStripe\Action\Api\WebhookEvent;

use Stripe\Event;
use Stripe\PaymentIntent;

final class PaymentIntentCanceledAction extends AbstractPaymentIntentAction
{
Expand All @@ -17,6 +18,6 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'automatic';
return PaymentIntent::CAPTURE_METHOD_AUTOMATIC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FluxSE\PayumStripe\Action\Api\WebhookEvent;

use Stripe\Event;
use Stripe\PaymentIntent;

final class PaymentIntentCanceledFromAuthorizeAction extends AbstractPaymentIntentAction
{
Expand All @@ -17,6 +18,6 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'manual';
return PaymentIntent::CAPTURE_METHOD_MANUAL;
}
}
3 changes: 2 additions & 1 deletion src/Action/Api/WebhookEvent/PaymentIntentSucceededAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FluxSE\PayumStripe\Action\Api\WebhookEvent;

use Stripe\Event;
use Stripe\PaymentIntent;

final class PaymentIntentSucceededAction extends AbstractPaymentIntentAction
{
Expand All @@ -17,6 +18,6 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'automatic';
return PaymentIntent::CAPTURE_METHOD_AUTOMATIC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FluxSE\PayumStripe\Action\Api\WebhookEvent;

use Stripe\Event;
use Stripe\PaymentIntent;

final class PaymentIntentSucceededFromAuthorizeAction extends AbstractPaymentIntentAction
{
Expand All @@ -17,6 +18,6 @@ protected function getSupportedEventTypes(): array

protected function getSupportedCaptureMethod(): string
{
return 'manual';
return PaymentIntent::CAPTURE_METHOD_MANUAL;
}
}
8 changes: 6 additions & 2 deletions src/Action/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function supports($request): bool
return false;
}

// if capture_method=manual it means the payment intent was created from a checkout session with authorization
return $model->offsetExists('capture_method') && $model->offsetGet('capture_method') === 'manual';
if (!$model->offsetExists('object') || $model->offsetGet('object') !== PaymentIntent::OBJECT_NAME) {
return false;
}

// if capture_method=manual it means the payment intent was created with authorization
return $model->offsetExists('capture_method') && $model->offsetGet('capture_method') === PaymentIntent::CAPTURE_METHOD_MANUAL;
}
}
3 changes: 2 additions & 1 deletion src/Action/StripeCheckoutSession/AuthorizeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Payum\Core\Request\Authorize;
use Payum\Core\Request\Generic;
use Payum\Core\Security\TokenInterface;
use Stripe\PaymentIntent;

/**
* For more information about Stripe Authorize payments :.
Expand All @@ -34,7 +35,7 @@ public function embedOnModeData(ArrayObject $model, TokenInterface $token, strin

/** @var array $embeddedModeData */
$embeddedModeData = $model->offsetGet($modeDataKey);
$embeddedModeData['capture_method'] = 'manual';
$embeddedModeData['capture_method'] = PaymentIntent::CAPTURE_METHOD_MANUAL;
$model->offsetSet($modeDataKey, $embeddedModeData);
}

Expand Down
1 change: 0 additions & 1 deletion src/Action/StripeCheckoutSession/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ArrayAccess;
use FluxSE\PayumStripe\Request\Api\Resource\ExpireSession;
use FluxSE\PayumStripe\Request\Api\Resource\RetrieveSession;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
Expand Down
2 changes: 1 addition & 1 deletion src/Action/StripeCheckoutSession/LegacyCancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function supports($request): bool
}

// if capture_method=automatic it means the payment intent was created from a checkout session without authorization
return $model->offsetExists('capture_method') && $model->offsetGet('capture_method') === 'automatic';
return $model->offsetExists('capture_method') && $model->offsetGet('capture_method') === PaymentIntent::CAPTURE_METHOD_AUTOMATIC;
}
}
3 changes: 2 additions & 1 deletion src/Action/StripeJs/AuthorizeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Payum\Core\Request\Authorize;
use Payum\Core\Request\Generic;
use Payum\Core\Security\TokenInterface;
use Stripe\PaymentIntent;

/**
* For more information about Stripe Authorize payments :.
Expand All @@ -19,7 +20,7 @@ final class AuthorizeAction extends CaptureAction
{
public function embedNotifyTokenHash(ArrayObject $model, Generic $request): TokenInterface
{
$model->offsetSet('capture_method', 'manual');
$model->offsetSet('capture_method', PaymentIntent::CAPTURE_METHOD_MANUAL);

return parent::embedNotifyTokenHash($model, $request);
}
Expand Down
4 changes: 3 additions & 1 deletion src/StripeCheckoutSessionGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use FluxSE\PayumStripe\Action\StripeCheckoutSession\CancelAction;
use FluxSE\PayumStripe\Action\StripeCheckoutSession\CaptureAction;
use FluxSE\PayumStripe\Action\StripeCheckoutSession\ConvertPaymentAction;
use FluxSE\PayumStripe\Action\StripeCheckoutSession\LegacyCancelAction;
use FluxSE\PayumStripe\Api\KeysAwareInterface;
use FluxSE\PayumStripe\Api\StripeCheckoutSessionApi;
use FluxSE\PayumStripe\Api\StripeCheckoutSessionApiInterface;
Expand Down Expand Up @@ -39,7 +40,8 @@ protected function populateConfig(ArrayObject $config): void
'payum.action.authorize' => new AuthorizeAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),
'payum.action.redirect_to_checkout' => new RedirectToCheckoutAction(),
'payum.action.cancel.payment_intent.automatic' => new CancelAction(),
'payum.action.cancel.payment_intent.automatic' => new LegacyCancelAction(),
'payum.action.cancel.checkout_session' => new CancelAction(),

// Extensions
'payum.extension.after_capture_cancel_payment_intent' => new CancelUrlCancelPaymentIntentExtension(),
Expand Down
28 changes: 9 additions & 19 deletions tests/Action/CancelActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,25 @@ public function testShouldSupportOnlyCancelWithAnArrayAccessModelWithCaptureMeth
{
$action = new CancelAction();

$this->assertTrue($action->supports(new Cancel(['capture_method' => 'manual'])));
$this->assertFalse($action->supports(new Cancel(['capture_method' => 'automatic'])));
$this->assertTrue($action->supports(new Cancel(['object' => PaymentIntent::OBJECT_NAME, 'capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL])));
$this->assertFalse($action->supports(new Cancel(['object' => PaymentIntent::OBJECT_NAME, 'capture_method' => PaymentIntent::CAPTURE_METHOD_AUTOMATIC])));
$this->assertFalse($action->supports(new Cancel([])));
$this->assertFalse($action->supports(new Cancel(null)));
$this->assertFalse($action->supports(new Authorize(['capture_method' => 'manual'])));
$this->assertFalse($action->supports(new Authorize(['capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL])));
}

public function testShouldDoNothingWhenRequiredModelInfoAreNotAvailable(): void
{
$action = new CancelAction();

$model = ['capture_method' => 'manual'];
$model = ['capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL];
$request = new Cancel($model);
$supports = $action->supports($request);
$this->assertTrue($supports);
$action->execute($request);
$this->assertFalse($supports);

$model = [
'object' => PaymentIntent::OBJECT_NAME,
'capture_method' => 'manual',
];
$request = new Cancel($model);
$supports = $action->supports($request);
$this->assertTrue($supports);
$action->execute($request);

$model = [
'object' => SetupIntent::OBJECT_NAME,
'capture_method' => 'manual',
'capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL,
];
$request = new Cancel($model);
$supports = $action->supports($request);
Expand All @@ -78,7 +68,7 @@ public function testShouldDoNothingWhenRequiredModelInfoAreNotAvailable(): void
$model = [
'object' => PaymentIntent::OBJECT_NAME,
'id' => '',
'capture_method' => 'manual',
'capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL,
];
$request = new Cancel($model);
$supports = $action->supports($request);
Expand All @@ -93,7 +83,7 @@ public function testShouldThrowAnExceptionWhenNoTokenIsProvided(): void
$model = [
'object' => PaymentIntent::OBJECT_NAME,
'id' => 'pi_0000',
'capture_method' => 'manual',
'capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL,
];
$request = new Cancel($model);

Expand All @@ -110,7 +100,7 @@ public function testShouldCancelThePaymentIntent(): void
$model = [
'object' => PaymentIntent::OBJECT_NAME,
'id' => 'pi_0000',
'capture_method' => 'manual',
'capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL,
];

$token = new Token();
Expand Down
4 changes: 3 additions & 1 deletion tests/StripeGatewayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ public function testConfigurationForCheckoutSession(): void
$this->assertArrayHasKey('payum.action.redirect_to_checkout', $actualActions);
$this->assertEquals(new StripeCheckoutSession\Api\RedirectToCheckoutAction(), $actualActions['payum.action.redirect_to_checkout']);
$this->assertArrayHasKey('payum.action.cancel.payment_intent.automatic', $actualActions);
$this->assertEquals(new StripeCheckoutSession\CancelAction(), $actualActions['payum.action.cancel.payment_intent.automatic']);
$this->assertEquals(new StripeCheckoutSession\LegacyCancelAction(), $actualActions['payum.action.cancel.payment_intent.automatic']);
$this->assertArrayHasKey('payum.action.cancel.checkout_session', $actualActions);
$this->assertEquals(new StripeCheckoutSession\CancelAction(), $actualActions['payum.action.cancel.checkout_session']);

$api = $config['payum.api'](ArrayObject::ensureArrayObject($config));
$this->assertInstanceOf(StripeCheckoutSessionApi::class, $api);
Expand Down

0 comments on commit 23bbc54

Please sign in to comment.