Skip to content

Stripe Checkout: pay recurring orders with a Stripe subscription - #1083

Merged
KrzysztofPajak merged 5 commits into
developfrom
feature/recurring-payments-stripe
Nov 22, 2026
Merged

KrzysztofPajak merged 5 commits into
developfrom
feature/recurring-payments-stripe

Conversation

@KrzysztofPajak

Copy link
Copy Markdown
Member

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

  • A recurring order opens the Checkout session in subscription mode: one recurring price of the order total (each cycle order is a copy of the initial order), every CycleLength day/week/month/year. The session and the subscription carry recurring_payment_id and order_guid as metadata.
  • The number of cycles stays with the shop: the core cancels the subscription when the last cycle is paid.
  • A recurring payment that ended before the order was paid leaves a one-off payment of the order.
  • "Complete payment" is refused once the subscription started, so a customer waiting for the first invoice webhook cannot start a second subscription.

Webhooks

Event Action
checkout.session.completed (subscription) RecordRecurringPaymentReferenceCommand (#1082): the subscription id is known at once, so a cancel before the first invoice stops it
invoice.paid RecordRecurringPaymentCycleCommand; the first invoice (subscription_create) also marks the initial order paid, keeping the invoice's payment intent so the order can be refunded online
invoice.payment_failed RecordRecurringPaymentFailureCommand
customer.subscription.deleted RecordRecurringPaymentEndedCommand with the subscription id
  • Only subscription_create and subscription_cycle invoices are cycles. Updates and one-off invoices made in the Stripe dashboard are ignored.
  • A recurring event that fails throws, so the webhook answers 400 and Stripe resends it. The commands record each report once, and the initial order is marked paid only while it can be.
  • payment_intent.succeeded of a subscription invoice carries no order and is now skipped quietly (it used to log an error).
  • The configuration screens (Admin and Store) list the events to enable on the Stripe webhook endpoint.

Cancel and timeouts

  • CancelRecurringPayment cancels 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.
  • Every Stripe call shares one HTTP client with a 5 s timeout and no retry. Stripe.net's default is 80 s plus retries.

Calls made (please check)

  • Cancel is immediate, not at period end: the shop shows "Cancelled" the moment it asks, so Stripe must not charge again.
  • Cycle orders keep no Stripe payment id, so a cycle order is refunded from the Stripe dashboard (the refund screen already says so). The initial order refunds online.
  • A cycle longer than Stripe allows (3 years) fails when the session is created; the method is not hidden for such carts.
  • Amounts keep the plugin's existing × 100 (two-decimal currencies), as the one-off path does.
  • Failure reason shown to staff: "Stripe could not charge invoice INV-2 (attempt 2)".

Not verified

  • Not run against live Stripe (no test account here). The webhook tests build real signed events and check the Stripe API calls (method, path, form body, API key) on a fake HTTP handler. Before release, run one end-to-end in Stripe test mode, ideally with a test clock to advance a cycle.

Tests

Grand.Modules.Tests: 461 passed (31 new or changed Stripe tests).

support 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.
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;
}
@KrzysztofPajak
KrzysztofPajak merged commit 7385d00 into develop Nov 22, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the feature/recurring-payments-stripe branch November 22, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant