Skip to content

Commit

Permalink
Handle the async failed status
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Dec 15, 2023
1 parent 64cbfba commit 3a84831
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Action/Api/StripeApiAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @property StripeClientAwareInterface $api
* @property string $apiClass
*/
trait StripeApiAwareTrait
{
Expand Down
24 changes: 22 additions & 2 deletions src/Action/StatusPaymentIntentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Request\GetStatusInterface;
use Stripe\PaymentIntent;
use Stripe\StripeObject;

class StatusPaymentIntentAction extends AbstractStatusAction
{
Expand Down Expand Up @@ -36,7 +37,7 @@ public function isMarkedStatus(GetStatusInterface $request, ArrayObject $model):
return true;
}

if ($this->isCanceledStatus($status)) {
if ($this->isCanceledStatus($status) || $this->isSpecialCanceledStatus($model)) {
$request->markCanceled();

return true;
Expand All @@ -52,7 +53,7 @@ public function isMarkedStatus(GetStatusInterface $request, ArrayObject $model):
}

/**
* @see https://stripe.com/docs/payments/intents#payment-intent
* @see https://stripe.com/docs/payments/paymentintents/lifecycle
*/
protected function isCanceledStatus(string $status): bool
{
Expand All @@ -72,4 +73,23 @@ public function getSupportedObjectName(): string
{
return PaymentIntent::OBJECT_NAME;
}

/**
* @see https://stripe.com/docs/payments/paymentintents/lifecycle
*/
protected function isSpecialCanceledStatus(ArrayObject $model): bool
{
/** @var string|null $status */
$status = $model->offsetGet('status');
/** @var null|StripeObject $lastPaymentError */
$lastPaymentError = $model->offsetGet('last_payment_error');

if (PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD === $status) {
if (null !== $lastPaymentError) {
return true;
}
}

return false;
}
}
27 changes: 25 additions & 2 deletions src/Action/StatusSetupIntentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

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

class StatusSetupIntentAction extends AbstractStatusAction
{
Expand All @@ -30,7 +32,7 @@ public function isMarkedStatus(GetStatusInterface $request, ArrayObject $model):
return true;
}

if ($this->isCanceledStatus($status)) {
if ($this->isCanceledStatus($status) || $this->isSpecialCanceledStatus($model)) {
$request->markCanceled();

return true;
Expand All @@ -46,7 +48,7 @@ public function isMarkedStatus(GetStatusInterface $request, ArrayObject $model):
}

/**
* @see https://stripe.com/docs/payments/intents#payment-intent
* @see https://stripe.com/docs/payments/setupintents/lifecycle
*/
protected function isCanceledStatus(string $status): bool
{
Expand All @@ -62,6 +64,27 @@ protected function isNewStatus(string $status): bool
], true);
}



/**
* @see https://stripe.com/docs/payments/setupintents/lifecycle
*/
protected function isSpecialCanceledStatus(ArrayObject $model): bool
{
/** @var string|null $status */
$status = $model->offsetGet('status');
/** @var null|StripeObject $lastPaymentError */
$lastPaymentError = $model->offsetGet('last_setup_error');

if (SetupIntent::STATUS_REQUIRES_PAYMENT_METHOD === $status) {
if (null !== $lastPaymentError) {
return true;
}
}

return false;
}

public function getSupportedObjectName(): string
{
return SetupIntent::OBJECT_NAME;
Expand Down
23 changes: 22 additions & 1 deletion tests/Action/StatusPaymentIntentActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Payum\Core\Request\Sync;
use PHPUnit\Framework\TestCase;
use Stripe\PaymentIntent;
use Stripe\StripeObject;

final class StatusPaymentIntentActionTest extends TestCase
{
Expand Down Expand Up @@ -161,7 +162,27 @@ public function testShouldMarkCanceledIfIsAPaymentIntentObjectAndStatusIsCancele
$this->assertTrue($request->isCanceled());
}

public function testShouldMarkAsCanceledIfIsAPaymentIntentObjectAndStatusRequiresPaymentMethod(): void
public function testShouldMarkAsCanceledIfIsAPaymentIntentObjectAndStatusRequiresPaymentMethodWithError(): void
{
$action = $this->createStatusWithGateway();

$model = [
'object' => PaymentIntent::OBJECT_NAME,
'status' => PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD,
'last_payment_error' => new StripeObject(),
];

$request = new GetHumanStatus($model);

$supports = $action->supports($request);
$this->assertTrue($supports);

$action->execute($request);

$this->assertTrue($request->isCanceled());
}

public function testShouldMarkAsNewIfIsAPaymentIntentObjectAndStatusRequiresPaymentMethod(): void
{
$action = $this->createStatusWithGateway();

Expand Down
21 changes: 21 additions & 0 deletions tests/Action/StatusSetupIntentActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Payum\Core\Request\Sync;
use PHPUnit\Framework\TestCase;
use Stripe\SetupIntent;
use Stripe\StripeObject;

final class StatusSetupIntentActionTest extends TestCase
{
Expand Down Expand Up @@ -161,6 +162,26 @@ public function testShouldMarkAsCanceledIfIsASetupIntentObjectAndStatusRequiresP
$this->assertTrue($request->isNew());
}

public function testShouldMarkAsNewIfIsASetupIntentObjectAndStatusRequiresPaymentMethodWithError(): void
{
$action = $this->createStatusWithGateway();

$model = [
'object' => SetupIntent::OBJECT_NAME,
'status' => SetupIntent::STATUS_REQUIRES_PAYMENT_METHOD,
'last_setup_error' => new StripeObject(),
];

$request = new GetHumanStatus($model);

$supports = $action->supports($request);
$this->assertTrue($supports);

$action->execute($request);

$this->assertTrue($request->isCanceled());
}

public function testShouldMarkAsNewIfIsASetupIntentObjectAndStatusRequiresConfirmation(): void
{
$action = $this->createStatusWithGateway();
Expand Down

0 comments on commit 3a84831

Please sign in to comment.