Skip to content

Commit

Permalink
Stripe - Add method to cancel payment intents (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Jul 11, 2024
1 parent 13e961f commit 2b4d127
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/stripe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ Stripe::updateIntent(\Lunar\Models\Cart $cart, [
]);
```

### Cancel an existing intent

If you need to cancel a PaymentIntent, you can do so. You will need to provide a valid reason, those of which can be found in the Stripe docs: https://docs.stripe.com/api/payment_intents/cancel.

Lunar Stripe includes a PHP Enum to make this easier for you:

```php
use Lunar\Stripe\Enums\CancellationReason;

CancellationReason::ABANDONED;
CancellationReason::DUPLICATE;
CancellationReason::REQUESTED_BY_CUSTOMER;
CancellationReason::FRAUDULENT;
```

```php
use Lunar\Stripe\Facades\Stripe;
use Lunar\Stripe\Enums\CancellationReason;

Stripe::cancelIntent(\Lunar\Models\Cart $cart, CancellationReason $reason);
```

### Update the address on Stripe

So you don't have to manually specify all the shipping address fields you can use the helper function to do it for you.
Expand Down
11 changes: 11 additions & 0 deletions packages/stripe/src/Enums/CancellationReason.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Lunar\Stripe\Enums;

enum CancellationReason: string
{
case DUPLICATE = 'duplicate';
case FRAUDULENT = 'fraudulent';
case REQUESTED_BY_CUSTOMER = 'requested_by_customer';
case ABANDONED = 'abandoned';
}
2 changes: 2 additions & 0 deletions packages/stripe/src/Facades/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Stripe\Facades;

use Illuminate\Support\Facades\Facade;
use Lunar\Stripe\Enums\CancellationReason;
use Lunar\Stripe\MockClient;
use Stripe\ApiRequestor;

Expand All @@ -11,6 +12,7 @@
* @method static createIntent(\Lunar\Models\Cart $cart, array $opts): \Stripe\PaymentIntent
* @method static syncIntent(\Lunar\Models\Cart $cart): void
* @method static updateIntent(\Lunar\Models\Cart $cart, array $values): void
* @method static cancelIntent(\Lunar\Models\Cart $cart, CancellationReason $reason): void
* @method static updateShippingAddress(\Lunar\Models\Cart $cart): void
* @method static getCharges(string $paymentIntentId): \Illuminate\Support\Collection
* @method static getCharge(string $chargeId): \Stripe\Charge
Expand Down
19 changes: 19 additions & 0 deletions packages/stripe/src/Managers/StripeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Lunar\Models\Cart;
use Lunar\Models\CartAddress;
use Lunar\Stripe\Enums\CancellationReason;
use Stripe\Charge;
use Stripe\Exception\InvalidRequestException;
use Stripe\PaymentIntent;
Expand Down Expand Up @@ -119,6 +120,24 @@ public function syncIntent(Cart $cart): void
);
}

public function cancelIntent(Cart $cart, CancellationReason $reason): void
{
$meta = (array) $cart->meta;

if (empty($meta['payment_intent'])) {
return;
}

try {
$this->getClient()->paymentIntents->cancel(
$meta['payment_intent'],
['cancellation_reason' => $reason->value]
);
} catch (\Exception $e) {

}
}

/**
* Fetch an intent from the Stripe API.
*/
Expand Down

0 comments on commit 2b4d127

Please sign in to comment.