@base44-preview/sdk 0.8.48-pr.145.de7eff3 → 0.8.48-pr.276.4f760f9
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/dist/iap/account-token.d.ts +14 -0
- package/dist/iap/account-token.js +43 -0
- package/dist/iap/config.d.ts +18 -0
- package/dist/iap/config.js +99 -0
- package/dist/iap/errors.d.ts +89 -0
- package/dist/iap/errors.js +66 -0
- package/dist/iap/errors.types.d.ts +74 -0
- package/dist/iap/errors.types.js +7 -0
- package/dist/iap/events/emitter.d.ts +28 -0
- package/dist/iap/events/emitter.js +33 -0
- package/dist/iap/events/events.types.d.ts +124 -0
- package/dist/iap/events/events.types.js +1 -0
- package/dist/iap/iap.types.d.ts +618 -0
- package/dist/iap/iap.types.js +1 -0
- package/dist/iap/index.d.ts +120 -0
- package/dist/iap/index.js +140 -0
- package/dist/iap/ingest/device.d.ts +7 -0
- package/dist/iap/ingest/device.js +191 -0
- package/dist/iap/ingest/device.types.d.ts +80 -0
- package/dist/iap/ingest/device.types.js +1 -0
- package/dist/iap/ingest/mappers.d.ts +66 -0
- package/dist/iap/ingest/mappers.js +248 -0
- package/dist/iap/ingest/matrix.d.ts +55 -0
- package/dist/iap/ingest/matrix.js +202 -0
- package/dist/iap/ingest/notifications.d.ts +70 -0
- package/dist/iap/ingest/notifications.js +238 -0
- package/dist/iap/read/derive.d.ts +40 -0
- package/dist/iap/read/derive.js +112 -0
- package/dist/iap/read/read.d.ts +47 -0
- package/dist/iap/read/read.js +215 -0
- package/dist/iap/read/read.types.d.ts +174 -0
- package/dist/iap/read/read.types.js +1 -0
- package/dist/iap/runtime/base64.d.ts +34 -0
- package/dist/iap/runtime/base64.js +131 -0
- package/dist/iap/runtime/clock.d.ts +13 -0
- package/dist/iap/runtime/clock.js +11 -0
- package/dist/iap/runtime/webcrypto.d.ts +14 -0
- package/dist/iap/runtime/webcrypto.js +66 -0
- package/dist/iap/server-api/client.d.ts +16 -0
- package/dist/iap/server-api/client.js +140 -0
- package/dist/iap/server-api/jwt.d.ts +3 -0
- package/dist/iap/server-api/jwt.js +70 -0
- package/dist/iap/server-api/server-api.types.d.ts +155 -0
- package/dist/iap/server-api/server-api.types.js +12 -0
- package/dist/iap/store/collapse.d.ts +27 -0
- package/dist/iap/store/collapse.js +85 -0
- package/dist/iap/store/descriptors.d.ts +46 -0
- package/dist/iap/store/descriptors.js +90 -0
- package/dist/iap/store/entities-store.d.ts +43 -0
- package/dist/iap/store/entities-store.js +348 -0
- package/dist/iap/store/rows.types.d.ts +233 -0
- package/dist/iap/store/rows.types.js +1 -0
- package/dist/iap/store/schemas.d.ts +68 -0
- package/dist/iap/store/schemas.js +214 -0
- package/dist/iap/store/store-errors.d.ts +52 -0
- package/dist/iap/store/store-errors.js +103 -0
- package/dist/iap/store/store.types.d.ts +193 -0
- package/dist/iap/store/store.types.js +1 -0
- package/dist/iap/verify/apple-roots.d.ts +18 -0
- package/dist/iap/verify/apple-roots.js +121 -0
- package/dist/iap/verify/payload-checks.d.ts +30 -0
- package/dist/iap/verify/payload-checks.js +20 -0
- package/dist/iap/verify/verifier.d.ts +36 -0
- package/dist/iap/verify/verifier.js +161 -0
- package/dist/iap/verify/verify.types.d.ts +292 -0
- package/dist/iap/verify/verify.types.js +15 -0
- package/dist/iap/version.d.ts +13 -0
- package/dist/iap/version.js +13 -0
- package/dist/index.d.ts +11 -0
- package/dist/modules/functions.js +2 -6
- package/package.json +33 -3
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turning a decoded Apple payload into the rows the store writes.
|
|
3
|
+
*
|
|
4
|
+
* Two things worth knowing before reading further.
|
|
5
|
+
*
|
|
6
|
+
* **The account-token mapping is one-way.** A Base44 user id hashes to a UUID,
|
|
7
|
+
* and Apple signs that UUID into every transaction — but a hash cannot be
|
|
8
|
+
* inverted, so a token does not yield the user back. That has a concrete
|
|
9
|
+
* consequence per path:
|
|
10
|
+
*
|
|
11
|
+
* - `recordTransaction` and `syncEntitlements` run inside an authenticated
|
|
12
|
+
* request, so the user is already known. The token is used to *check* that
|
|
13
|
+
* claim, by re-deriving it and comparing.
|
|
14
|
+
* - A webhook has no user. It inherits `appUserId` from a row already stored
|
|
15
|
+
* for the same subscription, and leaves it null when there is none — which
|
|
16
|
+
* is the honest answer for a purchase made before the customer logged in.
|
|
17
|
+
*
|
|
18
|
+
* **A merge omits what it does not know.** Every patch here drops nullish
|
|
19
|
+
* values, so a bare device transaction cannot erase renewal information that a
|
|
20
|
+
* notification supplied. Clearing a field is always explicit.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
import { appAccountTokenFor } from "../account-token.js";
|
|
25
|
+
import { IapVerificationError } from "../errors.js";
|
|
26
|
+
import { IAP_MODULE_VERSION } from "../version.js";
|
|
27
|
+
const DAY_MS = 86400000;
|
|
28
|
+
/** Apple's window for consumption data: 12 hours live, 5 minutes in sandbox. */
|
|
29
|
+
export function consumptionDeadline(receivedAt, environment) {
|
|
30
|
+
return environment === "Production"
|
|
31
|
+
? receivedAt + 12 * 60 * 60 * 1000
|
|
32
|
+
: receivedAt + 5 * 60 * 1000;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Confirms a claimed user matches the UUID Apple signed into the transaction.
|
|
36
|
+
*
|
|
37
|
+
* @throws {IapVerificationError} when the token belongs to a different user.
|
|
38
|
+
*/
|
|
39
|
+
export function assertUserMatchesToken(decoded, claimedUserId) {
|
|
40
|
+
const token = decoded.appAccountToken;
|
|
41
|
+
if (typeof token !== "string" || token.length === 0)
|
|
42
|
+
return;
|
|
43
|
+
const expected = appAccountTokenFor(claimedUserId);
|
|
44
|
+
if (token.toLowerCase() !== expected.toLowerCase()) {
|
|
45
|
+
throw new IapVerificationError("INVALID_APP_IDENTIFIER", "this transaction's account token belongs to a different user than the one " +
|
|
46
|
+
"making the request, so it will not be attributed to them");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* When a non-renewing subscription runs out.
|
|
51
|
+
*
|
|
52
|
+
* Apple never expires these — its own documentation says the app is
|
|
53
|
+
* responsible — so the configured duration is the only thing that decides.
|
|
54
|
+
*/
|
|
55
|
+
function appDefinedExpiry(decoded, config) {
|
|
56
|
+
const productId = decoded.productId;
|
|
57
|
+
if (typeof productId !== "string")
|
|
58
|
+
return null;
|
|
59
|
+
const product = config.products[productId];
|
|
60
|
+
if (!product || product.type !== "nonRenewingSubscription")
|
|
61
|
+
return null;
|
|
62
|
+
const days = product.nonRenewingDurationDays;
|
|
63
|
+
const purchasedAt = decoded.purchaseDate;
|
|
64
|
+
if (typeof days !== "number" || typeof purchasedAt !== "number")
|
|
65
|
+
return null;
|
|
66
|
+
return purchasedAt + days * DAY_MS;
|
|
67
|
+
}
|
|
68
|
+
function pick(value) {
|
|
69
|
+
return value === undefined ? null : value;
|
|
70
|
+
}
|
|
71
|
+
/** Builds a complete transaction row, for an insert. */
|
|
72
|
+
export function transactionRowFrom(decoded, context) {
|
|
73
|
+
var _a, _b, _c, _d;
|
|
74
|
+
const environment = ((_a = decoded.environment) !== null && _a !== void 0 ? _a : "Production");
|
|
75
|
+
return {
|
|
76
|
+
transactionId: String(decoded.transactionId),
|
|
77
|
+
originalTransactionId: String((_b = decoded.originalTransactionId) !== null && _b !== void 0 ? _b : decoded.transactionId),
|
|
78
|
+
appUserId: context.appUserId,
|
|
79
|
+
appAccountToken: pick(decoded.appAccountToken),
|
|
80
|
+
productId: String((_c = decoded.productId) !== null && _c !== void 0 ? _c : ""),
|
|
81
|
+
type: pick(decoded.type),
|
|
82
|
+
subscriptionGroupIdentifier: pick(decoded.subscriptionGroupIdentifier),
|
|
83
|
+
purchaseDate: pick(decoded.purchaseDate),
|
|
84
|
+
originalPurchaseDate: pick(decoded.originalPurchaseDate),
|
|
85
|
+
expiresDate: pick(decoded.expiresDate),
|
|
86
|
+
appDefinedExpiresDate: appDefinedExpiry(decoded, context.config),
|
|
87
|
+
quantity: pick(decoded.quantity),
|
|
88
|
+
inAppOwnershipType: pick(decoded.inAppOwnershipType),
|
|
89
|
+
transactionReason: pick(decoded.transactionReason),
|
|
90
|
+
isUpgraded: pick(decoded.isUpgraded),
|
|
91
|
+
offerType: pick(decoded.offerType),
|
|
92
|
+
offerIdentifier: pick(decoded.offerIdentifier),
|
|
93
|
+
offerDiscountType: pick(decoded.offerDiscountType),
|
|
94
|
+
offerPeriod: pick(decoded.offerPeriod),
|
|
95
|
+
revocationDate: pick(decoded.revocationDate),
|
|
96
|
+
revocationReason: pick(decoded.revocationReason),
|
|
97
|
+
revocationType: pick(decoded.revocationType),
|
|
98
|
+
revocationPercentage: pick(decoded.revocationPercentage),
|
|
99
|
+
environment,
|
|
100
|
+
storefront: pick(decoded.storefront),
|
|
101
|
+
storefrontId: pick(decoded.storefrontId),
|
|
102
|
+
signedDate: Number((_d = decoded.signedDate) !== null && _d !== void 0 ? _d : context.now),
|
|
103
|
+
rawJws: context.rawJws,
|
|
104
|
+
source: context.source,
|
|
105
|
+
finishedAt: null,
|
|
106
|
+
recordedAt: context.now,
|
|
107
|
+
updatedAt: context.now,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** Builds the merge for an existing transaction row. */
|
|
111
|
+
export function transactionPatchFrom(row, options = {}) {
|
|
112
|
+
const { transactionId, recordedAt, finishedAt, ...rest } = row;
|
|
113
|
+
void transactionId;
|
|
114
|
+
void recordedAt;
|
|
115
|
+
void finishedAt;
|
|
116
|
+
if (options.clearRevocation) {
|
|
117
|
+
// Apple omits revocationPercentage entirely when a refund is reversed, so
|
|
118
|
+
// omitting nullish values is not enough — the stale revocation would
|
|
119
|
+
// survive and keep a paying customer locked out. It has to be cleared.
|
|
120
|
+
const { revocationDate, revocationReason, revocationType, revocationPercentage, ...withoutRevocation } = rest;
|
|
121
|
+
void revocationDate;
|
|
122
|
+
void revocationReason;
|
|
123
|
+
void revocationType;
|
|
124
|
+
void revocationPercentage;
|
|
125
|
+
return {
|
|
126
|
+
set: withoutRevocation,
|
|
127
|
+
clear: [
|
|
128
|
+
"revocationDate",
|
|
129
|
+
"revocationReason",
|
|
130
|
+
"revocationType",
|
|
131
|
+
"revocationPercentage",
|
|
132
|
+
],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return { set: rest };
|
|
136
|
+
}
|
|
137
|
+
/** Builds a complete subscription row, for an insert. */
|
|
138
|
+
export function subscriptionRowFrom(context) {
|
|
139
|
+
var _a, _b, _c, _d, _e;
|
|
140
|
+
const { transaction, renewalInfo } = context;
|
|
141
|
+
return {
|
|
142
|
+
originalTransactionId: String((_a = transaction.originalTransactionId) !== null && _a !== void 0 ? _a : transaction.transactionId),
|
|
143
|
+
appUserId: context.appUserId,
|
|
144
|
+
subscriptionGroupIdentifier: pick(transaction.subscriptionGroupIdentifier),
|
|
145
|
+
productId: pick(transaction.productId),
|
|
146
|
+
latestTransactionJws: context.transactionJws,
|
|
147
|
+
latestRenewalInfoJws: (_b = context.renewalInfoJws) !== null && _b !== void 0 ? _b : null,
|
|
148
|
+
latestSignedDate: Number((_c = transaction.signedDate) !== null && _c !== void 0 ? _c : context.now),
|
|
149
|
+
latestRenewalSignedDate: renewalInfo && typeof renewalInfo.signedDate === "number"
|
|
150
|
+
? renewalInfo.signedDate
|
|
151
|
+
: null,
|
|
152
|
+
appleStatus: (_d = context.appleStatus) !== null && _d !== void 0 ? _d : null,
|
|
153
|
+
environment: ((_e = transaction.environment) !== null && _e !== void 0 ? _e : "Production"),
|
|
154
|
+
recordedAt: context.now,
|
|
155
|
+
updatedAt: context.now,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The transaction half of a subscription merge.
|
|
160
|
+
*
|
|
161
|
+
* Deliberately excludes the renewal columns. They have their own cursor,
|
|
162
|
+
* because a device sync brings a fresh transaction with no renewal
|
|
163
|
+
* information — and if one cursor covered both, that sync would advance past a
|
|
164
|
+
* later notification carrying a new grace-period date, denying service to a
|
|
165
|
+
* customer Apple is still trying to bill.
|
|
166
|
+
*/
|
|
167
|
+
export function subscriptionTransactionPatch(row) {
|
|
168
|
+
return {
|
|
169
|
+
set: {
|
|
170
|
+
subscriptionGroupIdentifier: row.subscriptionGroupIdentifier,
|
|
171
|
+
productId: row.productId,
|
|
172
|
+
latestTransactionJws: row.latestTransactionJws,
|
|
173
|
+
latestSignedDate: row.latestSignedDate,
|
|
174
|
+
appleStatus: row.appleStatus,
|
|
175
|
+
appUserId: row.appUserId,
|
|
176
|
+
environment: row.environment,
|
|
177
|
+
updatedAt: row.updatedAt,
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** The renewal half of a subscription merge, guarded by its own cursor. */
|
|
182
|
+
export function subscriptionRenewalPatch(row) {
|
|
183
|
+
return {
|
|
184
|
+
set: {
|
|
185
|
+
latestRenewalInfoJws: row.latestRenewalInfoJws,
|
|
186
|
+
latestRenewalSignedDate: row.latestRenewalSignedDate,
|
|
187
|
+
appleStatus: row.appleStatus,
|
|
188
|
+
updatedAt: row.updatedAt,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Builds a notification row, claimed but not yet applied.
|
|
194
|
+
*
|
|
195
|
+
* `outcome: "error"` is the commit flag. Duplicate detection only treats a row
|
|
196
|
+
* as already handled once the outcome is something else, so a delivery whose
|
|
197
|
+
* writes failed after this row landed is re-applied on Apple's retry rather
|
|
198
|
+
* than dismissed as a duplicate — which would lose it for good, because Apple
|
|
199
|
+
* stops retrying once it sees success.
|
|
200
|
+
*/
|
|
201
|
+
export function notificationRowFrom(context) {
|
|
202
|
+
var _a, _b, _c, _d, _e, _f;
|
|
203
|
+
const { notification } = context;
|
|
204
|
+
const transaction = (_a = notification.data) === null || _a === void 0 ? void 0 : _a.transactionInfo;
|
|
205
|
+
return {
|
|
206
|
+
notificationUUID: notification.notificationUUID,
|
|
207
|
+
notificationType: notification.notificationType,
|
|
208
|
+
subtype: pick(notification.subtype),
|
|
209
|
+
signedDate: Number((_b = notification.signedDate) !== null && _b !== void 0 ? _b : context.now),
|
|
210
|
+
receivedAt: context.now,
|
|
211
|
+
originalTransactionId: (transaction === null || transaction === void 0 ? void 0 : transaction.originalTransactionId)
|
|
212
|
+
? String(transaction.originalTransactionId)
|
|
213
|
+
: null,
|
|
214
|
+
transactionId: (transaction === null || transaction === void 0 ? void 0 : transaction.transactionId)
|
|
215
|
+
? String(transaction.transactionId)
|
|
216
|
+
: null,
|
|
217
|
+
environment: ((_f = (_d = (_c = notification.data) === null || _c === void 0 ? void 0 : _c.environment) !== null && _d !== void 0 ? _d : (_e = notification.summary) === null || _e === void 0 ? void 0 : _e.environment) !== null && _f !== void 0 ? _f : "Production"),
|
|
218
|
+
rawSignedPayload: context.rawSignedPayload,
|
|
219
|
+
outcome: "error",
|
|
220
|
+
attempts: 1,
|
|
221
|
+
sdkVersion: IAP_MODULE_VERSION,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/** Builds a consumption-request row from a `CONSUMPTION_REQUEST` notification. */
|
|
225
|
+
export function consumptionRowFrom(context) {
|
|
226
|
+
var _a, _b, _c, _d, _e;
|
|
227
|
+
const transaction = (_a = context.notification.data) === null || _a === void 0 ? void 0 : _a.transactionInfo;
|
|
228
|
+
if (!(transaction === null || transaction === void 0 ? void 0 : transaction.transactionId))
|
|
229
|
+
return null;
|
|
230
|
+
const environment = ((_c = (_b = context.notification.data) === null || _b === void 0 ? void 0 : _b.environment) !== null && _c !== void 0 ? _c : "Production");
|
|
231
|
+
return {
|
|
232
|
+
transactionId: String(transaction.transactionId),
|
|
233
|
+
originalTransactionId: transaction.originalTransactionId
|
|
234
|
+
? String(transaction.originalTransactionId)
|
|
235
|
+
: null,
|
|
236
|
+
appUserId: context.appUserId,
|
|
237
|
+
consumptionRequestReason: pick((_d = context.notification.data) === null || _d === void 0 ? void 0 : _d.consumptionRequestReason),
|
|
238
|
+
receivedAt: context.now,
|
|
239
|
+
deadlineAt: consumptionDeadline(context.now, environment),
|
|
240
|
+
requestSignedDate: Number((_e = context.notification.signedDate) !== null && _e !== void 0 ? _e : context.now),
|
|
241
|
+
respondedAt: null,
|
|
242
|
+
response: null,
|
|
243
|
+
outcome: null,
|
|
244
|
+
outcomeSignedDate: null,
|
|
245
|
+
environment,
|
|
246
|
+
updatedAt: context.now,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What to do with each of Apple's notification types.
|
|
3
|
+
*
|
|
4
|
+
* A pure table, mapping Apple's `notificationType` and `subtype` onto three
|
|
5
|
+
* things: which rows to touch, which event to emit, and what outcome to record.
|
|
6
|
+
* No I/O, so every row is testable on its own — and adding a type Apple
|
|
7
|
+
* invents later is a one-line change here rather than a new branch somewhere
|
|
8
|
+
* in the webhook.
|
|
9
|
+
*
|
|
10
|
+
* Two rules from Apple that the table encodes rather than leaves to a caller:
|
|
11
|
+
*
|
|
12
|
+
* - An unrecognised type still gets a `200`. Apple retries a failure for up to
|
|
13
|
+
* 72 hours, and retrying will not teach this version a type it does not
|
|
14
|
+
* know, so the payload is stored raw and the response is success.
|
|
15
|
+
* - A reversed refund must **clear** the revocation, not merely record a new
|
|
16
|
+
* event, because the app has to give the content back.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
import type { IapNotificationOutcome } from "../store/rows.types.js";
|
|
21
|
+
import type { IapEvent, IapEventType } from "../events/events.types.js";
|
|
22
|
+
/** What a notification does to stored state, and what it means to the app. */
|
|
23
|
+
export interface NotificationPlan {
|
|
24
|
+
/** The event to emit. */
|
|
25
|
+
readonly event: IapEventType;
|
|
26
|
+
/** What to record in the notification's own row. */
|
|
27
|
+
readonly outcome: Extract<IapNotificationOutcome, "applied" | "unhandled" | "unknown_type">;
|
|
28
|
+
/** Whether to store the transaction the payload carries. */
|
|
29
|
+
readonly storeTransaction: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Whether this concerns an auto-renewable subscription.
|
|
32
|
+
*
|
|
33
|
+
* The payload still decides: the subscription row is only touched when the
|
|
34
|
+
* transaction really is a subscription and carries the tokens for it.
|
|
35
|
+
*/
|
|
36
|
+
readonly touchesSubscription: boolean;
|
|
37
|
+
/** Whether to open a refund-consumption request, or to fill in its outcome. */
|
|
38
|
+
readonly consumption?: "open" | "resolve";
|
|
39
|
+
/** Whether to clear the revocation fields, so the app reinstates the content. */
|
|
40
|
+
readonly clearRevocation?: boolean;
|
|
41
|
+
/** Extra event detail this row implies. */
|
|
42
|
+
readonly detail?: Partial<IapEvent>;
|
|
43
|
+
}
|
|
44
|
+
/** A type Apple added after this version shipped. */
|
|
45
|
+
export declare const UNKNOWN_TYPE_PLAN: NotificationPlan;
|
|
46
|
+
/**
|
|
47
|
+
* Looks up what to do with a notification.
|
|
48
|
+
*
|
|
49
|
+
* Never throws and never returns nothing: a type this version has never seen
|
|
50
|
+
* resolves to {@link UNKNOWN_TYPE_PLAN}, which stores the payload and answers
|
|
51
|
+
* Apple successfully.
|
|
52
|
+
*/
|
|
53
|
+
export declare function planFor(notificationType: string, subtype?: string | null): NotificationPlan;
|
|
54
|
+
/** Every notification type this version recognises. For tests and diagnostics. */
|
|
55
|
+
export declare const KNOWN_NOTIFICATION_TYPES: readonly string[];
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
const subscriptionPlan = (event, detail) => ({
|
|
2
|
+
event,
|
|
3
|
+
outcome: "applied",
|
|
4
|
+
storeTransaction: true,
|
|
5
|
+
touchesSubscription: true,
|
|
6
|
+
detail,
|
|
7
|
+
});
|
|
8
|
+
const purchasePlan = (event, extra = {}) => ({
|
|
9
|
+
event,
|
|
10
|
+
outcome: "applied",
|
|
11
|
+
storeTransaction: true,
|
|
12
|
+
touchesSubscription: true,
|
|
13
|
+
...extra,
|
|
14
|
+
});
|
|
15
|
+
/** A type this version records but takes no action on. */
|
|
16
|
+
const unhandledPlan = {
|
|
17
|
+
event: "apple.unhandled",
|
|
18
|
+
outcome: "unhandled",
|
|
19
|
+
storeTransaction: false,
|
|
20
|
+
touchesSubscription: false,
|
|
21
|
+
};
|
|
22
|
+
/** A type Apple added after this version shipped. */
|
|
23
|
+
export const UNKNOWN_TYPE_PLAN = {
|
|
24
|
+
event: "apple.unknown",
|
|
25
|
+
outcome: "unknown_type",
|
|
26
|
+
storeTransaction: false,
|
|
27
|
+
touchesSubscription: false,
|
|
28
|
+
};
|
|
29
|
+
const TABLE = {
|
|
30
|
+
// ---- Subscription lifecycle ------------------------------------------
|
|
31
|
+
SUBSCRIBED: {
|
|
32
|
+
base: subscriptionPlan("subscription.started", { startReason: "initial" }),
|
|
33
|
+
subtypes: {
|
|
34
|
+
INITIAL_BUY: subscriptionPlan("subscription.started", {
|
|
35
|
+
startReason: "initial",
|
|
36
|
+
}),
|
|
37
|
+
RESUBSCRIBE: subscriptionPlan("subscription.started", {
|
|
38
|
+
startReason: "resubscribe",
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
DID_RENEW: {
|
|
43
|
+
base: subscriptionPlan("subscription.renewed", { renewReason: "renewal" }),
|
|
44
|
+
subtypes: {
|
|
45
|
+
BILLING_RECOVERY: subscriptionPlan("subscription.renewed", {
|
|
46
|
+
renewReason: "billing_recovery",
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
DID_CHANGE_RENEWAL_PREF: {
|
|
51
|
+
// No subtype means a scheduled change was reverted.
|
|
52
|
+
base: subscriptionPlan("subscription.plan_change_cancelled"),
|
|
53
|
+
subtypes: {
|
|
54
|
+
// Effective immediately, with a prorated refund of the old plan.
|
|
55
|
+
UPGRADE: subscriptionPlan("subscription.plan_changed"),
|
|
56
|
+
// Effective at the next renewal, so the current tier continues.
|
|
57
|
+
DOWNGRADE: subscriptionPlan("subscription.plan_change_scheduled"),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
DID_CHANGE_RENEWAL_STATUS: {
|
|
61
|
+
// No subtype means the customer cancelled after a price-increase notice.
|
|
62
|
+
base: subscriptionPlan("subscription.auto_renew_changed", {
|
|
63
|
+
autoRenewEnabled: false,
|
|
64
|
+
}),
|
|
65
|
+
subtypes: {
|
|
66
|
+
AUTO_RENEW_ENABLED: subscriptionPlan("subscription.auto_renew_changed", {
|
|
67
|
+
autoRenewEnabled: true,
|
|
68
|
+
}),
|
|
69
|
+
AUTO_RENEW_DISABLED: subscriptionPlan("subscription.auto_renew_changed", {
|
|
70
|
+
autoRenewEnabled: false,
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
DID_FAIL_TO_RENEW: {
|
|
75
|
+
// No subtype means no grace period: Apple retries for up to 60 days and
|
|
76
|
+
// the customer is not entitled meanwhile.
|
|
77
|
+
base: subscriptionPlan("subscription.billing_issue", { inGracePeriod: false }),
|
|
78
|
+
subtypes: {
|
|
79
|
+
// Apple's requirement here is explicit: keep providing full service for
|
|
80
|
+
// the whole grace period.
|
|
81
|
+
GRACE_PERIOD: subscriptionPlan("subscription.billing_issue", {
|
|
82
|
+
inGracePeriod: true,
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
GRACE_PERIOD_EXPIRED: {
|
|
87
|
+
base: subscriptionPlan("subscription.grace_period_ended"),
|
|
88
|
+
},
|
|
89
|
+
EXPIRED: {
|
|
90
|
+
base: subscriptionPlan("subscription.expired", { expiryReason: "other" }),
|
|
91
|
+
subtypes: {
|
|
92
|
+
VOLUNTARY: subscriptionPlan("subscription.expired", {
|
|
93
|
+
expiryReason: "voluntary",
|
|
94
|
+
}),
|
|
95
|
+
BILLING_RETRY: subscriptionPlan("subscription.expired", {
|
|
96
|
+
expiryReason: "billing",
|
|
97
|
+
}),
|
|
98
|
+
PRICE_INCREASE: subscriptionPlan("subscription.expired", {
|
|
99
|
+
expiryReason: "price_increase",
|
|
100
|
+
}),
|
|
101
|
+
PRODUCT_NOT_FOR_SALE: subscriptionPlan("subscription.expired", {
|
|
102
|
+
expiryReason: "product_unavailable",
|
|
103
|
+
}),
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
OFFER_REDEEMED: {
|
|
107
|
+
// The subtype refines which kind of change came with the offer; the offer
|
|
108
|
+
// fields on the transaction say the rest.
|
|
109
|
+
base: subscriptionPlan("subscription.offer_redeemed"),
|
|
110
|
+
},
|
|
111
|
+
PRICE_INCREASE: {
|
|
112
|
+
base: subscriptionPlan("subscription.price_increase", {
|
|
113
|
+
priceIncreaseConsent: "pending",
|
|
114
|
+
}),
|
|
115
|
+
subtypes: {
|
|
116
|
+
PENDING: subscriptionPlan("subscription.price_increase", {
|
|
117
|
+
priceIncreaseConsent: "pending",
|
|
118
|
+
}),
|
|
119
|
+
ACCEPTED: subscriptionPlan("subscription.price_increase", {
|
|
120
|
+
priceIncreaseConsent: "accepted",
|
|
121
|
+
}),
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
RENEWAL_EXTENDED: {
|
|
125
|
+
base: subscriptionPlan("subscription.renewal_extended"),
|
|
126
|
+
},
|
|
127
|
+
RENEWAL_EXTENSION: {
|
|
128
|
+
// A mass extension result. Concerns a product, not one customer, so there
|
|
129
|
+
// is no transaction to store.
|
|
130
|
+
base: {
|
|
131
|
+
event: "subscription.mass_extension_result",
|
|
132
|
+
outcome: "applied",
|
|
133
|
+
storeTransaction: false,
|
|
134
|
+
touchesSubscription: false,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
// ---- One-time purchases and refunds ----------------------------------
|
|
138
|
+
ONE_TIME_CHARGE: {
|
|
139
|
+
base: purchasePlan("purchase.completed", { touchesSubscription: false }),
|
|
140
|
+
},
|
|
141
|
+
REFUND: {
|
|
142
|
+
// The payload carries revocationDate, so storing the transaction is what
|
|
143
|
+
// withdraws entitlement. Also resolves any open consumption request.
|
|
144
|
+
base: purchasePlan("purchase.refunded", { consumption: "resolve" }),
|
|
145
|
+
},
|
|
146
|
+
REFUND_DECLINED: {
|
|
147
|
+
base: purchasePlan("purchase.refund_declined", { consumption: "resolve" }),
|
|
148
|
+
},
|
|
149
|
+
REFUND_REVERSED: {
|
|
150
|
+
// Apple's wording: "If your app revoked content or services because of the
|
|
151
|
+
// refund, it needs to reinstate them." Clearing the revocation is what
|
|
152
|
+
// makes the read layer say "entitled" again.
|
|
153
|
+
base: purchasePlan("purchase.refund_reversed", {
|
|
154
|
+
consumption: "resolve",
|
|
155
|
+
clearRevocation: true,
|
|
156
|
+
}),
|
|
157
|
+
},
|
|
158
|
+
REVOKE: {
|
|
159
|
+
base: purchasePlan("purchase.revoked"),
|
|
160
|
+
},
|
|
161
|
+
CONSUMPTION_REQUEST: {
|
|
162
|
+
// Apple's page lists this for consumables and subscriptions, its API page
|
|
163
|
+
// for any type, so every type is handled.
|
|
164
|
+
base: purchasePlan("refund.consumption_requested", { consumption: "open" }),
|
|
165
|
+
},
|
|
166
|
+
// ---- Housekeeping -----------------------------------------------------
|
|
167
|
+
TEST: {
|
|
168
|
+
base: {
|
|
169
|
+
event: "apple.test_received",
|
|
170
|
+
outcome: "applied",
|
|
171
|
+
storeTransaction: false,
|
|
172
|
+
touchesSubscription: false,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
// Programmes this SDK does not participate in. Stored raw, answered 200.
|
|
176
|
+
EXTERNAL_PURCHASE_TOKEN: { base: unhandledPlan },
|
|
177
|
+
METADATA_UPDATE: { base: unhandledPlan },
|
|
178
|
+
PRICE_CHANGE: { base: unhandledPlan },
|
|
179
|
+
RESCIND_CONSENT: { base: unhandledPlan },
|
|
180
|
+
// Apple's enumeration page spells this MIGRATION and its changelog MIGRATE.
|
|
181
|
+
// Both are accepted rather than guessing which is authoritative.
|
|
182
|
+
MIGRATION: { base: unhandledPlan },
|
|
183
|
+
MIGRATE: { base: unhandledPlan },
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Looks up what to do with a notification.
|
|
187
|
+
*
|
|
188
|
+
* Never throws and never returns nothing: a type this version has never seen
|
|
189
|
+
* resolves to {@link UNKNOWN_TYPE_PLAN}, which stores the payload and answers
|
|
190
|
+
* Apple successfully.
|
|
191
|
+
*/
|
|
192
|
+
export function planFor(notificationType, subtype) {
|
|
193
|
+
const entry = TABLE[notificationType];
|
|
194
|
+
if (!entry)
|
|
195
|
+
return UNKNOWN_TYPE_PLAN;
|
|
196
|
+
if (subtype && entry.subtypes && entry.subtypes[subtype]) {
|
|
197
|
+
return entry.subtypes[subtype];
|
|
198
|
+
}
|
|
199
|
+
return entry.base;
|
|
200
|
+
}
|
|
201
|
+
/** Every notification type this version recognises. For tests and diagnostics. */
|
|
202
|
+
export const KNOWN_NOTIFICATION_TYPES = Object.keys(TABLE);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The App Store Server Notifications webhook.
|
|
3
|
+
*
|
|
4
|
+
* The contract with Apple is unforgiving in one direction. Apple retries a
|
|
5
|
+
* failure five times over 72 hours, but it **never** retries a success — so a
|
|
6
|
+
* `200` returned before the data is stored loses that notification forever.
|
|
7
|
+
* Everything below is arranged around that single fact:
|
|
8
|
+
*
|
|
9
|
+
* ```
|
|
10
|
+
* parse malformed -> 400
|
|
11
|
+
* verify bad signature -> 401
|
|
12
|
+
* dedupe already applied -> 200
|
|
13
|
+
* claim write the raw payload, marked "not yet applied"
|
|
14
|
+
* apply entity writes
|
|
15
|
+
* commit mark the real outcome
|
|
16
|
+
* respond -> 200
|
|
17
|
+
* emit handlers, after the writes, before the response
|
|
18
|
+
* anything failed -> 503, so Apple comes back
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* The claim step is what makes a retry safe. If the raw row lands and the
|
|
22
|
+
* entity writes then fail, the row exists — and a naive duplicate check would
|
|
23
|
+
* tell Apple's retry "already seen" and drop the purchase data. So the row
|
|
24
|
+
* carries a commit flag, and a claimed-but-unapplied row is re-applied rather
|
|
25
|
+
* than dismissed.
|
|
26
|
+
*
|
|
27
|
+
* Sandbox has no retries at all, so a failure there is a real loss. The
|
|
28
|
+
* launch-time sync is what heals it.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
import type { ResolvedIapConfig } from "../config.js";
|
|
33
|
+
import type { Clock } from "../runtime/clock.js";
|
|
34
|
+
import type { IapEmitter } from "../events/emitter.js";
|
|
35
|
+
import type { IapEvent } from "../events/events.types.js";
|
|
36
|
+
import type { IapStore } from "../store/store.types.js";
|
|
37
|
+
import type { IapNotificationOutcome } from "../store/rows.types.js";
|
|
38
|
+
import type { Verifier } from "../verify/verifier.js";
|
|
39
|
+
/** What `handleNotification` did, for tests, replay and the owner panel. */
|
|
40
|
+
export interface HandleNotificationResult {
|
|
41
|
+
/** The HTTP status returned to Apple. */
|
|
42
|
+
readonly status: number;
|
|
43
|
+
/** What was recorded, when the payload was verified. */
|
|
44
|
+
readonly outcome?: IapNotificationOutcome;
|
|
45
|
+
/** Apple's notification id. */
|
|
46
|
+
readonly notificationUUID?: string;
|
|
47
|
+
/** Apple's notification type. */
|
|
48
|
+
readonly notificationType?: string;
|
|
49
|
+
/** The events emitted. */
|
|
50
|
+
readonly events: readonly IapEvent[];
|
|
51
|
+
/** Why the request was rejected, when it was. */
|
|
52
|
+
readonly error?: string;
|
|
53
|
+
}
|
|
54
|
+
/** What the ingestion paths share. */
|
|
55
|
+
export interface IngestContext {
|
|
56
|
+
readonly store: IapStore;
|
|
57
|
+
readonly verifier: Verifier;
|
|
58
|
+
readonly config: ResolvedIapConfig;
|
|
59
|
+
readonly clock: Clock;
|
|
60
|
+
readonly emitter: IapEmitter;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Handles one signed payload.
|
|
64
|
+
*
|
|
65
|
+
* Separate from the `Request` wrapper so a stored payload can be replayed, and
|
|
66
|
+
* so tests do not have to build an HTTP request.
|
|
67
|
+
*/
|
|
68
|
+
export declare function handleSignedPayload(context: IngestContext, signedPayload: string): Promise<HandleNotificationResult>;
|
|
69
|
+
/** Handles an incoming HTTP request from Apple. */
|
|
70
|
+
export declare function handleNotification(context: IngestContext, request: Request): Promise<Response>;
|