Repository navigation
Stripe Checkout: pay recurring orders with a Stripe subscription - #1083
Merged
Merged
Conversation
added 4 commits
November 22, 2026 07:32
…re reports on another one A provider can now record the gateway's reference of a recurring payment as soon as it learns it (RecordRecurringPaymentReferenceCommand), under the recurring payment's lock. A cancel made before the first paid cycle then stops the gateway at once; a payment cancelled before the reference was known is stopped when the reference arrives. The cycle, failure and ended reports are checked against the recorded reference: a report on another subscription (for example a second one started for the same order) records nothing and leaves a note on the initial order. RecordRecurringPaymentEndedCommand gains ProviderReference.
A recurring order opens a Checkout session in subscription mode: one recurring price of the order total, every CycleLength days/weeks/months/years, with the recurring payment and the order named in the session and subscription metadata. The number of cycles stays with the shop, which cancels the subscription when the last one is paid. A recurring payment cancelled before the order was paid leaves a one-off payment. Webhooks: - checkout.session.completed records the subscription id at once (RecordRecurringPaymentReferenceCommand), so a cancel before the first invoice stops it; - invoice.paid records the cycle; the first invoice (subscription_create) also marks the initial order paid, with the invoice's payment intent kept for refunds; - invoice.payment_failed records the failure, customer.subscription.deleted the end; - other invoices of the subscription (updates, one-off dashboard invoices) are not cycles. A failing recurring event throws, so the webhook answers with an error and Stripe resends it. CancelRecurringPayment cancels the subscription with the recurring payment's store key; one Stripe ended already counts as cancelled. Paying again is refused once the subscription started. Every Stripe call shares one client with a 5 s timeout and no retry (Stripe.net waits 80 s).
…other subscription The recurring payment service now returns the outcome of every gateway report (Recorded, NotFound, Duplicate, OtherReference, Ended) with the payment it read under the lock, so the provider commands no longer read it again to guess why nothing was recorded. - The first reported reference is recorded whichever report brings it: a failure or an end reported before the reference was known now records it, as a paid cycle already did. - The reference command stops the gateway of an ended payment only when it records the reference, so a resent checkout event no longer asks the gateway to cancel again. - A failure on an ended payment records the missing reference under the lock before the gateway is asked to stop, instead of patching it in memory. - A paid cycle, a failure or a reference reported on another subscription asks the provider to stop that subscription and leaves one note per report on the initial order; another subscription that ended is only logged. - RecordRecurringPaymentFailureCommand returns true when the failure was recorded, also while the payment is already past due.
…ture/recurring-payments-stripe
Base automatically changed from
feature/recurring-payments-provider-reference
to
develop
November 22, 2026 07:44
| { | ||
| if (paymentIntent.Metadata.TryGetValue("order_guid", out var order_guid) | ||
| //a subscription's payments carry no order: its invoices record them | ||
| if (paymentIntent.Metadata?.ContainsKey(OrderGuidKey) != true) |
| //one client for every store: it keeps the connections, and a call to Stripe never holds a webhook, a checkout or | ||
| //a cancel for long; no retry, so a slow Stripe costs one timeout and the webhook is resent or the action repeated | ||
| private static readonly IHttpClient SharedHttpClient = new SystemNetHttpClient( | ||
| new HttpClient { Timeout = StripeCheckoutDefaults.RequestTimeout }, 0); |
|
|
||
| _mediator.Verify(x => x.Send(It.Is<RecordRecurringPaymentCycleCommand>(c => | ||
| c.RecurringPaymentId == "rp1" && c.ProviderReference == "sub_1" && c.CycleReference == "in_2" && | ||
| c.Amount == 19.99 && c.PaidOnUtc == new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc) && |
Comment on lines
+165
to
+170
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Stripe subscription {SubscriptionId} could not be cancelled", | ||
| recurringPayment.ProviderReference); | ||
| return [$"Stripe could not be reached ({e.Message}). Try again, or cancel the subscription in the Stripe dashboard."]; | ||
| } |
Comment on lines
+179
to
+183
| catch (Exception e) | ||
| { | ||
| _logger.LogError(e, "Stripe subscription {SubscriptionId} could not be read", subscriptionId); | ||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The Stripe stage of recurring payments: Stripe Checkout becomes the first payment method that supports recurring payments, so recurring products can be bought. Stacked on #1082: merge that one first, and this PR will retarget to
develop.Checkout
subscriptionmode: one recurring price of the order total (each cycle order is a copy of the initial order), everyCycleLengthday/week/month/year. The session and the subscription carryrecurring_payment_idandorder_guidas metadata.Webhooks
checkout.session.completed(subscription)RecordRecurringPaymentReferenceCommand(#1082): the subscription id is known at once, so a cancel before the first invoice stops itinvoice.paidRecordRecurringPaymentCycleCommand; the first invoice (subscription_create) also marks the initial order paid, keeping the invoice's payment intent so the order can be refunded onlineinvoice.payment_failedRecordRecurringPaymentFailureCommandcustomer.subscription.deletedRecordRecurringPaymentEndedCommandwith the subscription idsubscription_createandsubscription_cycleinvoices are cycles. Updates and one-off invoices made in the Stripe dashboard are ignored.payment_intent.succeededof a subscription invoice carries no order and is now skipped quietly (it used to log an error).Cancel and timeouts
CancelRecurringPaymentcancels the subscription at once, without a refund, with the recurring payment's store key. If Stripe refuses a subscription it already cancelled, that counts as success.Calls made (please check)
× 100(two-decimal currencies), as the one-off path does.Not verified
Tests
Grand.Modules.Tests: 461 passed (31 new or changed Stripe tests).