From 2b4d127e3d475cffa22f46c7a3e8bbc54cac87b3 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Thu, 11 Jul 2024 11:27:26 +0100 Subject: [PATCH] Stripe - Add method to cancel payment intents (#1860) --- packages/stripe/README.md | 22 +++++++++++++++++++ .../stripe/src/Enums/CancellationReason.php | 11 ++++++++++ packages/stripe/src/Facades/Stripe.php | 2 ++ .../stripe/src/Managers/StripeManager.php | 19 ++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 packages/stripe/src/Enums/CancellationReason.php diff --git a/packages/stripe/README.md b/packages/stripe/README.md index 85f67d68a8..12b3e0b1d9 100644 --- a/packages/stripe/README.md +++ b/packages/stripe/README.md @@ -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. diff --git a/packages/stripe/src/Enums/CancellationReason.php b/packages/stripe/src/Enums/CancellationReason.php new file mode 100644 index 0000000000..9f0289c4fb --- /dev/null +++ b/packages/stripe/src/Enums/CancellationReason.php @@ -0,0 +1,11 @@ +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. */