@antelopejs/payment-subscriptions 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -67
- package/dist/config.d.ts +3 -1
- package/dist/config.js.map +1 -1
- package/dist/engine.js +6 -1
- package/dist/engine.js.map +1 -1
- package/dist/implementation.js +12 -11
- package/dist/implementation.js.map +1 -1
- package/dist/schedule.js +5 -5
- package/dist/schedule.js.map +1 -1
- package/dist/triggers.js +27 -11
- package/dist/triggers.js.map +1 -1
- package/dist/view.js +12 -9
- package/dist/view.js.map +1 -1
- package/docs/1.introduction.md +50 -37
- package/docs/2.renewal-clock.md +89 -73
- package/docs/3.failed-renewals.md +123 -93
- package/docs/4.provider-schedule.md +29 -20
- package/docs/tsconfig.json +10 -0
- package/package.json +19 -8
package/docs/1.introduction.md
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
title: Introduction
|
|
3
|
+
description: What the generic subscriptions module implements, which interfaces it needs, how it charges, and what it stores.
|
|
4
|
+
mode: reference
|
|
5
|
+
---
|
|
2
6
|
|
|
3
7
|
`@antelopejs/payment-subscriptions` implements
|
|
4
8
|
`@antelopejs/interface-subscriptions` for any provider wired behind
|
|
5
9
|
`@antelopejs/interface-payment`. It is the implementation for providers that
|
|
6
|
-
leave each renewal charge to the merchant
|
|
7
|
-
|
|
8
|
-
implementation by default
|
|
10
|
+
leave each renewal charge to the merchant, such as Adyen, Checkout.com, and
|
|
11
|
+
Mangopay. It is a fallback that you choose explicitly. Stripe users should use
|
|
12
|
+
the native subscription implementation of Stripe by default, so that two
|
|
13
|
+
renewal engines never compete.
|
|
9
14
|
|
|
10
15
|
## What it needs
|
|
11
16
|
|
|
@@ -13,49 +18,57 @@ implementation by default rather than run two competing renewal engines.
|
|
|
13
18
|
| --------------------------------------- | ------------------------------------------------------------- | -------- |
|
|
14
19
|
| `@antelopejs/interface-payment` | Every charge is an off-session payment against a saved method | Yes |
|
|
15
20
|
| `@antelopejs/interface-database` | Every subscription is a record there, and nowhere else | Yes |
|
|
16
|
-
| `@antelopejs/interface-redis-scheduler` | Fires each renewal
|
|
21
|
+
| `@antelopejs/interface-redis-scheduler` | Fires each renewal when it is due instead of at the next poll | No |
|
|
22
|
+
| `@antelopejs/interface-redis` | The module uses the scheduler only when Redis is also wired | No |
|
|
17
23
|
|
|
18
|
-
The database adapter must implement `atomicMutation`, the
|
|
19
|
-
|
|
20
|
-
runs against it.
|
|
24
|
+
The database adapter must implement `atomicMutation`, the conditional
|
|
25
|
+
single-record write of `@antelopejs/interface-database`. `@antelopejs/mongodb`
|
|
26
|
+
implements it, and the suite runs against it.
|
|
21
27
|
|
|
22
28
|
## What it charges
|
|
23
29
|
|
|
24
|
-
Each period is one `CreatePayment` with `offSession: true` against the
|
|
25
|
-
|
|
26
|
-
subscription
|
|
27
|
-
idempotency key is
|
|
28
|
-
`payment-subscriptions:<subscription>:<period>:<attempt
|
|
29
|
-
anything that might repeat a charge, and
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
Each period is one `CreatePayment` call with `offSession: true` against the
|
|
31
|
+
saved method of the subscription. The call carries the `reference` of the
|
|
32
|
+
subscription and its metadata, plus `antelopeSubscription` set to the
|
|
33
|
+
subscription ID. The idempotency key is
|
|
34
|
+
`payment-subscriptions:<subscription>:<period>:<attempt>`. The key stays the
|
|
35
|
+
same for anything that might repeat a charge, and changes for each deliberate
|
|
36
|
+
retry.
|
|
37
|
+
|
|
38
|
+
The optional `provider` that you pass when you create a subscription selects
|
|
39
|
+
the payment account, as on the payment interface. The module stores it with
|
|
40
|
+
the subscription, and every later charge goes to the same account, because the
|
|
41
|
+
customer and the method exist only there. To read the subscription back, pass
|
|
35
42
|
the same `provider`.
|
|
36
43
|
|
|
37
44
|
## What it records
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
`subscriptions`, default instance
|
|
46
|
+
The module keeps one record per subscription, in the schema
|
|
47
|
+
`payment-subscriptions`, table `subscriptions`, on the default instance. The
|
|
48
|
+
record holds:
|
|
41
49
|
|
|
42
|
-
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
50
|
+
- The request: customer, method, amount, interval, reference, description, and
|
|
51
|
+
metadata.
|
|
52
|
+
- The billing state: anchor, current period, status, attempt, the latest
|
|
53
|
+
charge, and any charge still settling.
|
|
54
|
+
- `nextActionAt`, when the subscription next needs attention, and `dueAt`, the
|
|
55
|
+
field that the sweep queries. `dueAt` equals `nextActionAt`, or now while
|
|
56
|
+
events wait for delivery.
|
|
57
|
+
- The claim: which instance holds it, and until when.
|
|
58
|
+
- The persisted start and replay deadline of the current attempt, and any
|
|
59
|
+
reconciliation hold.
|
|
60
|
+
- The outbox, which holds the events raised but not delivered yet.
|
|
61
|
+
- The idempotency keys of the last 50 cancellations and payment method changes.
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
the `provider
|
|
54
|
-
|
|
63
|
+
The subscription ID is `sub_` followed by a hash of the idempotency key of the
|
|
64
|
+
creation and the `provider`. Two creations that race with one key therefore
|
|
65
|
+
find the same record.
|
|
55
66
|
|
|
56
67
|
## Events
|
|
57
68
|
|
|
58
|
-
Each change records its events in the same write as the change itself
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
event
|
|
69
|
+
Each change records its events in the same write as the change itself. The
|
|
70
|
+
module delivers them from there to the handlers registered on
|
|
71
|
+
`SubscriptionEvents`, in order. If a handler rejects an event, delivery stops
|
|
72
|
+
at that event and the module tries again after `errorBackoffMs`, until every
|
|
73
|
+
handler accepts. Delivery is at least once, and an event survives a crash
|
|
74
|
+
between the change and its delivery.
|
package/docs/2.renewal-clock.md
CHANGED
|
@@ -1,95 +1,111 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
title: The renewal clock
|
|
3
|
+
description: How the module decides when to charge, picks polling or Redis, claims a renewal, and stays safe when several instances run or one dies mid-charge.
|
|
4
|
+
mode: explanation
|
|
5
|
+
---
|
|
2
6
|
|
|
3
|
-
The module decides on its own when a subscription needs
|
|
4
|
-
|
|
5
|
-
run at once and
|
|
7
|
+
The module decides on its own when a subscription needs a charge. That is the
|
|
8
|
+
reason it exists. It is also the part that has to be right when several
|
|
9
|
+
instances run at once and any of them can die in the middle of a charge.
|
|
6
10
|
|
|
7
|
-
## The database decides
|
|
11
|
+
## The database decides and the clock only triggers
|
|
8
12
|
|
|
9
|
-
Every subscription record carries `dueAt
|
|
10
|
-
trigger
|
|
11
|
-
passed.
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
Every subscription record carries `dueAt`, the time when it next needs
|
|
14
|
+
attention. A trigger has one job: make an instance look at the records whose
|
|
15
|
+
`dueAt` has passed. The instance reads what to do from the record, never from
|
|
16
|
+
the trigger. It charges the next period, retries a failed one, reads a
|
|
17
|
+
settling charge again, or ends the subscription at period end.
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
Whichever trigger runs, the database holds the same information. If Redis
|
|
20
|
+
never fires a renewal, the next sweep still makes it.
|
|
17
21
|
|
|
18
22
|
## Choosing the trigger
|
|
19
23
|
|
|
20
|
-
At start, before
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
the
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
At start, before the module touches Redis at all, it picks one of two
|
|
25
|
+
triggers:
|
|
26
|
+
|
|
27
|
+
1. If `@antelopejs/interface-redis` or `@antelopejs/interface-redis-scheduler`
|
|
28
|
+
has no implementation wired, the module **polls**. It sweeps the database
|
|
29
|
+
every `pollIntervalMs`, so a renewal is charged at most one interval late.
|
|
30
|
+
2. Otherwise the module **uses the Redis scheduler**. Each subscription is one
|
|
31
|
+
task, named after the subscription and fired at its `dueAt`. When `dueAt`
|
|
32
|
+
changes, the module schedules the task again, and when `dueAt` is cleared, it
|
|
33
|
+
removes the task. A sweep still runs every `sweepIntervalMs`, because the
|
|
34
|
+
scheduler drops a task whose handler dies mid-run, and the database alone
|
|
35
|
+
must be enough.
|
|
36
|
+
|
|
37
|
+
The Redis interfaces are optional dependencies, and the module imports them
|
|
38
|
+
lazily after the wiring check. Nothing on a request path or in `start()` waits
|
|
39
|
+
for a Redis connection. A Redis that is wired but unreachable delays renewals
|
|
40
|
+
until the next database sweep, and it blocks nothing.
|
|
41
|
+
|
|
42
|
+
The suite checks the Redis scheduler through normal interface routing, without
|
|
43
|
+
a test-only bypass for handler registration. If the scheduler refuses to
|
|
44
|
+
register the handler, the module logs a warning and falls back to polling. At
|
|
45
|
+
start, the module logs which clock it chose, and `ActiveClock()` returns
|
|
46
|
+
`"polling"` or `"redis"`.
|
|
39
47
|
|
|
40
48
|
## Claiming a renewal
|
|
41
49
|
|
|
42
|
-
Several instances
|
|
43
|
-
a conditional write against its current revision (`atomicMutation`),
|
|
44
|
-
|
|
50
|
+
Several instances can find the same renewal due. The module changes each record
|
|
51
|
+
only with a conditional write against its current revision (`atomicMutation`),
|
|
52
|
+
and a charge starts with one:
|
|
45
53
|
|
|
46
|
-
1. Read the record. If it is not due, or another instance
|
|
47
|
-
expired, leave it.
|
|
48
|
-
2. Write a claim
|
|
49
|
-
|
|
50
|
-
succeeds. The
|
|
54
|
+
1. Read the record. If it is not due, or the claim of another instance on it
|
|
55
|
+
has not expired, leave it.
|
|
56
|
+
2. Write a claim against the revision just read. The claim names this instance
|
|
57
|
+
and lasts until now plus `leaseMs`. Of several instances that read the same
|
|
58
|
+
revision, exactly one write succeeds. The others stand down without a call
|
|
59
|
+
to the provider.
|
|
51
60
|
3. Charge.
|
|
52
|
-
4.
|
|
53
|
-
release the claim.
|
|
61
|
+
4. Read the record again and write the outcome against its current revision.
|
|
62
|
+
Then deliver the events and release the claim.
|
|
54
63
|
|
|
55
|
-
If a cancellation lands between 2 and 4,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
If a cancellation lands between step 2 and step 4, step 4 applies the outcome
|
|
65
|
+
to the record as it is now. A charge that went through on a subscription that
|
|
66
|
+
was just cancelled is recorded and reported, and the subscription stays
|
|
67
|
+
cancelled.
|
|
59
68
|
|
|
60
69
|
## When an instance dies mid-charge
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
Before the first provider call, the module persists the payment
|
|
64
|
-
`attemptStartedAt
|
|
65
|
-
strictly before that deadline. The deadline
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
margin for clock skew, request
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
The claim of that instance expires after `leaseMs`, and another instance claims
|
|
72
|
+
the renewal. Before the first provider call, the module persists the payment
|
|
73
|
+
method, `attemptStartedAt`, and `attemptReplayUntil`. A takeover sends the same
|
|
74
|
+
idempotency key only strictly before that deadline. The deadline comes from
|
|
75
|
+
`guaranteedReplayMs`, which defaults to zero, and a later configuration change
|
|
76
|
+
never extends it. A lease is not an idempotency guarantee. Configure a
|
|
77
|
+
conservative retention duration, with enough margin for clock skew, request
|
|
78
|
+
latency, and process stalls across all instances.
|
|
79
|
+
|
|
80
|
+
If the result is still unknown after the protection expires, processing halts.
|
|
81
|
+
The subscription reports `vendorData.reconciliationRequired: true`, and the
|
|
82
|
+
module logs an error that contains the subscription ID. A record whose attempt
|
|
83
|
+
has already started but carries no attempt timing does not get a new replay
|
|
84
|
+
window. It halts for reconciliation too. The module reads a known pending
|
|
85
|
+
payment by its ID, even after the deadline, and that read never submits a
|
|
86
|
+
charge. For the operational recovery procedure, see
|
|
87
|
+
[failed renewals](./3.failed-renewals.md#reconciliation-and-operational-recovery).
|
|
88
|
+
|
|
89
|
+
The deadline is the second guard. The first guard, the claim, means that the
|
|
90
|
+
provider normally never sees a second request at all.
|
|
79
91
|
|
|
80
92
|
## Several instances
|
|
81
93
|
|
|
82
|
-
|
|
83
|
-
claim identity, sweeps on its own, and delivers the
|
|
84
|
-
processed to the handlers registered in it. Any
|
|
85
|
-
database.
|
|
94
|
+
No instance keeps anything in memory that another instance needs. Each
|
|
95
|
+
instance has its own claim identity, sweeps on its own, and delivers the
|
|
96
|
+
events of the records it processed to the handlers registered in it. Any
|
|
97
|
+
number of instances can run against one database.
|
|
86
98
|
|
|
87
99
|
## Testing the clock
|
|
88
100
|
|
|
89
|
-
The clock the module reads is replaceable (`UseClock`),
|
|
90
|
-
manual
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
101
|
+
The clock that the module reads is replaceable (`UseClock` in `src/clock.ts`),
|
|
102
|
+
and the suite drives a manual clock through trials, renewals, failures,
|
|
103
|
+
retries, and period ends. Separate tests check the real triggers in real time:
|
|
104
|
+
|
|
105
|
+
- A renewal made by polling, with nobody calling.
|
|
106
|
+
- With Redis, a task scheduled for the end of a trial and fired by Redis while
|
|
107
|
+
the sweep is an hour away.
|
|
108
|
+
- With Redis, a task deleted from Redis whose renewal the sweep still makes.
|
|
109
|
+
|
|
110
|
+
A boot test launches a project the way production does, without the test
|
|
111
|
+
harness, and checks that it starts and renews.
|
|
@@ -1,126 +1,156 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
title: Failed renewals
|
|
3
|
+
description: The retry policy of this module for declined renewals, settling charges, refusals, an unreachable provider, the first period, and reconciliation.
|
|
4
|
+
mode: reference
|
|
5
|
+
---
|
|
2
6
|
|
|
3
|
-
The interface promises only that a failed renewal is reported as `past_due`.
|
|
4
|
-
happens next is each implementation
|
|
7
|
+
The interface promises only that a failed renewal is reported as `past_due`.
|
|
8
|
+
What happens next is the policy of each implementation. This page describes
|
|
9
|
+
the policy of this module.
|
|
5
10
|
|
|
6
11
|
## The policy
|
|
7
12
|
|
|
8
|
-
When
|
|
13
|
+
When the charge for a period is declined:
|
|
9
14
|
|
|
10
|
-
1. The period advances anyway, the subscription goes `past_due`, and
|
|
11
|
-
`subscription.payment_failed
|
|
12
|
-
2.
|
|
13
|
-
before it. By default
|
|
14
|
-
eleven days.
|
|
15
|
-
3. A retry that succeeds makes
|
|
16
|
-
does not move.
|
|
17
|
-
4. When the last retry fails, the subscription ends
|
|
18
|
-
`endReason: "payment_failed"`, and
|
|
19
|
-
`subscription.
|
|
15
|
+
1. The period advances anyway, the subscription goes `past_due`, and the module
|
|
16
|
+
raises `subscription.payment_failed`. The unpaid period is the current one.
|
|
17
|
+
2. The module retries the charge after each entry of `retryDelaysMs`, measured
|
|
18
|
+
from the failure before it. By default the delays are one day, three days,
|
|
19
|
+
and seven days, which makes four attempts over eleven days.
|
|
20
|
+
3. A retry that succeeds makes the subscription `active` again, in the same
|
|
21
|
+
period. The cycle does not move.
|
|
22
|
+
4. When the last retry fails, the subscription ends. It becomes `canceled`,
|
|
23
|
+
with `endReason: "payment_failed"`, and the module raises both
|
|
24
|
+
`subscription.payment_failed` and `subscription.canceled`.
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
of the
|
|
26
|
+
A payment with the status `failed`, `canceled`, or `requires_action` counts as
|
|
27
|
+
a decline.
|
|
28
|
+
|
|
29
|
+
Each new attempt uses the payment method of the subscription at the time of
|
|
30
|
+
the attempt. A payer recovers by calling `UpdateSubscriptionPaymentMethod`
|
|
31
|
+
during `past_due`. Each attempt has its own idempotency key. A retry is a new
|
|
32
|
+
charge, not a replay of the decline.
|
|
25
33
|
|
|
26
34
|
A subscription set to end at period end keeps being retried until its period
|
|
27
|
-
ends
|
|
35
|
+
ends. Then it ends, and the module retries no further.
|
|
28
36
|
|
|
29
37
|
## A charge that is still settling
|
|
30
38
|
|
|
31
|
-
A bank debit can report `pending` for days. The
|
|
32
|
-
`
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
A bank debit can report `pending` for days. The module treats a payment with
|
|
40
|
+
the status `pending` or `authorized` as settling. The subscription keeps its
|
|
41
|
+
status: `active` after a trial or a paid period, or `past_due` during retries.
|
|
42
|
+
The module reads the charge again every `settlementPollMs` until it settles,
|
|
43
|
+
then treats it as succeeded or declined. Nothing is reported until it settles.
|
|
35
44
|
|
|
36
|
-
If the subscription is cancelled while a charge is settling, the
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
If the subscription is cancelled while a charge is settling, the module still
|
|
46
|
+
follows the charge. A charge that then succeeds is recorded and reported, so
|
|
47
|
+
that the merchant can refund the money.
|
|
39
48
|
|
|
40
49
|
## A provider that refuses the charge
|
|
41
50
|
|
|
42
|
-
`CustomerNotFoundError`, `PaymentNotFoundError`, `InvalidPaymentStateError
|
|
43
|
-
`UnsupportedOperationError` from the payment interface mean the
|
|
44
|
-
not make this charge
|
|
45
|
-
They count as a declined attempt
|
|
46
|
-
|
|
47
|
-
`
|
|
51
|
+
`CustomerNotFoundError`, `PaymentNotFoundError`, `InvalidPaymentStateError`,
|
|
52
|
+
and `UnsupportedOperationError` from the payment interface mean that the
|
|
53
|
+
provider will not make this charge. The most common cause is a saved method
|
|
54
|
+
that has been removed. They count as a declined attempt. The subscription goes
|
|
55
|
+
`past_due`, the module retries it under the policy, and it ends when the
|
|
56
|
+
retries are spent. The `subscription.payment_failed` event has no `payment`,
|
|
57
|
+
because no payment was made.
|
|
48
58
|
|
|
49
|
-
|
|
59
|
+
If one of these errors comes back when the module reads a settling charge, the
|
|
60
|
+
module does not count it as a decline. It holds the subscription for
|
|
61
|
+
reconciliation.
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
attempt can be made again after `errorBackoffMs` only within its persisted
|
|
53
|
-
provider guarantee, with the same idempotency key **and the same payment method**,
|
|
54
|
-
recorded before the first try — so if the first request did reach the provider,
|
|
55
|
-
the retry finds that charge instead of making another, even if the method was
|
|
56
|
-
changed meanwhile. The new method is used from the next attempt.
|
|
63
|
+
## A provider that cannot be reached
|
|
57
64
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
Any other `PaymentError` is not a decline. The charge is in doubt. The module
|
|
66
|
+
can make the same attempt again after `errorBackoffMs`, but only within the
|
|
67
|
+
persisted provider guarantee. The retry uses the same idempotency key **and
|
|
68
|
+
the same payment method**, both recorded before the first try. If the first
|
|
69
|
+
request did reach the provider, the retry finds that charge instead of making
|
|
70
|
+
another, even if the method was changed in the meantime. The new method is
|
|
71
|
+
used from the next attempt. With the default `guaranteedReplayMs` of zero, the
|
|
72
|
+
guarantee has already expired, so the module holds the subscription for
|
|
73
|
+
reconciliation instead of retrying.
|
|
74
|
+
|
|
75
|
+
If the subscription is cancelled while a charge is in doubt, the module does
|
|
76
|
+
not make the attempt again, and it holds the record for reconciliation. A
|
|
77
|
+
charge that did land then reaches the merchant only as the
|
|
78
|
+
`payment.succeeded` webhook of the payment interface, which carries
|
|
79
|
+
`antelopeSubscription`.
|
|
61
80
|
|
|
62
81
|
## After a stall
|
|
63
82
|
|
|
64
|
-
Periods are
|
|
65
|
-
than a period
|
|
66
|
-
period
|
|
83
|
+
Periods are counted from the anchor. If the module could not process a
|
|
84
|
+
subscription for longer than a period, because the database or every instance
|
|
85
|
+
was down, it charges every missed period on recovery, one after another.
|
|
67
86
|
|
|
68
87
|
## The first period
|
|
69
88
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
subscription
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
A creation
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
When the request has neither `trialEnd` nor `billingAnchor`,
|
|
90
|
+
`CreateSubscription` charges the first period itself, and a decline there
|
|
91
|
+
creates no subscription. A declined payment rejects with
|
|
92
|
+
`SubscriptionPaymentError`. A refusal from the provider, with no payment made,
|
|
93
|
+
rejects with `InvalidSubscriptionRequestError`. A payer with no subscription is
|
|
94
|
+
not `past_due`. With `trialEnd` or `billingAnchor`, the module creates the
|
|
95
|
+
subscription at once and charges the first period at that time, under the
|
|
96
|
+
policy above.
|
|
97
|
+
|
|
98
|
+
A creation that ends without an answer must be replayed with the same
|
|
99
|
+
idempotency key. That happens when the provider could not be reached, or when
|
|
100
|
+
the instance died during the call. The replay resumes the creation only within
|
|
101
|
+
its persisted replay window. After the window expires, the replay rejects with
|
|
102
|
+
the error `Subscription <id> requires reconciliation; charge replay protection unavailable or expired`
|
|
103
|
+
and does not charge again. A refused creation stays refused on replay. The
|
|
104
|
+
module never completes a creation that nobody replays, and a charge that such
|
|
105
|
+
a creation made belongs to no subscription. That charge still reaches the
|
|
106
|
+
merchant as a webhook of the payment interface that carries
|
|
107
|
+
`antelopeSubscription`.
|
|
82
108
|
|
|
83
109
|
## Reconciliation and operational recovery
|
|
84
110
|
|
|
85
|
-
`guaranteedReplayMs` defaults to zero
|
|
86
|
-
|
|
87
|
-
provider guarantee that covers every account
|
|
88
|
-
margin.
|
|
89
|
-
|
|
111
|
+
`guaranteedReplayMs` defaults to zero. The module makes the first call, but it
|
|
112
|
+
does not replay an uncertain result blindly. Set a positive value only from a
|
|
113
|
+
documented provider guarantee that covers every account that uses this module,
|
|
114
|
+
with a safety margin. The module retries transient errors only strictly before
|
|
115
|
+
the saved deadline. Declines follow the separate, finite `retryDelaysMs` policy
|
|
116
|
+
with new attempt keys.
|
|
90
117
|
|
|
91
118
|
Live subscriptions expose `reconciliationRequired`, `attemptStartedAt`,
|
|
92
|
-
`attemptReplayUntil
|
|
93
|
-
charge scheduled. Cancellation and payment
|
|
94
|
-
hold.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
1. Stop all module instances
|
|
100
|
-
record and
|
|
101
|
-
payment method. Never delete the record or reset timing to
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
119
|
+
`attemptReplayUntil`, and `phase` in `vendorData`. A halted record has no next
|
|
120
|
+
charge scheduled. Cancellation and payment method changes do not clear the
|
|
121
|
+
hold. An initial creation is not a public live subscription yet, so it exposes
|
|
122
|
+
the hold through the replay error and the persisted record.
|
|
123
|
+
|
|
124
|
+
To recover a halted subscription:
|
|
125
|
+
|
|
126
|
+
1. Stop all module instances and triggers, and drain the requests in flight.
|
|
127
|
+
Back up the record, and keep its revision, provider account, attempt key,
|
|
128
|
+
timing, and payment method. Never delete the record or reset its timing to
|
|
129
|
+
force a replay.
|
|
130
|
+
2. Search the authoritative ledger of the provider, including webhooks and the
|
|
131
|
+
metadata key `antelopeSubscription`, with the original account and attempt
|
|
132
|
+
key. If the result is still unknown, leave the hold in place and escalate to
|
|
133
|
+
the provider.
|
|
134
|
+
3. If you find a payment, verify its customer, method, amount, currency,
|
|
135
|
+
subscription metadata, and original attempt key. Recover the target period
|
|
107
136
|
index from that key (`payment-subscriptions:<id>:<period>:<attempt>`), not
|
|
108
|
-
from the
|
|
137
|
+
from the last paid period of the record. If you cannot establish the target,
|
|
109
138
|
leave the hold in place.
|
|
110
|
-
An operator-reviewed
|
|
111
|
-
as `pendingPayment`, clear `reconciliationRequired`, and set
|
|
112
|
-
and `dueAt` to now with the lease cleared. Set `periodIndex`
|
|
113
|
-
target for every record,
|
|
114
|
-
For a `creating` record, replay the original creation
|
|
115
|
-
payment instead of charging. For a live
|
|
116
|
-
record, restart processing
|
|
117
|
-
original attempt identity and timing intact.
|
|
118
|
-
4. If the provider
|
|
119
|
-
|
|
120
|
-
merchant decision. Do not
|
|
139
|
+
An operator-reviewed database repair, conditional on the revision, can
|
|
140
|
+
attach that ID as `pendingPayment`, clear `reconciliationRequired`, and set
|
|
141
|
+
`nextActionAt` and `dueAt` to now with the lease cleared. Set `periodIndex`
|
|
142
|
+
to the recovered target for every record, renewals included. The initial
|
|
143
|
+
charge targets `0`. For a `creating` record, replay the original creation
|
|
144
|
+
key, and the module reads that payment instead of charging. For a live
|
|
145
|
+
record, restart processing so that the module reads and applies the actual
|
|
146
|
+
status of the payment. Keep the original attempt identity and timing intact.
|
|
147
|
+
4. If the provider proves that no charge exists, prefer to retire the held
|
|
148
|
+
subscription and create a replacement with a new key, under an audited
|
|
149
|
+
merchant decision. Do not retry the uncertain attempt silently. Resolve any
|
|
121
150
|
provider schedule registration as well. Refunds and entitlement corrections
|
|
122
|
-
|
|
151
|
+
stay merchant operations.
|
|
123
152
|
|
|
124
|
-
|
|
125
|
-
repair
|
|
126
|
-
a method update or a longer configuration value is not
|
|
153
|
+
The module has no portable force-retry or reconciliation API, on purpose. A
|
|
154
|
+
database repair needs evidence from the provider and an audit trail. An
|
|
155
|
+
elapsed lease, a method update, or a longer configuration value is not
|
|
156
|
+
evidence of non-payment.
|