Repository navigation
Recurring payments: cancelling and messages (slice 3 of 5) - #1077
Merged
Merged
Conversation
added 11 commits
November 21, 2026 05:03
…ed or a cycle fails
…g it The lock's lease can end while the gateway is asked to stop, and a webhook may record a cycle or end the payment meanwhile. Cancel now applies to a fresh copy and writes nothing when it is already cancelled or completed.
…cancelled-by line for any caller The store owner message now reads "Cancelled by: ...", which fits a staff name, a customer e-mail and the order hook's "Order #12 cancelled" alike. The plugin guide notes the lock's 30 s lease and the re-read after the gateway call.
Comment on lines
+32
to
+40
| catch (Exception e) | ||
| { | ||
| //a throw is treated as a refusal: the gateway may still be charging, so the status must stay as it is | ||
| logger.LogError(e, "Recurring payment {RecurringPaymentId}: the payment gateway could not be asked to stop charging", | ||
| recurringPayment.Id); | ||
| var result = new CancelRecurringPaymentResult(); | ||
| result.AddError(e.Message); | ||
| return result; | ||
| } |
Comment on lines
+57
to
+64
| catch (Exception ex) | ||
| { | ||
| // the order is already cancelled or deleted: publishing must complete whatever happens here | ||
| _logger.LogError(ex, | ||
| "Order {OrderNumber} ({OrderId}) was {What} but its recurring payment could not be cancelled", | ||
| order.OrderNumber, order.Id, what); | ||
| await AddNote(order, what, ex.Message); | ||
| } |
Comment on lines
+79
to
+83
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "The note about the recurring payment of order {OrderId} could not be saved", | ||
| order.Id); | ||
| } |
Comment on lines
+75
to
+80
| catch (Exception ex) | ||
| { | ||
| // the status is already saved: publishing must complete whatever happens here | ||
| _logger.LogError(ex, "The messages about recurring payment {RecurringPaymentId} could not be sent", | ||
| recurringPayment.Id); | ||
| } |
Comment on lines
+89
to
+93
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "A message about recurring payment {RecurringPaymentId} could not be sent", | ||
| recurringPayment.Id); | ||
| } |
Comment on lines
+46
to
+50
| catch (Exception ex) | ||
| { | ||
| logService.LogError(ex, "UpgradeProcess - RecurringPaymentTemplates (2.5)"); | ||
| return false; | ||
| } |
… it on late reports The cancel held the lock across the gateway call, so a slow call stalled every webhook for the payment and, past the lease, let a cycle claim and the cancel overwrite each other. The call now runs between two short locked sections, and the cancel is written on a fresh read. A failure reported for a payment the shop already ended now asks the gateway to stop, using the reference the failure command can now carry, and a cycle charged after the payment ended is noted on the initial order for a refund.
Comment on lines
+22
to
+28
| catch (Exception e) | ||
| { | ||
| // a throw is treated as a refusal: the report is recorded and a resend is a no-op, so the note is the only trace | ||
| logger.LogError(e, "Recurring payment {RecurringPaymentId}: the payment gateway could not be asked to stop charging", | ||
| recurringPayment.Id); | ||
| errors = [e.Message]; | ||
| } |
Comment on lines
+46
to
+51
| catch (Exception e) | ||
| { | ||
| // the note is a courtesy to staff; losing it must not hide the original failure or fail a recorded report | ||
| logger.LogError(e, "Recurring payment {RecurringPaymentId}: the order note could not be written: {Note}", | ||
| recurringPayment.Id, note); | ||
| } |
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.
Type: feature
Issue
After slices 1 and 2 (#1075, #1076) a recurring payment can be recorded and the gateway can report paid cycles, failed cycles and a schedule ended at its end. The shop still cannot cancel a recurring payment, and nobody is told when a cycle fails or a recurring payment ends. Cancelling or deleting the order the customer placed leaves its recurring payment running. The gateway can then keep charging for an order the shop has cancelled.
This is slice 3 of 5 of recurring payments (payments roadmap P5): cancelling and the messages. The cancel is not reachable from a screen yet. The My account tab is slice 4; the Admin and Store screens are slice 5.
Solution
Cancel (
CancelRecurringPaymentCommand,RecurringPaymentService.Cancel)RecurringPayment:{id}) and reads the record again inside it.CancelledorCompleted→ success, and nothing changes. A double click, or a cancel followed by a delete, does one cancel.Pendingwith no gateway reference yet →Cancelledwithout calling the gateway. If the gateway did start a subscription at checkout, its first reported cycle is recorded on the cancelled payment, and slice 2 then asks the gateway to stop. A test pins this path.CancelRecurringPaymentis called first. A refusal or an exception returns the errors and keeps the status, so the shop never showsCancelledwhile the gateway may still charge.RecurringPaymentServicemust not depend onIPaymentService: a provider will injectIRecurringPaymentServicein the Stripe stage, and the reverse dependency would be a DI cycle.Pending, before the gateway reference was known) asks the gateway to stop.RecordRecurringPaymentFailureCommandgained an optionalProviderReferencefor this. A cycle charged after the payment was cancelled or completed adds an order note on the initial order so staff can refund it.UpdateRecurringPaymentis removed. Nothing called it, and it was a whole-document write without the lock (a follow-up from slice 2).Initial order cancelled or deleted
OrderCancelledEvent/OrderDeletedEventcancels the recurring payment of an initial order (RecurringCycleNumber == 1). This is the same pattern asOrderCancelledOrderApprovalHandler. The two order command handlers are unchanged.CancelledByisOrder #N cancelledorOrder #N deleted.Messages
RecurringPaymentServicepublishesRecurringPaymentCancelledEventonly when the status changes toCancelled(byCancelor byRecordEnded). It publishesRecurringPaymentPastDueEventonly when the status changes toPastDue. A gateway retrying the same failure therefore sends nothing new.RecurringPaymentMessagesHandlersends the customer and the store owner their e-mails:RecurringPayment.Cancelled.CustomerNotificationRecurringPayment.Cancelled.StoreOwnerNotificationRecurringPayment.PaymentFailed.CustomerNotificationRecurringPayment.PaymentFailed.StoreOwnerNotification{{Order.*}}). The newLiquidRecurringPaymentdrop, in the existingLiquidObject.RecurringPaymentslot, holdsCycleLength,CyclePeriod,CyclesPaid,TotalCycles,LastErrorandCancelledBy.LastErrorandCancelledBycome from a gateway or a person, so they are HTML-encoded.IRecurringPaymentMessageProviderService.Pendingrecurring payment that never charged.Persisted data
MigrationRecurringPaymentTemplates(2.5) adds them to existing ones. It adds only the templates that are missing, so an edited one is left alone, and a test keeps the seed and the migration equal.DefaultLanguage.xmlanden_250.xml:Enums.Grand.Domain.Catalog.RecurringCyclePeriod.*. The existing...RecurringProductCyclePeriod.*keys name an enum that does not exist.Docs:
.ai/skills/plugin-payment.mddescribes whatCancelRecurringPaymentmust do and its time budget..ai/skills/message-notification.mdlists the new interface and drop.Breaking changes
IRecurringPaymentService:UpdateRecurringPaymentis removed andCancelis added. This only affects custom implementations of the service.IPaymentProvider.CancelRecurringPaymentalready has a default.Testing
Automated:
dotnet build GrandNode.slnx→ 0 errors.Grand.Business.Checkout.Tests1072/1072,Grand.Business.Messages.Tests168/168,Grand.Modules.Tests427/427,Grand.Web.Tests624/624.The new tests cover:
E2E on a throwaway installation (Mongo, sample data). No shipped provider charges in cycles yet, so a temporary controller, not part of this PR, inserted an initial order with its recurring payment and sent the commands:
Pendingrecurring payment with no reference →Cancelled. Two e-mails are queued, one to the customer and one to the store, with no{{left in the body. A second cancel succeeds and queues nothing.Activeone with a reference, paid by Cash on delivery (which cannot cancel) → error "Recurring payment method not supported". The status staysActiveand nothing is queued.PastDue, with e-mails on the first report only. A<script>reason arrives in the e-mail encoded.CancelledbyPayments.CashOnDelivery, two e-mails. A second ended event does nothing.CancelledwithOrder #12 cancelled. The customer gets the standard "order cancelled" e-mail plus the two recurring payment e-mails.Active, and the order has the note.CancelledwithOrder #N deleted.GrandNodeVersionback to 2.4 and removed this migration's record.E2E after the review fixes (new throwaway install, same temporary controller):
10. Active payment, a gateway that takes 5 s to confirm the cancel. A cycle webhook sent 1 s in is recorded at once (cycle 2, still
Active). The cancel then finishes at about 5.2 s asCancelledand keeps both cycles. Two e-mails.11.
Pendingpayment cancelled, then the gateway reports cycle 1 → recorded on the cancelled payment. The initial order gets the note "Cycle 1 was charged at the payment gateway (in_1) after the recurring payment was cancelled: check whether the customer must be refunded", and the gateway is asked to stop (Cash on delivery refuses, so a second note says so).12.
Pendingpayment cancelled, then the gateway reports a failed charge with a reference → status unchanged, no e-mails, gateway asked to stop (refusal note).13. A failure on an
Activepayment, reported twice →PastDueonce and no gateway call. A cancel refused by the gateway → stillActive, no e-mails. Admin → Cancel order on aPendinginitial order →CancelledwithOrder #7 cancelled, plus three e-mails.Follow-ups (not in this PR):