@antelopejs/interface-subscriptions 0.0.0-stage → 0.0.2
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/LICENSE +190 -0
- package/README.md +215 -2
- package/dist/antelope.test.d.ts +2 -0
- package/dist/antelope.test.js +10 -0
- package/dist/antelope.test.js.map +1 -0
- package/dist/errors.d.ts +68 -0
- package/dist/errors.js +85 -0
- package/dist/errors.js.map +1 -0
- package/dist/functions.d.ts +101 -0
- package/dist/functions.js +104 -0
- package/dist/functions.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/tests/subscriptions-changes.test.d.ts +1 -0
- package/dist/tests/subscriptions-changes.test.js +194 -0
- package/dist/tests/subscriptions-changes.test.js.map +1 -0
- package/dist/tests/subscriptions.test.d.ts +1 -0
- package/dist/tests/subscriptions.test.js +144 -0
- package/dist/tests/subscriptions.test.js.map +1 -0
- package/dist/tests/support.d.ts +77 -0
- package/dist/tests/support.js +181 -0
- package/dist/tests/support.js.map +1 -0
- package/dist/types.d.ts +228 -0
- package/dist/types.js +71 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +16 -0
- package/dist/validation.js +59 -0
- package/dist/validation.js.map +1 -0
- package/docs/1.introduction.md +98 -0
- package/docs/2.subscriptions.md +147 -0
- package/docs/3.events.md +79 -0
- package/docs/4.implementing.md +158 -0
- package/docs/5.provider-capability-matrix.md +61 -0
- package/docs/tsconfig.json +14 -0
- package/package.json +80 -4
- package/skills/managing-subscriptions/SKILL.md +154 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright (c) 2025-present - AntelopeJS Team
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,216 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @antelopejs/interface-subscriptions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@antelopejs/interface-subscriptions)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
Provider-agnostic subscriptions for AntelopeJS. Bill a saved payment method
|
|
7
|
+
every period — with trials, a billing anchor, cancellation now or at period end,
|
|
8
|
+
and `past_due` reporting — without naming a provider, and without every
|
|
9
|
+
consumer rebuilding a billing cycle of its own.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @antelopejs/interface-subscriptions
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This package declares the contract and ships no billing logic. Wire a module
|
|
18
|
+
that implements it:
|
|
19
|
+
|
|
20
|
+
- a provider that fires renewals itself implements it natively (Stripe, PayPal,
|
|
21
|
+
Mollie);
|
|
22
|
+
- for any other `@antelopejs/interface-payment` provider, wire
|
|
23
|
+
`@antelopejs/payment-subscriptions`, which runs the renewal clock itself on
|
|
24
|
+
saved methods and off-session charges.
|
|
25
|
+
|
|
26
|
+
A payment module that cannot do subscriptions simply does not implement this
|
|
27
|
+
interface. There is no half-honoured contract and no `UnsupportedOperationError`
|
|
28
|
+
here.
|
|
29
|
+
|
|
30
|
+
For Stripe, native subscriptions in `@antelopejs/stripe` are recommended.
|
|
31
|
+
`@antelopejs/payment-subscriptions` is an explicit alternative, not an automatic
|
|
32
|
+
fallback. Wire one renewal owner per subscription, never both.
|
|
33
|
+
|
|
34
|
+
## Choose a task
|
|
35
|
+
|
|
36
|
+
- Start or cancel recurring access: [subscriptions](./docs/2.subscriptions.md).
|
|
37
|
+
- Synchronize access after billing: [events](./docs/3.events.md).
|
|
38
|
+
- Implement a billing provider: [implementation guide](./docs/4.implementing.md).
|
|
39
|
+
- Choose native or generic billing: [provider capabilities](./docs/5.provider-capability-matrix.md).
|
|
40
|
+
|
|
41
|
+
Replay protection lasts only for the provider-defined replay window. Persist
|
|
42
|
+
operation keys, subscription IDs and payment IDs. After expiration, applications
|
|
43
|
+
must reconcile uncertain payments with provider records before retrying; reusing
|
|
44
|
+
a key is not indefinite duplicate-charge protection.
|
|
45
|
+
|
|
46
|
+
## What it covers
|
|
47
|
+
|
|
48
|
+
| Area | Functions |
|
|
49
|
+
| --------- | --------------------------------------------------------------- |
|
|
50
|
+
| Lifecycle | `CreateSubscription`, `CancelSubscription` |
|
|
51
|
+
| Reading | `GetSubscription`, `ListSubscriptions` |
|
|
52
|
+
| Recovery | `UpdateSubscriptionPaymentMethod` |
|
|
53
|
+
| Events | `SubscriptionEvents` (a registering proxy, not a webhook route) |
|
|
54
|
+
|
|
55
|
+
The portable core is small on purpose: amount, interval and interval count,
|
|
56
|
+
billing anchor, trial end, status including `past_due`, cancel now or at period
|
|
57
|
+
end, and the current period. Proration, dunning schedules, tax, coupons, metered
|
|
58
|
+
billing and multiple items per subscription are out, because the generic
|
|
59
|
+
fallback cannot honour them and a contract that only some implementations keep
|
|
60
|
+
is a lie. For those, use the provider's own interface.
|
|
61
|
+
|
|
62
|
+
## Quick start
|
|
63
|
+
|
|
64
|
+
```ts docs-check
|
|
65
|
+
import {
|
|
66
|
+
CreatePayment,
|
|
67
|
+
GetPayment,
|
|
68
|
+
ListPaymentMethods,
|
|
69
|
+
} from "@antelopejs/interface-payment";
|
|
70
|
+
import { CreateSubscription } from "@antelopejs/interface-subscriptions";
|
|
71
|
+
|
|
72
|
+
// 1. Take the mandate with a separate €1 on-session payment.
|
|
73
|
+
export async function startSignup(
|
|
74
|
+
customer: string,
|
|
75
|
+
token: string,
|
|
76
|
+
signupId: string,
|
|
77
|
+
) {
|
|
78
|
+
const payment = await CreatePayment(
|
|
79
|
+
{
|
|
80
|
+
amount: { value: 100, currency: "EUR" },
|
|
81
|
+
reference: signupId,
|
|
82
|
+
customer,
|
|
83
|
+
paymentMethod: token,
|
|
84
|
+
savePaymentMethod: true,
|
|
85
|
+
returnUrl: "https://shop.example/billing/return",
|
|
86
|
+
},
|
|
87
|
+
`save-${signupId}`,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
// The route must redirect the payer when this URL is returned.
|
|
91
|
+
return {
|
|
92
|
+
paymentId: payment.id,
|
|
93
|
+
status: payment.status,
|
|
94
|
+
redirectUrl:
|
|
95
|
+
payment.status === "requires_action" ? payment.redirectUrl : undefined,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 2. Resume from trusted server state, not the browser's claimed outcome.
|
|
100
|
+
export async function finishSignup(paymentId: string, accountId: string) {
|
|
101
|
+
const payment = await GetPayment(paymentId);
|
|
102
|
+
if (payment.status !== "succeeded" && payment.status !== "authorized") {
|
|
103
|
+
return undefined; // Still pending/action required, or failed/canceled: do not subscribe.
|
|
104
|
+
}
|
|
105
|
+
if (!payment.customer) throw new Error("Signup payment has no customer");
|
|
106
|
+
|
|
107
|
+
const methods = await ListPaymentMethods(payment.customer);
|
|
108
|
+
const method = methods.find((saved) => saved.id === payment.paymentMethod);
|
|
109
|
+
if (!method) throw new Error("Signup payment has no saved method");
|
|
110
|
+
|
|
111
|
+
return CreateSubscription(
|
|
112
|
+
{
|
|
113
|
+
reference: accountId,
|
|
114
|
+
customer: payment.customer,
|
|
115
|
+
paymentMethod: method.id,
|
|
116
|
+
amount: { value: 1900, currency: "EUR" },
|
|
117
|
+
interval: "month",
|
|
118
|
+
},
|
|
119
|
+
`subscribe-${payment.id}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Use an existing payment-interface customer and a provider-issued method token.
|
|
125
|
+
Persist a unique `signupId` and the returned `paymentId` against the authenticated
|
|
126
|
+
account. Replace the return URL with your own route and send the payer to
|
|
127
|
+
`redirectUrl` when action is required. If the payment is pending, wait for a
|
|
128
|
+
verified `payment.succeeded` or `payment.authorized` webhook before retrying
|
|
129
|
+
`finishSignup`; failed or canceled payments must not create subscriptions.
|
|
130
|
+
|
|
131
|
+
Call `finishSignup` with the saved payment ID and account ID, using the same
|
|
132
|
+
provider account throughout. The saved-method payment and the first subscription
|
|
133
|
+
period are separate charges (€1 then €19 here); the first period is charged
|
|
134
|
+
off-session before `CreateSubscription` resolves. Adapt those amounts to your
|
|
135
|
+
billing policy. Register an idempotent handler for subsequent lifecycle events:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { SubscriptionEvents } from "@antelopejs/interface-subscriptions";
|
|
139
|
+
|
|
140
|
+
SubscriptionEvents.register("billing", async (event) => {
|
|
141
|
+
await grantOrRevokeAccess(event.subscription);
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`grantOrRevokeAccess` stands for your application's access policy. Deduplicate
|
|
146
|
+
events by `event.id` as described in [Events](./docs/3.events.md).
|
|
147
|
+
|
|
148
|
+
## Compatibility
|
|
149
|
+
|
|
150
|
+
<!-- docs-compat: ./package.json#peerDependencies -->
|
|
151
|
+
|
|
152
|
+
| Package | Range |
|
|
153
|
+
| ------------------------------- | ---------------- |
|
|
154
|
+
| `@antelopejs/interface-core` | `>=0.0.5 <1.0.0` |
|
|
155
|
+
| `@antelopejs/interface-payment` | `>=0.0.2 <1.0.0` |
|
|
156
|
+
|
|
157
|
+
These are the peer requirements in `package.json`. Development uses the same
|
|
158
|
+
payment-interface semver range. Check your implementing module's requirements
|
|
159
|
+
too. `@antelopejs/core` 1.16.0 is a development test runner, not a runtime peer.
|
|
160
|
+
|
|
161
|
+
## Conventions
|
|
162
|
+
|
|
163
|
+
- **Built on the payment interface's portable pair**: one on-session payment that
|
|
164
|
+
saves a method, then off-session charges. Customers, methods and payments are
|
|
165
|
+
`@antelopejs/interface-payment` identifiers from the same account.
|
|
166
|
+
- **An idempotency key on every mutating call**, positionally.
|
|
167
|
+
- **A declined renewal is a status, not an exception.** Only a declined _first_
|
|
168
|
+
period rejects, with `SubscriptionPaymentError`, and creates nothing.
|
|
169
|
+
- **`past_due` is portable to report; the retry policy is not.**
|
|
170
|
+
- **`vendorData` is a read-only echo.** No request accepts it.
|
|
171
|
+
- **An optional trailing `provider`** selects the payment account, as in the
|
|
172
|
+
payment interface.
|
|
173
|
+
|
|
174
|
+
## Documentation
|
|
175
|
+
|
|
176
|
+
| Document | Contents |
|
|
177
|
+
| -------------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
178
|
+
| [Introduction](./docs/1.introduction.md) | Why a second interface, the portable core, who fires renewals |
|
|
179
|
+
| [Subscriptions](./docs/2.subscriptions.md) | Creating, periods, trials and anchors, statuses, cancelling |
|
|
180
|
+
| [Events](./docs/3.events.md) | The event set, delivery, and how it relates to payment webhooks |
|
|
181
|
+
| [Implementing](./docs/4.implementing.md) | Native and fallback implementations, the conformance suite |
|
|
182
|
+
| [Provider capability matrix](./docs/5.provider-capability-matrix.md) | Which providers fire renewals themselves, and what that means |
|
|
183
|
+
|
|
184
|
+
## Development
|
|
185
|
+
|
|
186
|
+
Install the published payment interface from npm using the frozen lockfile:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
pnpm install --frozen-lockfile
|
|
190
|
+
pnpm test
|
|
191
|
+
pnpm lint
|
|
192
|
+
pnpm format:check
|
|
193
|
+
pnpm knip
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
CI runs the same checks against the published dependency without Git submodules or workspace overrides.
|
|
197
|
+
|
|
198
|
+
## Conformance suite
|
|
199
|
+
|
|
200
|
+
`src/tests/` ships in the package as `dist/tests` and runs against whichever
|
|
201
|
+
module implements the interface. It is much weaker than the payment suite, and
|
|
202
|
+
on purpose: a test cannot wait a month for a renewal, so for a native provider it
|
|
203
|
+
covers creation, reading, cancellation, method changes and request validation —
|
|
204
|
+
not renewals. The generic fallback's own suite drives an injected clock through
|
|
205
|
+
every renewal path and is the executable specification for the rest.
|
|
206
|
+
|
|
207
|
+
| Variable | Purpose |
|
|
208
|
+
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
|
209
|
+
| `ANTELOPE_PAYMENT_TEST_METHOD` | A token that saves on-session and charges off-session. Without it, everything but the unknown-id tests skips. |
|
|
210
|
+
| `ANTELOPE_SUBSCRIPTIONS_TEST_METHOD_DECLINES_LATER` | A token that saves on-session and is declined off-session. Without it, the declined-first-period tests skip. |
|
|
211
|
+
| `ANTELOPE_SUBSCRIPTIONS_TEST_EVENTS` | Set when events can reach the test process. A native provider delivers them by webhook, which usually cannot. |
|
|
212
|
+
| `ANTELOPE_PAYMENT_TEST_CURRENCY` | ISO-4217 code to run against. Defaults to `EUR`. |
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
Apache-2.0
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const config_1 = require("@antelopejs/interface-core/config");
|
|
4
|
+
exports.default = (0, config_1.defineConfig)({
|
|
5
|
+
name: "interface-subscriptions-test",
|
|
6
|
+
cacheFolder: ".antelope/cache",
|
|
7
|
+
modules: {},
|
|
8
|
+
test: { folder: "test" },
|
|
9
|
+
});
|
|
10
|
+
//# sourceMappingURL=antelope.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"antelope.test.js","sourceRoot":"","sources":["../src/antelope.test.ts"],"names":[],"mappings":";;AAAA,8DAAiE;AAEjE,kBAAe,IAAA,qBAAY,EAAC;IAC1B,IAAI,EAAE,8BAA8B;IACpC,WAAW,EAAE,iBAAiB;IAC9B,OAAO,EAAE,EAAE;IACX,IAAI,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;CACzB,CAAC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Payment } from "@antelopejs/interface-payment";
|
|
2
|
+
import type { SubscriptionStatus } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Base class for every error this interface raises.
|
|
5
|
+
*
|
|
6
|
+
* A failed *renewal* is not an error: it is reported as a `past_due` status
|
|
7
|
+
* and a `subscription.payment_failed` event. These errors mean the call itself
|
|
8
|
+
* did not happen.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SubscriptionError extends Error {
|
|
11
|
+
/** The provider's own error code, verbatim and opaque. */
|
|
12
|
+
readonly providerCode?: string | undefined;
|
|
13
|
+
constructor(message: string,
|
|
14
|
+
/** The provider's own error code, verbatim and opaque. */
|
|
15
|
+
providerCode?: string | undefined);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* No subscription exists under that identifier, for that provider.
|
|
19
|
+
*
|
|
20
|
+
* Also raised for an identifier that cannot be parsed at all.
|
|
21
|
+
*/
|
|
22
|
+
export declare class SubscriptionNotFoundError extends SubscriptionError {
|
|
23
|
+
constructor(id: string, providerCode?: string);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The request is malformed, or asks for something outside the portable core:
|
|
27
|
+
* a non-positive amount, an interval count below 1, a trial end in the past,
|
|
28
|
+
* both a trial and a billing anchor, a period longer than the provider allows.
|
|
29
|
+
*/
|
|
30
|
+
export declare class InvalidSubscriptionRequestError extends SubscriptionError {
|
|
31
|
+
constructor(message: string, providerCode?: string);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The request was well-formed but does not apply to the subscription's current
|
|
35
|
+
* state: cancelling one that has already ended, changing the payment method of
|
|
36
|
+
* one that has ended.
|
|
37
|
+
*/
|
|
38
|
+
export declare class InvalidSubscriptionStateError extends SubscriptionError {
|
|
39
|
+
/** The status the subscription was actually in. */
|
|
40
|
+
readonly status?: SubscriptionStatus | undefined;
|
|
41
|
+
constructor(message: string,
|
|
42
|
+
/** The status the subscription was actually in. */
|
|
43
|
+
status?: SubscriptionStatus | undefined, providerCode?: string);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The first period could not be charged, so no subscription was created.
|
|
47
|
+
*
|
|
48
|
+
* Only {@link CreateSubscription} raises this, and only when it charges at
|
|
49
|
+
* once — no trial, no future billing anchor. Later charges that fail are a
|
|
50
|
+
* `past_due` status, not an error.
|
|
51
|
+
*/
|
|
52
|
+
export declare class SubscriptionPaymentError extends SubscriptionError {
|
|
53
|
+
/**
|
|
54
|
+
* The declined payment, as the payment interface reports it: `failed`, or
|
|
55
|
+
* `canceled` where the provider voids the declined attempt when it refuses
|
|
56
|
+
* the subscription (Stripe does). `GetPayment` on its id agrees. The
|
|
57
|
+
* decline reason is in `failure` when `failed`, and in `providerCode`.
|
|
58
|
+
*/
|
|
59
|
+
readonly payment: Payment;
|
|
60
|
+
constructor(
|
|
61
|
+
/**
|
|
62
|
+
* The declined payment, as the payment interface reports it: `failed`, or
|
|
63
|
+
* `canceled` where the provider voids the declined attempt when it refuses
|
|
64
|
+
* the subscription (Stripe does). `GetPayment` on its id agrees. The
|
|
65
|
+
* decline reason is in `failure` when `failed`, and in `providerCode`.
|
|
66
|
+
*/
|
|
67
|
+
payment: Payment, providerCode?: string);
|
|
68
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubscriptionPaymentError = exports.InvalidSubscriptionStateError = exports.InvalidSubscriptionRequestError = exports.SubscriptionNotFoundError = exports.SubscriptionError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base class for every error this interface raises.
|
|
6
|
+
*
|
|
7
|
+
* A failed *renewal* is not an error: it is reported as a `past_due` status
|
|
8
|
+
* and a `subscription.payment_failed` event. These errors mean the call itself
|
|
9
|
+
* did not happen.
|
|
10
|
+
*/
|
|
11
|
+
class SubscriptionError extends Error {
|
|
12
|
+
providerCode;
|
|
13
|
+
constructor(message,
|
|
14
|
+
/** The provider's own error code, verbatim and opaque. */
|
|
15
|
+
providerCode) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.providerCode = providerCode;
|
|
18
|
+
this.name = "SubscriptionError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.SubscriptionError = SubscriptionError;
|
|
22
|
+
/**
|
|
23
|
+
* No subscription exists under that identifier, for that provider.
|
|
24
|
+
*
|
|
25
|
+
* Also raised for an identifier that cannot be parsed at all.
|
|
26
|
+
*/
|
|
27
|
+
class SubscriptionNotFoundError extends SubscriptionError {
|
|
28
|
+
constructor(id, providerCode) {
|
|
29
|
+
super(`No such subscription: ${id}`, providerCode);
|
|
30
|
+
this.name = "SubscriptionNotFoundError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.SubscriptionNotFoundError = SubscriptionNotFoundError;
|
|
34
|
+
/**
|
|
35
|
+
* The request is malformed, or asks for something outside the portable core:
|
|
36
|
+
* a non-positive amount, an interval count below 1, a trial end in the past,
|
|
37
|
+
* both a trial and a billing anchor, a period longer than the provider allows.
|
|
38
|
+
*/
|
|
39
|
+
class InvalidSubscriptionRequestError extends SubscriptionError {
|
|
40
|
+
constructor(message, providerCode) {
|
|
41
|
+
super(message, providerCode);
|
|
42
|
+
this.name = "InvalidSubscriptionRequestError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.InvalidSubscriptionRequestError = InvalidSubscriptionRequestError;
|
|
46
|
+
/**
|
|
47
|
+
* The request was well-formed but does not apply to the subscription's current
|
|
48
|
+
* state: cancelling one that has already ended, changing the payment method of
|
|
49
|
+
* one that has ended.
|
|
50
|
+
*/
|
|
51
|
+
class InvalidSubscriptionStateError extends SubscriptionError {
|
|
52
|
+
status;
|
|
53
|
+
constructor(message,
|
|
54
|
+
/** The status the subscription was actually in. */
|
|
55
|
+
status, providerCode) {
|
|
56
|
+
super(message, providerCode);
|
|
57
|
+
this.status = status;
|
|
58
|
+
this.name = "InvalidSubscriptionStateError";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.InvalidSubscriptionStateError = InvalidSubscriptionStateError;
|
|
62
|
+
/**
|
|
63
|
+
* The first period could not be charged, so no subscription was created.
|
|
64
|
+
*
|
|
65
|
+
* Only {@link CreateSubscription} raises this, and only when it charges at
|
|
66
|
+
* once — no trial, no future billing anchor. Later charges that fail are a
|
|
67
|
+
* `past_due` status, not an error.
|
|
68
|
+
*/
|
|
69
|
+
class SubscriptionPaymentError extends SubscriptionError {
|
|
70
|
+
payment;
|
|
71
|
+
constructor(
|
|
72
|
+
/**
|
|
73
|
+
* The declined payment, as the payment interface reports it: `failed`, or
|
|
74
|
+
* `canceled` where the provider voids the declined attempt when it refuses
|
|
75
|
+
* the subscription (Stripe does). `GetPayment` on its id agrees. The
|
|
76
|
+
* decline reason is in `failure` when `failed`, and in `providerCode`.
|
|
77
|
+
*/
|
|
78
|
+
payment, providerCode) {
|
|
79
|
+
super(`The first period could not be charged: payment ${payment.id} is ${payment.status}`, providerCode);
|
|
80
|
+
this.payment = payment;
|
|
81
|
+
this.name = "SubscriptionPaymentError";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.SubscriptionPaymentError = SubscriptionPaymentError;
|
|
85
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAIA;;;;;;GAMG;AACH,MAAa,iBAAkB,SAAQ,KAAK;IAIxB;IAHlB,YACE,OAAe;IACf,0DAA0D;IAC1C,YAAqB;QAErC,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,iBAAY,GAAZ,YAAY,CAAS;QAGrC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AATD,8CASC;AAED;;;;GAIG;AACH,MAAa,yBAA0B,SAAQ,iBAAiB;IAC9D,YAAY,EAAU,EAAE,YAAqB;QAC3C,KAAK,CAAC,yBAAyB,EAAE,EAAE,EAAE,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF;AALD,8DAKC;AAED;;;;GAIG;AACH,MAAa,+BAAgC,SAAQ,iBAAiB;IACpE,YAAY,OAAe,EAAE,YAAqB;QAChD,KAAK,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,iCAAiC,CAAC;IAChD,CAAC;CACF;AALD,0EAKC;AAED;;;;GAIG;AACH,MAAa,6BAA8B,SAAQ,iBAAiB;IAIhD;IAHlB,YACE,OAAe;IACf,mDAAmD;IACnC,MAA2B,EAC3C,YAAqB;QAErB,KAAK,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAHb,WAAM,GAAN,MAAM,CAAqB;QAI3C,IAAI,CAAC,IAAI,GAAG,+BAA+B,CAAC;IAC9C,CAAC;CACF;AAVD,sEAUC;AAED;;;;;;GAMG;AACH,MAAa,wBAAyB,SAAQ,iBAAiB;IAQ3C;IAPlB;IACE;;;;;OAKG;IACa,OAAgB,EAChC,YAAqB;QAErB,KAAK,CACH,kDAAkD,OAAO,CAAC,EAAE,OAAO,OAAO,CAAC,MAAM,EAAE,EACnF,YAAY,CACb,CAAC;QANc,YAAO,GAAP,OAAO,CAAS;QAOhC,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AAjBD,4DAiBC"}
|