@ultimabase/stripe 0.0.0-first.0 → 0.0.0-stage
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 +2 -6
- package/package.json +3 -48
- package/dist/accounts.d.ts +0 -9
- package/dist/accounts.d.ts.map +0 -1
- package/dist/accounts.js +0 -20
- package/dist/apply.d.ts +0 -26
- package/dist/apply.d.ts.map +0 -1
- package/dist/apply.js +0 -73
- package/dist/audit.d.ts +0 -3
- package/dist/audit.d.ts.map +0 -1
- package/dist/audit.js +0 -123
- package/dist/billing-gateway.d.ts +0 -158
- package/dist/billing-gateway.d.ts.map +0 -1
- package/dist/billing-gateway.js +0 -23
- package/dist/billing.d.ts +0 -130
- package/dist/billing.d.ts.map +0 -1
- package/dist/billing.js +0 -128
- package/dist/calls.d.ts +0 -61
- package/dist/calls.d.ts.map +0 -1
- package/dist/calls.js +0 -172
- package/dist/catalog.d.ts +0 -54
- package/dist/catalog.d.ts.map +0 -1
- package/dist/catalog.js +0 -182
- package/dist/credit.d.ts +0 -16
- package/dist/credit.d.ts.map +0 -1
- package/dist/credit.js +0 -18
- package/dist/credits.d.ts +0 -38
- package/dist/credits.d.ts.map +0 -1
- package/dist/credits.js +0 -138
- package/dist/customer.d.ts +0 -25
- package/dist/customer.d.ts.map +0 -1
- package/dist/customer.js +0 -88
- package/dist/describe.d.ts +0 -4
- package/dist/describe.d.ts.map +0 -1
- package/dist/describe.js +0 -56
- package/dist/duplicates.d.ts +0 -12
- package/dist/duplicates.d.ts.map +0 -1
- package/dist/duplicates.js +0 -24
- package/dist/fake-billing.d.ts +0 -72
- package/dist/fake-billing.d.ts.map +0 -1
- package/dist/fake-billing.js +0 -290
- package/dist/gateway.d.ts +0 -108
- package/dist/gateway.d.ts.map +0 -1
- package/dist/gateway.js +0 -196
- package/dist/index.d.ts +0 -15
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -6
- package/dist/job-input.d.ts +0 -6
- package/dist/job-input.d.ts.map +0 -1
- package/dist/job-input.js +0 -20
- package/dist/jobs.d.ts +0 -16
- package/dist/jobs.d.ts.map +0 -1
- package/dist/jobs.js +0 -189
- package/dist/keys.d.ts +0 -10
- package/dist/keys.d.ts.map +0 -1
- package/dist/keys.js +0 -30
- package/dist/kinds.d.ts +0 -11
- package/dist/kinds.d.ts.map +0 -1
- package/dist/kinds.js +0 -4
- package/dist/models.d.ts +0 -71
- package/dist/models.d.ts.map +0 -1
- package/dist/models.js +0 -41
- package/dist/plugin.d.ts +0 -41
- package/dist/plugin.d.ts.map +0 -1
- package/dist/plugin.js +0 -229
- package/dist/portal.d.ts +0 -23
- package/dist/portal.d.ts.map +0 -1
- package/dist/portal.js +0 -66
- package/dist/product.d.ts +0 -57
- package/dist/product.d.ts.map +0 -1
- package/dist/product.js +0 -113
- package/dist/running.d.ts +0 -61
- package/dist/running.d.ts.map +0 -1
- package/dist/running.js +0 -35
- package/dist/sdk-billing.d.ts +0 -8
- package/dist/sdk-billing.d.ts.map +0 -1
- package/dist/sdk-billing.js +0 -200
- package/dist/sdk-common.d.ts +0 -13
- package/dist/sdk-common.d.ts.map +0 -1
- package/dist/sdk-common.js +0 -41
- package/dist/sdk-customer.d.ts +0 -9
- package/dist/sdk-customer.d.ts.map +0 -1
- package/dist/sdk-customer.js +0 -149
- package/dist/sdk-invoices.d.ts +0 -10
- package/dist/sdk-invoices.d.ts.map +0 -1
- package/dist/sdk-invoices.js +0 -53
- package/dist/sdk-recovery.d.ts +0 -5
- package/dist/sdk-recovery.d.ts.map +0 -1
- package/dist/sdk-recovery.js +0 -81
- package/dist/sync.d.ts +0 -8
- package/dist/sync.d.ts.map +0 -1
- package/dist/sync.js +0 -196
- package/dist/testing.d.ts +0 -24
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -100
- package/dist/webhook.d.ts +0 -4
- package/dist/webhook.d.ts.map +0 -1
- package/dist/webhook.js +0 -73
- package/src/accounts.ts +0 -30
- package/src/apply.ts +0 -99
- package/src/audit.ts +0 -171
- package/src/billing-gateway.ts +0 -188
- package/src/billing.ts +0 -361
- package/src/calls.ts +0 -280
- package/src/catalog.ts +0 -327
- package/src/credit.ts +0 -36
- package/src/credits.ts +0 -206
- package/src/customer.ts +0 -113
- package/src/describe.ts +0 -76
- package/src/duplicates.ts +0 -45
- package/src/fake-billing.ts +0 -388
- package/src/gateway.ts +0 -333
- package/src/index.ts +0 -35
- package/src/job-input.ts +0 -24
- package/src/jobs.ts +0 -230
- package/src/keys.ts +0 -40
- package/src/kinds.ts +0 -15
- package/src/models.ts +0 -107
- package/src/plugin.ts +0 -307
- package/src/portal.ts +0 -95
- package/src/product.ts +0 -203
- package/src/running.ts +0 -104
- package/src/sdk-billing.ts +0 -220
- package/src/sdk-common.ts +0 -57
- package/src/sdk-customer.ts +0 -203
- package/src/sdk-invoices.ts +0 -81
- package/src/sdk-recovery.ts +0 -93
- package/src/sync.ts +0 -246
- package/src/testing.ts +0 -135
- package/src/webhook.ts +0 -100
package/dist/fake-billing.d.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import type { BillingGateway, CheckoutWrite, RecoveryGateway, CustomerProfile, PortalWrite, StripeInvoice, StripePayment, StripeSubscription } from './billing-gateway.ts';
|
|
2
|
-
import type { StripePrice } from './gateway.ts';
|
|
3
|
-
/** A customer of the fake account. */
|
|
4
|
-
export interface FakeCustomer {
|
|
5
|
-
readonly id: string;
|
|
6
|
-
readonly profile: CustomerProfile;
|
|
7
|
-
readonly deleted: boolean;
|
|
8
|
-
/** Its default card: the one a renewal charges (D-380). */
|
|
9
|
-
readonly card: string | null;
|
|
10
|
-
}
|
|
11
|
-
/** A Checkout of the fake account. */
|
|
12
|
-
export interface FakeCheckout extends CheckoutWrite {
|
|
13
|
-
readonly id: string;
|
|
14
|
-
readonly url: string;
|
|
15
|
-
readonly status: 'open' | 'complete' | 'expired';
|
|
16
|
-
}
|
|
17
|
-
/** What the fake account holds of its payers, and what a test does at Stripe. */
|
|
18
|
-
export interface FakeBilling {
|
|
19
|
-
readonly webhookSecret: string;
|
|
20
|
-
readonly customers: Map<string, FakeCustomer>;
|
|
21
|
-
readonly checkouts: Map<string, FakeCheckout>;
|
|
22
|
-
readonly subscriptions: Map<string, StripeSubscription>;
|
|
23
|
-
readonly payments: Map<string, StripePayment>;
|
|
24
|
-
/** The paid invoices of subscriptions, each period's. */
|
|
25
|
-
readonly invoices: Map<string, StripeInvoice>;
|
|
26
|
-
/** Each portal session opened, in order. */
|
|
27
|
-
readonly portalSessions: PortalWrite[];
|
|
28
|
-
/** The subscriptions whose latest payment the plugin refunded. */
|
|
29
|
-
readonly subscriptionRefunds: Set<string>;
|
|
30
|
-
/**
|
|
31
|
-
* Pays an open Checkout, by its URL, as its payer would; gives its id.
|
|
32
|
-
* `free`: a promotion code took the whole price, and Stripe makes no
|
|
33
|
-
* payment for a purchase.
|
|
34
|
-
*/
|
|
35
|
-
readonly pay: (url: string, options?: {
|
|
36
|
-
readonly free?: boolean;
|
|
37
|
-
}) => string;
|
|
38
|
-
/** Changes a subscription, as the portal or a renewal would. */
|
|
39
|
-
readonly update: (id: string, changes: Partial<Omit<StripeSubscription, 'id'>>) => void;
|
|
40
|
-
/** Pays a subscription's next period, as Stripe renews it; gives its invoice. */
|
|
41
|
-
readonly renew: (id: string) => string;
|
|
42
|
-
/** Refunds a payment or an invoice, all of it by default. */
|
|
43
|
-
readonly refund: (id: string, amount?: number) => void;
|
|
44
|
-
readonly loseDispute: (id: string) => void;
|
|
45
|
-
/** Deletes a customer, as the dashboard would. */
|
|
46
|
-
readonly deleteCustomer: (id: string) => void;
|
|
47
|
-
/** Posts each event sent since the last call to the application at `url`. */
|
|
48
|
-
readonly deliver: (url: string, path?: string) => Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
/** A thin event's body, as Stripe sends it to an event destination. */
|
|
51
|
-
export declare const thinEvent: (event: {
|
|
52
|
-
readonly id?: string;
|
|
53
|
-
/** Its v1 type: `customer.subscription.updated`. */
|
|
54
|
-
readonly type: string;
|
|
55
|
-
/** Its related object: `{ id: 'sub_1', type: 'subscription' }`. */
|
|
56
|
-
readonly object: {
|
|
57
|
-
readonly id: string;
|
|
58
|
-
readonly type: string;
|
|
59
|
-
};
|
|
60
|
-
readonly livemode?: boolean;
|
|
61
|
-
}) => string;
|
|
62
|
-
/** Posts `payload` to the webhook of the application at `url`, signed. */
|
|
63
|
-
export declare const postEvent: (url: string, payload: string, options: {
|
|
64
|
-
readonly secret: string;
|
|
65
|
-
readonly path?: string | undefined;
|
|
66
|
-
}) => Promise<Response>;
|
|
67
|
-
/** The fake's billing, over its catalog's prices. */
|
|
68
|
-
export declare function fakeBilling(prices: ReadonlyMap<string, StripePrice>, newId: (prefix: string) => string): {
|
|
69
|
-
readonly gateway: BillingGateway & RecoveryGateway;
|
|
70
|
-
readonly fake: FakeBilling;
|
|
71
|
-
};
|
|
72
|
-
//# sourceMappingURL=fake-billing.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fake-billing.d.ts","sourceRoot":"","sources":["../src/fake-billing.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,cAAc,EACd,aAAa,EACb,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE/C,sCAAsC;AACtC,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,OAAO,EAAE,eAAe,CAAA;IACjC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAC5B;AAED,sCAAsC;AACtC,MAAM,WAAW,YAAa,SAAQ,aAAa;IAClD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACn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290 +0,0 @@
|
|
|
1
|
-
// The fake account's payers (D-371 point 8): customers, Checkouts a test
|
|
2
|
-
// pays, subscriptions and payments, and the thin events each change sends
|
|
3
|
-
// to the webhook, signed as Stripe signs them.
|
|
4
|
-
import { Stripe } from 'stripe';
|
|
5
|
-
import { Missing } from './billing-gateway.js';
|
|
6
|
-
/** A thin event's body, as Stripe sends it to an event destination. */
|
|
7
|
-
export const thinEvent = (event) => JSON.stringify({
|
|
8
|
-
id: event.id ?? `evt_${crypto.randomUUID().replaceAll('-', '')}`,
|
|
9
|
-
object: 'v2.core.event',
|
|
10
|
-
type: `v1.${event.type}`,
|
|
11
|
-
livemode: event.livemode ?? false,
|
|
12
|
-
created: new Date().toISOString(),
|
|
13
|
-
related_object: { ...event.object, url: `/v1/${event.object.id}` },
|
|
14
|
-
});
|
|
15
|
-
/** Posts `payload` to the webhook of the application at `url`, signed. */
|
|
16
|
-
export const postEvent = async (url, payload, options) => fetch(`${url}${options.path ?? '/webhooks/stripe'}`, {
|
|
17
|
-
method: 'POST',
|
|
18
|
-
headers: {
|
|
19
|
-
'content-type': 'application/json',
|
|
20
|
-
'stripe-signature': Stripe.webhooks.generateTestHeaderString({
|
|
21
|
-
payload,
|
|
22
|
-
secret: options.secret,
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
body: payload,
|
|
26
|
-
});
|
|
27
|
-
const found = (map, id) => {
|
|
28
|
-
const record = map.get(id);
|
|
29
|
-
if (record === undefined)
|
|
30
|
-
throw new Missing(id);
|
|
31
|
-
return record;
|
|
32
|
-
};
|
|
33
|
-
const day = 24 * 60 * 60 * 1000;
|
|
34
|
-
const days = {
|
|
35
|
-
day: 1,
|
|
36
|
-
week: 7,
|
|
37
|
-
month: 30,
|
|
38
|
-
year: 365,
|
|
39
|
-
};
|
|
40
|
-
const periodOf = ({ every, everyCount }) => (days[every] ?? 30) * everyCount * day;
|
|
41
|
-
/** The fake's billing, over its catalog's prices. */
|
|
42
|
-
export function fakeBilling(prices, newId) {
|
|
43
|
-
const customers = new Map();
|
|
44
|
-
const keys = new Map();
|
|
45
|
-
const checkouts = new Map();
|
|
46
|
-
const subscriptions = new Map();
|
|
47
|
-
const payments = new Map();
|
|
48
|
-
const invoices = new Map();
|
|
49
|
-
const portalSessions = [];
|
|
50
|
-
const subscriptionRefunds = new Set();
|
|
51
|
-
const sent = [];
|
|
52
|
-
/** Each event's object and time, which Stripe lists for 30 days. */
|
|
53
|
-
const listed = [];
|
|
54
|
-
const send = (type, id, object) => {
|
|
55
|
-
sent.push(thinEvent({ type, object: { id, type: object } }));
|
|
56
|
-
listed.push({ id, type: object, at: new Date() });
|
|
57
|
-
};
|
|
58
|
-
const ownerOf = (type, id) => {
|
|
59
|
-
const maps = {
|
|
60
|
-
'checkout.session': checkouts,
|
|
61
|
-
subscription: subscriptions,
|
|
62
|
-
payment_intent: payments,
|
|
63
|
-
invoice: invoices,
|
|
64
|
-
};
|
|
65
|
-
if (type === 'customer')
|
|
66
|
-
return customers.has(id) ? id : null;
|
|
67
|
-
return maps[type]?.get(id)?.customerId ?? null;
|
|
68
|
-
};
|
|
69
|
-
const priceOf = (lookupKey) => {
|
|
70
|
-
const held = [...prices.values()].find((one) => one.active && one.lookupKey === lookupKey);
|
|
71
|
-
if (held === undefined)
|
|
72
|
-
throw new Error(`No price is ${lookupKey}.`);
|
|
73
|
-
return held;
|
|
74
|
-
};
|
|
75
|
-
const live = (id) => {
|
|
76
|
-
const customer = found(customers, id);
|
|
77
|
-
if (customer.deleted)
|
|
78
|
-
throw new Missing(id);
|
|
79
|
-
return customer;
|
|
80
|
-
};
|
|
81
|
-
/** Bills a subscription's period, which ends at `end`. */
|
|
82
|
-
const bill = (subscription, end) => {
|
|
83
|
-
const id = newId('in');
|
|
84
|
-
const { product, price, quantity, amount, customerId } = subscription;
|
|
85
|
-
const periods = [{ product, price, quantity, end }];
|
|
86
|
-
const refunds = { refunded: 0, disputeLost: false };
|
|
87
|
-
invoices.set(id, { id, customerId, periods, amount, ...refunds });
|
|
88
|
-
send('invoice.paid', id, 'invoice');
|
|
89
|
-
return id;
|
|
90
|
-
};
|
|
91
|
-
const subscribe = (checkout, price) => {
|
|
92
|
-
const id = newId('sub');
|
|
93
|
-
const now = Date.now();
|
|
94
|
-
const { every = 'month', count = 1 } = price.recurring ?? {};
|
|
95
|
-
const trialEnd = checkout.trialDays === null
|
|
96
|
-
? null
|
|
97
|
-
: new Date(now + checkout.trialDays * day);
|
|
98
|
-
const [product = '', name = ''] = checkout.lookupKey.split('.');
|
|
99
|
-
subscriptions.set(id, {
|
|
100
|
-
id,
|
|
101
|
-
customerId: checkout.customerId,
|
|
102
|
-
product,
|
|
103
|
-
price: name,
|
|
104
|
-
status: trialEnd === null ? 'active' : 'trialing',
|
|
105
|
-
quantity: checkout.quantity,
|
|
106
|
-
amount: (price.amount ?? 0) * checkout.quantity,
|
|
107
|
-
every,
|
|
108
|
-
everyCount: count,
|
|
109
|
-
currentPeriodEnd: trialEnd ?? new Date(now + (days[every] ?? 30) * count * day),
|
|
110
|
-
cancelAt: null,
|
|
111
|
-
canceledAt: null,
|
|
112
|
-
trialEnd,
|
|
113
|
-
startedAt: new Date(now),
|
|
114
|
-
scheduled: null,
|
|
115
|
-
card: newId('pm'),
|
|
116
|
-
});
|
|
117
|
-
send('customer.subscription.created', id, 'subscription');
|
|
118
|
-
const created = found(subscriptions, id);
|
|
119
|
-
if (trialEnd === null)
|
|
120
|
-
bill(created, created.currentPeriodEnd);
|
|
121
|
-
return id;
|
|
122
|
-
};
|
|
123
|
-
const charge = (checkout, price, free) => {
|
|
124
|
-
// A free Checkout stands for its payment, which Stripe does not make.
|
|
125
|
-
const id = free ? checkout.id : newId('pi');
|
|
126
|
-
payments.set(id, {
|
|
127
|
-
id,
|
|
128
|
-
customerId: checkout.customerId,
|
|
129
|
-
product: checkout.product,
|
|
130
|
-
quantity: checkout.quantity,
|
|
131
|
-
amount: free ? 0 : (price.amount ?? 0) * checkout.quantity,
|
|
132
|
-
refunded: 0,
|
|
133
|
-
disputeLost: false,
|
|
134
|
-
paidAt: new Date(),
|
|
135
|
-
receiptUrl: free ? null : `https://pay.stripe.test/receipts/${id}`,
|
|
136
|
-
});
|
|
137
|
-
if (!free)
|
|
138
|
-
send('payment_intent.succeeded', id, 'payment_intent');
|
|
139
|
-
return id;
|
|
140
|
-
};
|
|
141
|
-
const gateway = {
|
|
142
|
-
createCustomer: async (profile, idempotencyKey) => {
|
|
143
|
-
const held = keys.get(idempotencyKey);
|
|
144
|
-
if (held !== undefined)
|
|
145
|
-
return held;
|
|
146
|
-
const id = newId('cus');
|
|
147
|
-
customers.set(id, { id, profile, deleted: false, card: null });
|
|
148
|
-
keys.set(idempotencyKey, id);
|
|
149
|
-
return id;
|
|
150
|
-
},
|
|
151
|
-
updateCustomer: async (id, profile) => {
|
|
152
|
-
customers.set(id, { ...live(id), profile });
|
|
153
|
-
},
|
|
154
|
-
deleteCustomer: async (id) => {
|
|
155
|
-
fake.deleteCustomer(id);
|
|
156
|
-
},
|
|
157
|
-
readCustomer: async (id, options) => {
|
|
158
|
-
const customer = customers.get(id);
|
|
159
|
-
if (customer === undefined || customer.deleted) {
|
|
160
|
-
return { deleted: true, subscriptions: [], payments: [], invoices: [] };
|
|
161
|
-
}
|
|
162
|
-
const own = (map) => [...map.values()].filter((one) => one.customerId === id);
|
|
163
|
-
return {
|
|
164
|
-
deleted: false,
|
|
165
|
-
subscriptions: own(subscriptions),
|
|
166
|
-
// Stripe lists a free Checkout only when asked to.
|
|
167
|
-
payments: own(payments).filter((one) => options?.free === true || !one.id.startsWith('cs_')),
|
|
168
|
-
invoices: options?.invoices === true ? own(invoices) : [],
|
|
169
|
-
};
|
|
170
|
-
},
|
|
171
|
-
customerOf: async (type, id) => ownerOf(type, id),
|
|
172
|
-
openCheckout: async (checkout) => {
|
|
173
|
-
live(checkout.customerId);
|
|
174
|
-
priceOf(checkout.lookupKey);
|
|
175
|
-
const id = newId('cs');
|
|
176
|
-
const url = `https://checkout.stripe.test/${id}`;
|
|
177
|
-
checkouts.set(id, { ...checkout, id, url, status: 'open' });
|
|
178
|
-
return url;
|
|
179
|
-
},
|
|
180
|
-
expireCheckouts: async (customerId) => {
|
|
181
|
-
live(customerId);
|
|
182
|
-
for (const checkout of checkouts.values()) {
|
|
183
|
-
if (checkout.customerId === customerId && checkout.status === 'open') {
|
|
184
|
-
checkouts.set(checkout.id, { ...checkout, status: 'expired' });
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
checkoutCustomer: async (id) => checkouts.get(id)?.customerId ?? null,
|
|
189
|
-
openPortal: async (portal) => {
|
|
190
|
-
live(portal.customerId);
|
|
191
|
-
portalSessions.push(portal);
|
|
192
|
-
return `https://billing.stripe.test/${newId('bps')}`;
|
|
193
|
-
},
|
|
194
|
-
moveCard: async ({ id, customerId, card }) => {
|
|
195
|
-
customers.set(customerId, { ...live(customerId), card });
|
|
196
|
-
fake.update(id, { card: null });
|
|
197
|
-
},
|
|
198
|
-
cancelAndRefund: async (id) => {
|
|
199
|
-
const held = found(subscriptions, id);
|
|
200
|
-
if (held.status !== 'canceled') {
|
|
201
|
-
const ended = { status: 'canceled', canceledAt: new Date() };
|
|
202
|
-
subscriptions.set(id, { ...held, ...ended });
|
|
203
|
-
send('customer.subscription.deleted', id, 'subscription');
|
|
204
|
-
}
|
|
205
|
-
subscriptionRefunds.add(id);
|
|
206
|
-
},
|
|
207
|
-
changedSince: async (since) => {
|
|
208
|
-
const changed = new Map();
|
|
209
|
-
for (const { id, type, at } of listed) {
|
|
210
|
-
const customer = ownerOf(type, id);
|
|
211
|
-
if (customer !== null && at > since)
|
|
212
|
-
changed.set(customer, at);
|
|
213
|
-
}
|
|
214
|
-
return changed;
|
|
215
|
-
},
|
|
216
|
-
};
|
|
217
|
-
const fake = {
|
|
218
|
-
webhookSecret: `whsec_fake_${crypto.randomUUID()}`,
|
|
219
|
-
customers,
|
|
220
|
-
checkouts,
|
|
221
|
-
subscriptions,
|
|
222
|
-
payments,
|
|
223
|
-
invoices,
|
|
224
|
-
portalSessions,
|
|
225
|
-
subscriptionRefunds,
|
|
226
|
-
pay: (url, options) => {
|
|
227
|
-
const checkout = [...checkouts.values()].find((one) => one.url === url);
|
|
228
|
-
if (checkout?.status !== 'open')
|
|
229
|
-
throw new Error(`${url} is not open.`);
|
|
230
|
-
const price = priceOf(checkout.lookupKey);
|
|
231
|
-
checkouts.set(checkout.id, { ...checkout, status: 'complete' });
|
|
232
|
-
const id = checkout.mode === 'subscription'
|
|
233
|
-
? subscribe(checkout, price)
|
|
234
|
-
: charge(checkout, price, options?.free ?? false);
|
|
235
|
-
send('checkout.session.completed', checkout.id, 'checkout.session');
|
|
236
|
-
return id;
|
|
237
|
-
},
|
|
238
|
-
update: (id, changes) => {
|
|
239
|
-
subscriptions.set(id, { ...found(subscriptions, id), ...changes });
|
|
240
|
-
send('customer.subscription.updated', id, 'subscription');
|
|
241
|
-
},
|
|
242
|
-
renew: (id) => {
|
|
243
|
-
const held = found(subscriptions, id);
|
|
244
|
-
const start = Math.max(held.currentPeriodEnd.getTime(), Date.now());
|
|
245
|
-
const end = new Date(start + periodOf(held));
|
|
246
|
-
const renewed = { ...held, status: 'active', currentPeriodEnd: end };
|
|
247
|
-
subscriptions.set(id, renewed);
|
|
248
|
-
send('customer.subscription.updated', id, 'subscription');
|
|
249
|
-
return bill(renewed, end);
|
|
250
|
-
},
|
|
251
|
-
refund: (id, amount) => {
|
|
252
|
-
const map = invoices.has(id)
|
|
253
|
-
? invoices
|
|
254
|
-
: payments;
|
|
255
|
-
const paid = found(map, id);
|
|
256
|
-
const refunded = paid.refunded + (amount ?? paid.amount);
|
|
257
|
-
map.set(id, { ...paid, refunded });
|
|
258
|
-
send('charge.refunded', id, invoices.has(id) ? 'invoice' : 'payment_intent');
|
|
259
|
-
},
|
|
260
|
-
loseDispute: (id) => {
|
|
261
|
-
const map = invoices.has(id)
|
|
262
|
-
? invoices
|
|
263
|
-
: payments;
|
|
264
|
-
map.set(id, { ...found(map, id), disputeLost: true });
|
|
265
|
-
send('charge.dispute.closed', id, invoices.has(id) ? 'invoice' : 'payment_intent');
|
|
266
|
-
},
|
|
267
|
-
deleteCustomer: (id) => {
|
|
268
|
-
const customer = customers.get(id);
|
|
269
|
-
if (customer === undefined || customer.deleted)
|
|
270
|
-
return;
|
|
271
|
-
customers.set(id, { ...customer, deleted: true });
|
|
272
|
-
for (const one of subscriptions.values()) {
|
|
273
|
-
if (one.customerId === id && one.status !== 'canceled') {
|
|
274
|
-
subscriptions.set(one.id, { ...one, status: 'canceled' });
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
send('customer.deleted', id, 'customer');
|
|
278
|
-
},
|
|
279
|
-
deliver: async (url, path) => {
|
|
280
|
-
for (const payload of sent.splice(0)) {
|
|
281
|
-
const secret = fake.webhookSecret;
|
|
282
|
-
const response = await postEvent(url, payload, { secret, path });
|
|
283
|
-
if (!response.ok) {
|
|
284
|
-
throw new Error(`The webhook answered ${String(response.status)}.`);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
},
|
|
288
|
-
};
|
|
289
|
-
return { gateway, fake };
|
|
290
|
-
}
|
package/dist/gateway.d.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { Stripe } from 'stripe';
|
|
2
|
-
import type { Every } from './product.ts';
|
|
3
|
-
/** How often a price bills: Stripe's `interval` and `interval_count`. */
|
|
4
|
-
export interface Recurring<E extends string = string> {
|
|
5
|
-
readonly every: E;
|
|
6
|
-
readonly count: number;
|
|
7
|
-
}
|
|
8
|
-
/** A product the catalog manages, as Stripe holds it. */
|
|
9
|
-
export interface StripeProduct {
|
|
10
|
-
readonly id: string;
|
|
11
|
-
/** Its `metadata.key`. */
|
|
12
|
-
readonly key: string | null;
|
|
13
|
-
readonly name: string;
|
|
14
|
-
readonly active: boolean;
|
|
15
|
-
readonly taxCode: string | null;
|
|
16
|
-
}
|
|
17
|
-
/** A price, as Stripe holds it. */
|
|
18
|
-
export interface StripePrice {
|
|
19
|
-
readonly id: string;
|
|
20
|
-
readonly productId: string;
|
|
21
|
-
readonly lookupKey: string | null;
|
|
22
|
-
readonly amount: number | null;
|
|
23
|
-
readonly currency: string;
|
|
24
|
-
/** `null` for a one-time price. */
|
|
25
|
-
readonly recurring: Recurring | null;
|
|
26
|
-
readonly taxBehavior: string | null;
|
|
27
|
-
readonly nickname: string | null;
|
|
28
|
-
readonly active: boolean;
|
|
29
|
-
readonly metadata: Readonly<Record<string, string>>;
|
|
30
|
-
}
|
|
31
|
-
/** A product the portal lets a subscription move to, with its prices. */
|
|
32
|
-
export interface PortalProduct {
|
|
33
|
-
readonly productId: string;
|
|
34
|
-
readonly priceIds: readonly string[];
|
|
35
|
-
}
|
|
36
|
-
/** The customer portal's configuration, as the catalog writes it. */
|
|
37
|
-
export interface PortalSettings {
|
|
38
|
-
readonly returnUrl: string | null;
|
|
39
|
-
readonly termsUrl: string | null;
|
|
40
|
-
readonly privacyUrl: string | null;
|
|
41
|
-
/** `always_invoice` or `create_prorations`. */
|
|
42
|
-
readonly prorationBehavior: string;
|
|
43
|
-
/** Whether a downgrade waits for the period's end. */
|
|
44
|
-
readonly scheduleAtPeriodEnd: readonly string[];
|
|
45
|
-
/** `end_trial` or `continue_trial`. */
|
|
46
|
-
readonly trialUpdate: string;
|
|
47
|
-
/** `at_period_end` or `immediately`. */
|
|
48
|
-
readonly cancelMode: string;
|
|
49
|
-
/** `null` when Stripe did not list them. */
|
|
50
|
-
readonly products: readonly PortalProduct[] | null;
|
|
51
|
-
readonly active: boolean;
|
|
52
|
-
}
|
|
53
|
-
/** What Stripe holds of the catalog. */
|
|
54
|
-
export interface CatalogState {
|
|
55
|
-
/** The managed products, archived ones included. */
|
|
56
|
-
readonly products: readonly StripeProduct[];
|
|
57
|
-
/** Every price, archived ones included. */
|
|
58
|
-
readonly prices: readonly StripePrice[];
|
|
59
|
-
/** The managed portal configuration, if one was saved. */
|
|
60
|
-
readonly portal: {
|
|
61
|
-
readonly id: string;
|
|
62
|
-
readonly settings: PortalSettings;
|
|
63
|
-
} | null;
|
|
64
|
-
}
|
|
65
|
-
/** A product as the catalog writes it. */
|
|
66
|
-
export interface ProductWrite {
|
|
67
|
-
readonly key: string;
|
|
68
|
-
readonly name: string;
|
|
69
|
-
readonly taxCode: string | null;
|
|
70
|
-
}
|
|
71
|
-
/** A price as the catalog writes it: Stripe never changes its billing. */
|
|
72
|
-
export interface PriceWrite {
|
|
73
|
-
readonly productId: string;
|
|
74
|
-
readonly lookupKey: string;
|
|
75
|
-
readonly amount: number;
|
|
76
|
-
readonly currency: string;
|
|
77
|
-
/** `null` for a one-time price. */
|
|
78
|
-
readonly recurring: Recurring<Every> | null;
|
|
79
|
-
readonly taxBehavior: 'inclusive' | 'exclusive';
|
|
80
|
-
readonly nickname: string;
|
|
81
|
-
readonly metadata: Readonly<Record<string, string>>;
|
|
82
|
-
}
|
|
83
|
-
/** What the plugin asks Stripe. */
|
|
84
|
-
export interface StripeGateway {
|
|
85
|
-
/** The managed products, every price, and the managed portal. */
|
|
86
|
-
readonly readCatalog: () => Promise<CatalogState>;
|
|
87
|
-
/** Creates a managed product; returns its id. */
|
|
88
|
-
readonly createProduct: (product: ProductWrite) => Promise<string>;
|
|
89
|
-
/** Renames a product, sets its tax code, and makes it active. */
|
|
90
|
-
readonly updateProduct: (id: string, product: ProductWrite) => Promise<void>;
|
|
91
|
-
readonly archiveProduct: (id: string) => Promise<void>;
|
|
92
|
-
/** Creates a price, which takes its lookup key over; returns its id. */
|
|
93
|
-
readonly createPrice: (price: PriceWrite) => Promise<string>;
|
|
94
|
-
/** Sets a price's nickname and metadata. */
|
|
95
|
-
readonly updatePrice: (id: string, price: Pick<PriceWrite, 'nickname' | 'metadata'>) => Promise<void>;
|
|
96
|
-
readonly archivePrice: (id: string) => Promise<void>;
|
|
97
|
-
/** Creates the portal configuration, or updates `id`; returns its id. */
|
|
98
|
-
readonly savePortal: (id: string | null, settings: PortalSettings) => Promise<string>;
|
|
99
|
-
}
|
|
100
|
-
/** `metadata.managedBy` of what the catalog writes: nothing else is touched. */
|
|
101
|
-
export declare const managedBy = "ultimabase";
|
|
102
|
-
/** The Stripe client of a secret key. */
|
|
103
|
-
export declare const stripeClient: (secret: string) => Stripe;
|
|
104
|
-
/** The configuration's parameters, from `settings`. */
|
|
105
|
-
export declare const portalParams: (settings: PortalSettings) => Stripe.BillingPortal.ConfigurationCreateParams;
|
|
106
|
-
/** The gateway over Stripe's API. */
|
|
107
|
-
export declare const sdkGateway: (client: Stripe) => StripeGateway;
|
|
108
|
-
//# sourceMappingURL=gateway.d.ts.map
|
package/dist/gateway.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"gateway.d.ts","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAGzC,yEAAyE;AACzE,MAAM,WAAW,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACnD,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CACtB;AAED,yDAAyD;AACzD,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,0BAA0B;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAC/B;AAED,mCAAmC;AACnC,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAA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AAA;IAClE,iEAAiE;IACjE,QAAQ,CAAC,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5E,QAAQ,CAAC,cAAc,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACtD,wEAAwE;IACxE,QAAQ,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAC5D,4CAA4C;IAC5C,QAAQ,CAAC,WAAW,EAAE,CACrB,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,CAAC,KAC5C,OAAO,CAAC,IAAI,CAAC,CAAA;IAClB,QAAQ,CAAC,YAAY,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACpD,yEAAyE;IACzE,QAAQ,CAAC,UAAU,EAAE,CACpB,EAAE,EAAE,MAAM,GAAG,IAAI,EACjB,QAAQ,EAAE,cAAc,KACpB,OAAO,CAAC,MAAM,CAAC,CAAA;CACpB;AAED,gFAAgF;AAChF,eAAO,MAAM,SAAS,eAAe,CAAA;AAKrC,yCAAyC;AACzC,eAAO,MAAM,YAAY,WAAY,MAAM,KAAG,MAM3C,CAAA;AA+CH,uDAAuD;AACvD,eAAO,MAAM,YAAY,aACd,cAAc,KACtB,MAAM,CAAC,aAAa,CAAC,yBAsCvB,CAAA;AAED,qCAAqC;AACrC,eAAO,MAAM,UAAU,WAAY,MAAM,KAAG,aA6G1C,CAAA"}
|
package/dist/gateway.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
// Stripe, behind the narrow gateway the plugin calls. `sdkGateway` reaches
|
|
2
|
-
// Stripe through its SDK; `@ultimabase/stripe/testing` holds a fake in
|
|
3
|
-
// memory, and no test of `yarn validate` reaches Stripe.
|
|
4
|
-
import { Stripe } from 'stripe';
|
|
5
|
-
import { all } from './sdk-common.js';
|
|
6
|
-
/** `metadata.managedBy` of what the catalog writes: nothing else is touched. */
|
|
7
|
-
export const managedBy = 'ultimabase';
|
|
8
|
-
/** The API version the plugin reads Stripe's objects with: the SDK's. */
|
|
9
|
-
const apiVersion = '2026-10-28.endive';
|
|
10
|
-
/** The Stripe client of a secret key. */
|
|
11
|
-
export const stripeClient = (secret) => new Stripe(secret, {
|
|
12
|
-
apiVersion,
|
|
13
|
-
maxNetworkRetries: 2,
|
|
14
|
-
// A request past 10 s fails, and its caller retries.
|
|
15
|
-
timeout: 10_000,
|
|
16
|
-
});
|
|
17
|
-
const idOf = (value) => typeof value === 'string' ? value : value.id;
|
|
18
|
-
const priceOf = (price) => ({
|
|
19
|
-
id: price.id,
|
|
20
|
-
productId: idOf(price.product),
|
|
21
|
-
lookupKey: price.lookup_key,
|
|
22
|
-
amount: price.unit_amount,
|
|
23
|
-
currency: price.currency,
|
|
24
|
-
recurring: price.recurring === null
|
|
25
|
-
? null
|
|
26
|
-
: {
|
|
27
|
-
every: price.recurring.interval,
|
|
28
|
-
count: price.recurring.interval_count,
|
|
29
|
-
},
|
|
30
|
-
taxBehavior: price.tax_behavior,
|
|
31
|
-
nickname: price.nickname,
|
|
32
|
-
active: price.active,
|
|
33
|
-
metadata: price.metadata,
|
|
34
|
-
});
|
|
35
|
-
const portalOf = (portal) => {
|
|
36
|
-
const update = portal.features.subscription_update;
|
|
37
|
-
return {
|
|
38
|
-
returnUrl: portal.default_return_url,
|
|
39
|
-
termsUrl: portal.business_profile.terms_of_service_url,
|
|
40
|
-
privacyUrl: portal.business_profile.privacy_policy_url,
|
|
41
|
-
prorationBehavior: update.proration_behavior,
|
|
42
|
-
scheduleAtPeriodEnd: update.schedule_at_period_end.conditions.map(({ type }) => type),
|
|
43
|
-
trialUpdate: update.trial_update_behavior,
|
|
44
|
-
cancelMode: portal.features.subscription_cancel.mode,
|
|
45
|
-
products: update.products?.map(({ product, prices }) => ({
|
|
46
|
-
productId: product,
|
|
47
|
-
priceIds: prices,
|
|
48
|
-
})) ?? null,
|
|
49
|
-
active: portal.active,
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
/** The configuration's parameters, from `settings`. */
|
|
53
|
-
export const portalParams = (settings) => {
|
|
54
|
-
const products = settings.products ?? [];
|
|
55
|
-
return {
|
|
56
|
-
business_profile: {
|
|
57
|
-
...(settings.termsUrl !== null && {
|
|
58
|
-
terms_of_service_url: settings.termsUrl,
|
|
59
|
-
}),
|
|
60
|
-
...(settings.privacyUrl !== null && {
|
|
61
|
-
privacy_policy_url: settings.privacyUrl,
|
|
62
|
-
}),
|
|
63
|
-
},
|
|
64
|
-
...(settings.returnUrl !== null && {
|
|
65
|
-
default_return_url: settings.returnUrl,
|
|
66
|
-
}),
|
|
67
|
-
features: {
|
|
68
|
-
subscription_update: {
|
|
69
|
-
// The portal refuses an update with no product to move to.
|
|
70
|
-
enabled: products.length > 0,
|
|
71
|
-
default_allowed_updates: ['price'],
|
|
72
|
-
products: products.map(({ productId, priceIds }) => ({
|
|
73
|
-
product: productId,
|
|
74
|
-
prices: [...priceIds],
|
|
75
|
-
})),
|
|
76
|
-
proration_behavior: settings.prorationBehavior,
|
|
77
|
-
trial_update_behavior: settings.trialUpdate,
|
|
78
|
-
schedule_at_period_end: {
|
|
79
|
-
conditions: settings.scheduleAtPeriodEnd.map((type) => ({ type })),
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
subscription_cancel: {
|
|
83
|
-
enabled: true,
|
|
84
|
-
mode: settings.cancelMode,
|
|
85
|
-
},
|
|
86
|
-
payment_method_update: { enabled: true },
|
|
87
|
-
invoice_history: { enabled: true },
|
|
88
|
-
},
|
|
89
|
-
metadata: { managedBy },
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
/** The gateway over Stripe's API. */
|
|
93
|
-
export const sdkGateway = (client) => ({
|
|
94
|
-
readCatalog: async () => {
|
|
95
|
-
// Three lists, whatever the catalog's size.
|
|
96
|
-
const [products, prices, portals] = await Promise.all([
|
|
97
|
-
all(client.products.list({ limit: 100 })),
|
|
98
|
-
all(client.prices.list({ limit: 100 })),
|
|
99
|
-
all(client.billingPortal.configurations.list({
|
|
100
|
-
limit: 100,
|
|
101
|
-
expand: ['data.features.subscription_update.products'],
|
|
102
|
-
})),
|
|
103
|
-
]);
|
|
104
|
-
const portal = portals.find(({ metadata }) => metadata?.['managedBy'] === managedBy);
|
|
105
|
-
return {
|
|
106
|
-
products: products
|
|
107
|
-
.filter(({ metadata }) => metadata['managedBy'] === managedBy)
|
|
108
|
-
.map((product) => ({
|
|
109
|
-
id: product.id,
|
|
110
|
-
key: product.metadata['key'] ?? null,
|
|
111
|
-
name: product.name,
|
|
112
|
-
active: product.active,
|
|
113
|
-
taxCode: product.tax_code === null || product.tax_code === undefined
|
|
114
|
-
? null
|
|
115
|
-
: idOf(product.tax_code),
|
|
116
|
-
})),
|
|
117
|
-
prices: prices.map(priceOf),
|
|
118
|
-
portal: portal === undefined
|
|
119
|
-
? null
|
|
120
|
-
: { id: portal.id, settings: portalOf(portal) },
|
|
121
|
-
};
|
|
122
|
-
},
|
|
123
|
-
createProduct: async ({ key, name, taxCode }) => {
|
|
124
|
-
const created = await client.products.create({
|
|
125
|
-
name,
|
|
126
|
-
...(taxCode !== null && { tax_code: taxCode }),
|
|
127
|
-
metadata: { managedBy, key },
|
|
128
|
-
});
|
|
129
|
-
return created.id;
|
|
130
|
-
},
|
|
131
|
-
updateProduct: async (id, { key, name, taxCode }) => {
|
|
132
|
-
await client.products.update(id, {
|
|
133
|
-
name,
|
|
134
|
-
active: true,
|
|
135
|
-
// An empty string unsets it.
|
|
136
|
-
tax_code: taxCode ?? '',
|
|
137
|
-
metadata: { managedBy, key },
|
|
138
|
-
});
|
|
139
|
-
},
|
|
140
|
-
archiveProduct: async (id) => {
|
|
141
|
-
await client.products.update(id, { active: false });
|
|
142
|
-
},
|
|
143
|
-
createPrice: async (price) => {
|
|
144
|
-
const created = await client.prices.create({
|
|
145
|
-
product: price.productId,
|
|
146
|
-
currency: price.currency,
|
|
147
|
-
unit_amount: price.amount,
|
|
148
|
-
...(price.recurring !== null && {
|
|
149
|
-
recurring: {
|
|
150
|
-
interval: price.recurring.every,
|
|
151
|
-
interval_count: price.recurring.count,
|
|
152
|
-
},
|
|
153
|
-
}),
|
|
154
|
-
tax_behavior: price.taxBehavior,
|
|
155
|
-
lookup_key: price.lookupKey,
|
|
156
|
-
transfer_lookup_key: true,
|
|
157
|
-
nickname: price.nickname,
|
|
158
|
-
metadata: { ...price.metadata },
|
|
159
|
-
});
|
|
160
|
-
return created.id;
|
|
161
|
-
},
|
|
162
|
-
updatePrice: async (id, { nickname, metadata }) => {
|
|
163
|
-
await client.prices.update(id, { nickname, metadata: { ...metadata } });
|
|
164
|
-
},
|
|
165
|
-
archivePrice: async (id) => {
|
|
166
|
-
await client.prices.update(id, { active: false });
|
|
167
|
-
},
|
|
168
|
-
savePortal: async (id, settings) => {
|
|
169
|
-
const params = portalParams(settings);
|
|
170
|
-
if (id === null) {
|
|
171
|
-
return (await client.billingPortal.configurations.create(params)).id;
|
|
172
|
-
}
|
|
173
|
-
// Stripe drops an empty list or an absent key, keeping the old value:
|
|
174
|
-
// an update clears a setting with an empty string.
|
|
175
|
-
const { subscription_update: update } = params.features;
|
|
176
|
-
const conditions = update?.schedule_at_period_end?.conditions ?? [];
|
|
177
|
-
await client.billingPortal.configurations.update(id, {
|
|
178
|
-
...params,
|
|
179
|
-
business_profile: {
|
|
180
|
-
terms_of_service_url: settings.termsUrl ?? '',
|
|
181
|
-
privacy_policy_url: settings.privacyUrl ?? '',
|
|
182
|
-
},
|
|
183
|
-
features: {
|
|
184
|
-
...params.features,
|
|
185
|
-
subscription_update: {
|
|
186
|
-
...update,
|
|
187
|
-
schedule_at_period_end: {
|
|
188
|
-
conditions: conditions.length > 0 ? conditions : '',
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
active: true,
|
|
193
|
-
});
|
|
194
|
-
return id;
|
|
195
|
-
},
|
|
196
|
-
});
|