Skip to content

Commit

Permalink
Merge pull request #45 from FLUX-SE/stripe-js-cancel
Browse files Browse the repository at this point in the history
Stripe js cancel
  • Loading branch information
Prometee authored Jun 25, 2024
2 parents 30e4568 + 248aa5b commit 6417a0b
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 28 deletions.
6 changes: 5 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# UPGRADE FROM `v2.0.15` TO `v2.0.16`

**BC BREAK**: `FluxSE\PayumStripe\Action\CancelAction` has been renamed to `CancelAuthorizedAction`.

# UPGRADE FROM `v2.0.5` TO `v2.0.6`

_[STRIPE_CHECKOUT_SESSION]_
Expand Down Expand Up @@ -62,4 +66,4 @@ you made some JS customisations on this template.

# UPGRADE FROM `v1.1.2` TO `v1.2.0`

**BC BREAK**: The vendor name of this lib has change from `Prometee` to `FluxSE`
**BC BREAK**: The vendor name of this lib has change from `Prometee` to `FluxSE`
4 changes: 2 additions & 2 deletions src/AbstractStripeGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
use FluxSE\PayumStripe\Action\Api\WebhookEvent\SetupIntentCanceledAction;
use FluxSE\PayumStripe\Action\Api\WebhookEvent\SetupIntentSucceededAction;
use FluxSE\PayumStripe\Action\Api\WebhookEvent\StripeWebhookTestAction;
use FluxSE\PayumStripe\Action\CancelAction;
use FluxSE\PayumStripe\Action\CancelAuthorizedAction;
use FluxSE\PayumStripe\Action\CaptureAuthorizedAction;
use FluxSE\PayumStripe\Action\NotifyAction;
use FluxSE\PayumStripe\Action\RefundAction;
Expand All @@ -82,7 +82,7 @@ abstract class AbstractStripeGatewayFactory extends GatewayFactory
protected function getDefaultActions(): array
{
return [
'payum.action.cancel.payment_intent.manual' => new CancelAction(),
'payum.action.cancel.payment_intent.manual' => new CancelAuthorizedAction(),
'payum.action.refund' => new RefundAction(),
'payum.action.capture_authorized' => new CaptureAuthorizedAction(),
'payum.action.notify_unsafe' => new NotifyAction(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Payum\Core\Request\Cancel;
use Stripe\PaymentIntent;

final class CancelAction extends AbstractPaymentIntentAwareAction
final class CancelAuthorizedAction extends AbstractPaymentIntentAwareAction
{
/**
* @param Cancel $request
Expand Down Expand Up @@ -60,7 +60,11 @@ public function supports($request): bool
return false;
}

if (!$model->offsetExists('capture_method')) {
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;
return $model->offsetGet('capture_method') === PaymentIntent::CAPTURE_METHOD_MANUAL;
}
}
12 changes: 11 additions & 1 deletion src/Action/CaptureAuthorizedAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace FluxSE\PayumStripe\Action;

use ArrayAccess;
use ArrayObject;
use FluxSE\PayumStripe\Request\Api\Resource\CapturePaymentIntent;
use FluxSE\PayumStripe\Request\CaptureAuthorized;
Expand Down Expand Up @@ -54,6 +55,15 @@ public function supports($request): bool
return false;
}

return $request->getModel() instanceof ArrayObject;
$model = $request->getModel();
if (!$model instanceof ArrayAccess) {
return false;
}

if (!$model->offsetExists('object')) {
return false;
}

return $model->offsetGet('object') === PaymentIntent::OBJECT_NAME;
}
}
1 change: 0 additions & 1 deletion src/Action/StatusSetupIntentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Request\GetStatusInterface;
use Stripe\PaymentIntent;
use Stripe\SetupIntent;
use Stripe\StripeObject;

Expand Down
87 changes: 87 additions & 0 deletions src/Action/StripeJs/CancelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace FluxSE\PayumStripe\Action\StripeJs;

use ArrayAccess;
use FluxSE\PayumStripe\Request\Api\Resource\CancelPaymentIntent;
use FluxSE\PayumStripe\Request\Api\Resource\CancelSetupIntent;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Cancel;
use Stripe\PaymentIntent;
use Stripe\SetupIntent;

class CancelAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* @param Cancel $request
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

if ($model['object'] === PaymentIntent::OBJECT_NAME) {
$this->cancelPaymentIntent($model);
}

if ($model['object'] === SetupIntent::OBJECT_NAME) {
$this->cancelSetupIntent($model);
}
}

private function cancelPaymentIntent(ArrayObject $model): void
{
if ($model['status'] === PaymentIntent::STATUS_CANCELED) {
return;
}
if ($model['status'] === PaymentIntent::STATUS_SUCCEEDED) {
return;
}

/** @var string $id */
$id = $model['id'];
$this->gateway->execute(new CancelPaymentIntent($id));
}

private function cancelSetupIntent(ArrayObject $model): void
{
if ($model['status'] === SetupIntent::STATUS_CANCELED) {
return;
}

if ($model['status'] === SetupIntent::STATUS_SUCCEEDED) {
return;
}

/** @var string $id */
$id = $model['id'];
$this->gateway->execute(new CancelSetupIntent($id));
}

public function supports($request): bool
{
if (!$request instanceof Cancel) {
return false;
}

$model = $request->getModel();
if (!$model instanceof ArrayAccess) {
return false;
}

if (!$model->offsetExists('object')) {
return false;
}

return in_array($model->offsetGet('object'), [ PaymentIntent::OBJECT_NAME, SetupIntent::OBJECT_NAME ], true);
}
}
3 changes: 2 additions & 1 deletion src/StripeJsGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use FluxSE\PayumStripe\Action\StripeJs\Api\RenderStripeJsAction;
use FluxSE\PayumStripe\Action\StripeJs\AuthorizeAction;
use FluxSE\PayumStripe\Action\StripeJs\CancelAction;
use FluxSE\PayumStripe\Action\StripeJs\CaptureAction;
use FluxSE\PayumStripe\Action\StripeJs\ConvertPaymentAction;
use FluxSE\PayumStripe\Api\KeysAwareInterface;
use FluxSE\PayumStripe\Api\StripeCheckoutSessionApi;
use FluxSE\PayumStripe\Api\StripeJsApi;
use Payum\Core\Bridge\Spl\ArrayObject;
use Stripe\PaymentIntent;
Expand All @@ -29,6 +29,7 @@ protected function populateConfig(ArrayObject $config): void
// Actions
'payum.action.capture' => new CaptureAction(),
'payum.action.authorize' => new AuthorizeAction(),
'payum.action.cancel' => new CancelAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),
'payum.action.render_stripe_js.payment_intent' => function (ArrayObject $config) {
return new RenderStripeJsAction(
Expand Down
1 change: 0 additions & 1 deletion tests/Action/Api/ApiAwareActionTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use FluxSE\PayumStripe\Api\StripeClientAwareInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Stripe\Service\AbstractService;
use Stripe\StripeClient;

trait ApiAwareActionTestTrait
Expand Down
1 change: 0 additions & 1 deletion tests/Action/Api/Resource/CreateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use Stripe\Checkout\Session;
use Stripe\Coupon;
use Stripe\Customer;
use Stripe\Issuing\CardDetails;
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
use Stripe\Plan;
Expand Down
1 change: 0 additions & 1 deletion tests/Action/Api/Resource/RetrieveActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use Stripe\Coupon;
use Stripe\Customer;
use Stripe\Invoice;
use Stripe\Issuing\CardDetails;
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
use Stripe\Plan;
Expand Down
1 change: 0 additions & 1 deletion tests/Action/Api/Resource/UpdateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use PHPUnit\Framework\TestCase;
use Stripe\ApiResource;
use Stripe\Coupon;
use Stripe\Issuing\CardDetails;
use Stripe\PaymentIntent;
use Stripe\Plan;
use Stripe\Price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Tests\FluxSE\PayumStripe\Action;

use ArrayObject;
use FluxSE\PayumStripe\Action\CancelAction;
use FluxSE\PayumStripe\Action\CancelAuthorizedAction;
use FluxSE\PayumStripe\Request\Api\Resource\CancelPaymentIntent;
use FluxSE\PayumStripe\Request\Api\Resource\UpdatePaymentIntent;
use LogicException;
Expand All @@ -20,15 +20,14 @@
use Payum\Core\Storage\IdentityInterface;
use PHPUnit\Framework\TestCase;
use Stripe\PaymentIntent;
use Stripe\SetupIntent;

final class CancelActionTest extends TestCase
final class CancelAuthorizedActionTest extends TestCase
{
use GatewayAwareTestTrait;

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

$this->assertInstanceOf(GatewayAwareInterface::class, $action);
$this->assertInstanceOf(ActionInterface::class, $action);
Expand All @@ -38,7 +37,7 @@ public function testShouldImplements(): void

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

$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])));
Expand All @@ -49,7 +48,7 @@ public function testShouldSupportOnlyCancelWithAnArrayAccessModelWithCaptureMeth

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

$model = ['capture_method' => PaymentIntent::CAPTURE_METHOD_MANUAL];
$request = new Cancel($model);
Expand Down Expand Up @@ -78,7 +77,7 @@ public function testShouldDoNothingWhenRequiredModelInfoAreNotAvailable(): void

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

$model = [
'object' => PaymentIntent::OBJECT_NAME,
Expand Down Expand Up @@ -152,7 +151,7 @@ public function testShouldCancelThePaymentIntent(): void
->willReturn($notifyToken)
;

$action = new CancelAction();
$action = new CancelAuthorizedAction();

$action->setGateway($gatewayMock);
$action->setGenericTokenFactory($genericGatewayFactory);
Expand Down
10 changes: 8 additions & 2 deletions tests/Action/CaptureAuthorizedActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function testShouldSupportOnlyCaptureAuthorizedWithAnArrayAccessModel():
{
$action = new CaptureAuthorizedAction();

$this->assertTrue($action->supports(new CaptureAuthorized([])));
$model = [
'object' => PaymentIntent::OBJECT_NAME,
];
$this->assertTrue($action->supports(new CaptureAuthorized($model)));
$this->assertFalse($action->supports(new CaptureAuthorized([])));
$this->assertFalse($action->supports(new CaptureAuthorized(null)));
$this->assertFalse($action->supports(new Capture(null)));
$this->assertFalse($action->supports(new Authorize(null)));
Expand All @@ -50,7 +54,9 @@ public function testShouldDoNothingWhenRequiredModelInfoAreNotAvailable(): void
{
$action = new CaptureAuthorizedAction();

$model = [];
$model = [
'object' => PaymentIntent::OBJECT_NAME,
];
$request = new CaptureAuthorized($model);
$supports = $action->supports($request);
$this->assertTrue($supports);
Expand Down
3 changes: 0 additions & 3 deletions tests/Action/StripeCheckoutSession/CancelActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
namespace Tests\FluxSE\PayumStripe\Action\StripeCheckoutSession;

use FluxSE\PayumStripe\Action\StripeCheckoutSession\CancelAction;
use FluxSE\PayumStripe\Request\Api\Resource\AllSession;
use FluxSE\PayumStripe\Request\Api\Resource\ExpireSession;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\Authorize;
use Payum\Core\Request\Cancel;
use PHPUnit\Framework\TestCase;
use Stripe\Checkout\Session;
use Stripe\Collection;
use Tests\FluxSE\PayumStripe\Action\GatewayAwareTestTrait;

final class CancelActionTest extends TestCase
Expand Down
Loading

0 comments on commit 6417a0b

Please sign in to comment.