@phpsandbox/sdk 0.3.5 → 0.5.0
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 +37 -0
- package/dist/app.d.ts +21 -0
- package/dist/app.js +23 -0
- package/dist/browser/phpsandbox-sdk.esm.js +516 -30
- package/dist/browser/phpsandbox-sdk.esm.min.js +10 -10
- package/dist/browser/phpsandbox-sdk.iife.js +516 -30
- package/dist/browser/phpsandbox-sdk.iife.min.js +10 -10
- package/dist/customers.d.ts +241 -0
- package/dist/customers.js +141 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +20 -0
- package/dist/payments.d.ts +363 -0
- package/dist/payments.js +265 -0
- package/package.json +1 -1
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { Client } from './index.js';
|
|
3
|
+
export declare const DEFAULT_PAYMENT_CALLBACK_PATH = "/api/phpsandbox-payments/events";
|
|
4
|
+
export declare const paymentModeSchema: z.ZodEnum<{
|
|
5
|
+
test: "test";
|
|
6
|
+
live: "live";
|
|
7
|
+
}>;
|
|
8
|
+
export type PaymentMode = z.infer<typeof paymentModeSchema>;
|
|
9
|
+
declare const accountSchema: z.ZodObject<{
|
|
10
|
+
id: z.ZodString;
|
|
11
|
+
mode: z.ZodEnum<{
|
|
12
|
+
test: "test";
|
|
13
|
+
live: "live";
|
|
14
|
+
}>;
|
|
15
|
+
chargesEnabled: z.ZodBoolean;
|
|
16
|
+
payoutsEnabled: z.ZodBoolean;
|
|
17
|
+
detailsSubmitted: z.ZodBoolean;
|
|
18
|
+
requirements: z.ZodArray<z.ZodString>;
|
|
19
|
+
disabledReason: z.ZodNullable<z.ZodString>;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
export type PaymentAccountData = z.infer<typeof accountSchema>;
|
|
22
|
+
declare const accessSchema: z.ZodObject<{
|
|
23
|
+
subscribed: z.ZodBoolean;
|
|
24
|
+
liveAllowed: z.ZodBoolean;
|
|
25
|
+
testConfigured: z.ZodBoolean;
|
|
26
|
+
liveConfigured: z.ZodBoolean;
|
|
27
|
+
feeBasisPoints: z.ZodNullable<z.ZodNumber>;
|
|
28
|
+
canRead: z.ZodBoolean;
|
|
29
|
+
canManage: z.ZodBoolean;
|
|
30
|
+
canRefund: z.ZodBoolean;
|
|
31
|
+
recoveryEndsAt: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
export type PaymentsAccess = z.infer<typeof accessSchema>;
|
|
34
|
+
declare const applicationsSchema: z.ZodObject<{
|
|
35
|
+
notebookId: z.ZodString;
|
|
36
|
+
applications: z.ZodArray<z.ZodObject<{
|
|
37
|
+
id: z.ZodString;
|
|
38
|
+
url: z.ZodNullable<z.ZodString>;
|
|
39
|
+
healthy: z.ZodBoolean;
|
|
40
|
+
passwordProtected: z.ZodBoolean;
|
|
41
|
+
deployedAt: z.ZodNullable<z.ZodString>;
|
|
42
|
+
}, z.core.$strip>>;
|
|
43
|
+
}, z.core.$strip>;
|
|
44
|
+
export type PaymentApplicationsData = z.infer<typeof applicationsSchema>;
|
|
45
|
+
declare const bindingSchema: z.ZodObject<{
|
|
46
|
+
target: z.ZodOptional<z.ZodEnum<{
|
|
47
|
+
preview: "preview";
|
|
48
|
+
publication: "publication";
|
|
49
|
+
}>>;
|
|
50
|
+
id: z.ZodString;
|
|
51
|
+
notebookId: z.ZodString;
|
|
52
|
+
applicationId: z.ZodNullable<z.ZodString>;
|
|
53
|
+
mode: z.ZodEnum<{
|
|
54
|
+
test: "test";
|
|
55
|
+
live: "live";
|
|
56
|
+
}>;
|
|
57
|
+
revision: z.ZodNumber;
|
|
58
|
+
accountId: z.ZodString;
|
|
59
|
+
enabled: z.ZodBoolean;
|
|
60
|
+
callbackUrl: z.ZodNullable<z.ZodString>;
|
|
61
|
+
originUrl: z.ZodNullable<z.ZodString>;
|
|
62
|
+
runtimeConfigured: z.ZodBoolean;
|
|
63
|
+
}, z.core.$strip>;
|
|
64
|
+
export type PaymentBindingData = z.infer<typeof bindingSchema>;
|
|
65
|
+
declare const priceSchema: z.ZodObject<{
|
|
66
|
+
billingInterval: z.ZodDefault<z.ZodNullable<z.ZodEnum<{
|
|
67
|
+
month: "month";
|
|
68
|
+
year: "year";
|
|
69
|
+
}>>>;
|
|
70
|
+
id: z.ZodString;
|
|
71
|
+
name: z.ZodString;
|
|
72
|
+
amount: z.ZodNumber;
|
|
73
|
+
currency: z.ZodString;
|
|
74
|
+
bindingId: z.ZodString;
|
|
75
|
+
}, z.core.$strip>;
|
|
76
|
+
export type PaymentPriceData = z.infer<typeof priceSchema>;
|
|
77
|
+
declare const transactionSchema: z.ZodObject<{
|
|
78
|
+
subscriptionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
79
|
+
periodStart: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
80
|
+
periodEnd: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
81
|
+
id: z.ZodString;
|
|
82
|
+
bindingId: z.ZodString;
|
|
83
|
+
orderId: z.ZodString;
|
|
84
|
+
priceId: z.ZodString;
|
|
85
|
+
amount: z.ZodNumber;
|
|
86
|
+
currency: z.ZodString;
|
|
87
|
+
feeAmount: z.ZodNumber;
|
|
88
|
+
revision: z.ZodNumber;
|
|
89
|
+
refundedAmount: z.ZodNumber;
|
|
90
|
+
status: z.ZodEnum<{
|
|
91
|
+
pending: "pending";
|
|
92
|
+
failed: "failed";
|
|
93
|
+
paid: "paid";
|
|
94
|
+
refunded: "refunded";
|
|
95
|
+
partially_refunded: "partially_refunded";
|
|
96
|
+
}>;
|
|
97
|
+
checkoutUrl: z.ZodNullable<z.ZodString>;
|
|
98
|
+
deliveredAt: z.ZodNullable<z.ZodString>;
|
|
99
|
+
}, z.core.$strip>;
|
|
100
|
+
export type PaymentTransactionData = z.infer<typeof transactionSchema>;
|
|
101
|
+
declare const subscriptionSchema: z.ZodObject<{
|
|
102
|
+
id: z.ZodString;
|
|
103
|
+
bindingId: z.ZodString;
|
|
104
|
+
priceId: z.ZodString;
|
|
105
|
+
orderId: z.ZodString;
|
|
106
|
+
status: z.ZodEnum<{
|
|
107
|
+
pending: "pending";
|
|
108
|
+
incomplete: "incomplete";
|
|
109
|
+
incomplete_expired: "incomplete_expired";
|
|
110
|
+
trialing: "trialing";
|
|
111
|
+
active: "active";
|
|
112
|
+
past_due: "past_due";
|
|
113
|
+
canceled: "canceled";
|
|
114
|
+
unpaid: "unpaid";
|
|
115
|
+
paused: "paused";
|
|
116
|
+
}>;
|
|
117
|
+
billingInterval: z.ZodEnum<{
|
|
118
|
+
month: "month";
|
|
119
|
+
year: "year";
|
|
120
|
+
}>;
|
|
121
|
+
paidThrough: z.ZodNullable<z.ZodString>;
|
|
122
|
+
cancelAtPeriodEnd: z.ZodBoolean;
|
|
123
|
+
checkoutUrl: z.ZodNullable<z.ZodString>;
|
|
124
|
+
revision: z.ZodNumber;
|
|
125
|
+
}, z.core.$strip>;
|
|
126
|
+
export type PaymentSubscriptionData = z.infer<typeof subscriptionSchema>;
|
|
127
|
+
export interface PaymentSubscriptionPortalInput {
|
|
128
|
+
returnPath: string;
|
|
129
|
+
}
|
|
130
|
+
declare const refundResultSchema: z.ZodObject<{
|
|
131
|
+
subscriptionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
132
|
+
periodStart: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
133
|
+
periodEnd: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
134
|
+
id: z.ZodString;
|
|
135
|
+
bindingId: z.ZodString;
|
|
136
|
+
orderId: z.ZodString;
|
|
137
|
+
priceId: z.ZodString;
|
|
138
|
+
amount: z.ZodNumber;
|
|
139
|
+
currency: z.ZodString;
|
|
140
|
+
feeAmount: z.ZodNumber;
|
|
141
|
+
revision: z.ZodNumber;
|
|
142
|
+
refundedAmount: z.ZodNumber;
|
|
143
|
+
status: z.ZodEnum<{
|
|
144
|
+
pending: "pending";
|
|
145
|
+
failed: "failed";
|
|
146
|
+
paid: "paid";
|
|
147
|
+
refunded: "refunded";
|
|
148
|
+
partially_refunded: "partially_refunded";
|
|
149
|
+
}>;
|
|
150
|
+
checkoutUrl: z.ZodNullable<z.ZodString>;
|
|
151
|
+
deliveredAt: z.ZodNullable<z.ZodString>;
|
|
152
|
+
refund: z.ZodObject<{
|
|
153
|
+
id: z.ZodString;
|
|
154
|
+
amount: z.ZodNumber;
|
|
155
|
+
status: z.ZodEnum<{
|
|
156
|
+
pending: "pending";
|
|
157
|
+
failed: "failed";
|
|
158
|
+
succeeded: "succeeded";
|
|
159
|
+
canceled: "canceled";
|
|
160
|
+
requires_action: "requires_action";
|
|
161
|
+
}>;
|
|
162
|
+
}, z.core.$strip>;
|
|
163
|
+
}, z.core.$strip>;
|
|
164
|
+
export type PaymentRefundResult = z.infer<typeof refundResultSchema>;
|
|
165
|
+
declare const activationSchema: z.ZodObject<{
|
|
166
|
+
revision: z.ZodNumber;
|
|
167
|
+
reviewId: z.ZodString;
|
|
168
|
+
expiresAt: z.ZodString;
|
|
169
|
+
blockers: z.ZodArray<z.ZodString>;
|
|
170
|
+
binding: z.ZodObject<{
|
|
171
|
+
target: z.ZodOptional<z.ZodEnum<{
|
|
172
|
+
preview: "preview";
|
|
173
|
+
publication: "publication";
|
|
174
|
+
}>>;
|
|
175
|
+
id: z.ZodString;
|
|
176
|
+
notebookId: z.ZodString;
|
|
177
|
+
applicationId: z.ZodNullable<z.ZodString>;
|
|
178
|
+
mode: z.ZodEnum<{
|
|
179
|
+
test: "test";
|
|
180
|
+
live: "live";
|
|
181
|
+
}>;
|
|
182
|
+
revision: z.ZodNumber;
|
|
183
|
+
accountId: z.ZodString;
|
|
184
|
+
enabled: z.ZodBoolean;
|
|
185
|
+
callbackUrl: z.ZodNullable<z.ZodString>;
|
|
186
|
+
originUrl: z.ZodNullable<z.ZodString>;
|
|
187
|
+
runtimeConfigured: z.ZodBoolean;
|
|
188
|
+
}, z.core.$strip>;
|
|
189
|
+
account: z.ZodObject<{
|
|
190
|
+
id: z.ZodString;
|
|
191
|
+
mode: z.ZodEnum<{
|
|
192
|
+
test: "test";
|
|
193
|
+
live: "live";
|
|
194
|
+
}>;
|
|
195
|
+
chargesEnabled: z.ZodBoolean;
|
|
196
|
+
payoutsEnabled: z.ZodBoolean;
|
|
197
|
+
detailsSubmitted: z.ZodBoolean;
|
|
198
|
+
requirements: z.ZodArray<z.ZodString>;
|
|
199
|
+
disabledReason: z.ZodNullable<z.ZodString>;
|
|
200
|
+
}, z.core.$strip>;
|
|
201
|
+
feeBasisPoints: z.ZodNullable<z.ZodNumber>;
|
|
202
|
+
prices: z.ZodArray<z.ZodObject<{
|
|
203
|
+
billingInterval: z.ZodDefault<z.ZodNullable<z.ZodEnum<{
|
|
204
|
+
month: "month";
|
|
205
|
+
year: "year";
|
|
206
|
+
}>>>;
|
|
207
|
+
id: z.ZodString;
|
|
208
|
+
name: z.ZodString;
|
|
209
|
+
amount: z.ZodNumber;
|
|
210
|
+
currency: z.ZodString;
|
|
211
|
+
bindingId: z.ZodString;
|
|
212
|
+
}, z.core.$strip>>;
|
|
213
|
+
}, z.core.$strip>;
|
|
214
|
+
export type PaymentActivationReview = z.infer<typeof activationSchema>;
|
|
215
|
+
export interface ConfigurePaymentsInput {
|
|
216
|
+
target?: 'preview' | 'publication';
|
|
217
|
+
expectedRevision: number;
|
|
218
|
+
applicationId?: string | null;
|
|
219
|
+
callbackPath?: string;
|
|
220
|
+
}
|
|
221
|
+
export interface CreatePaymentPriceInput {
|
|
222
|
+
billingInterval?: 'month' | 'year';
|
|
223
|
+
requestId: string;
|
|
224
|
+
name: string;
|
|
225
|
+
amount: number;
|
|
226
|
+
currency: string;
|
|
227
|
+
}
|
|
228
|
+
export interface CreatePaymentCheckoutInput {
|
|
229
|
+
customerId?: string;
|
|
230
|
+
requestId: string;
|
|
231
|
+
orderId: string;
|
|
232
|
+
priceId: string;
|
|
233
|
+
successPath: string;
|
|
234
|
+
cancelPath: string;
|
|
235
|
+
}
|
|
236
|
+
export interface RefundPaymentInput {
|
|
237
|
+
requestId: string;
|
|
238
|
+
amount?: number;
|
|
239
|
+
}
|
|
240
|
+
export declare class PaymentsApi {
|
|
241
|
+
private readonly client;
|
|
242
|
+
readonly runtime: PaymentRuntime;
|
|
243
|
+
constructor(client: Client);
|
|
244
|
+
forNotebook(notebookId: string): NotebookPayments;
|
|
245
|
+
applications(notebookId: string): Promise<PaymentApplicationsData>;
|
|
246
|
+
access(): Promise<PaymentsAccess>;
|
|
247
|
+
accounts(): Promise<PaymentAccountData[]>;
|
|
248
|
+
createAccount(mode: PaymentMode, country?: string): Promise<PaymentAccountData>;
|
|
249
|
+
onboarding(mode: PaymentMode): Promise<{
|
|
250
|
+
url: string;
|
|
251
|
+
expiresAt: string;
|
|
252
|
+
}>;
|
|
253
|
+
}
|
|
254
|
+
export declare class NotebookPayments {
|
|
255
|
+
private readonly notebookId;
|
|
256
|
+
private readonly client;
|
|
257
|
+
constructor(notebookId: string, client: Client);
|
|
258
|
+
private path;
|
|
259
|
+
get(mode?: PaymentMode): Promise<PaymentBindingData | null>;
|
|
260
|
+
configure(input: ConfigurePaymentsInput, mode?: PaymentMode): Promise<PaymentBindingData>;
|
|
261
|
+
provision(mode?: PaymentMode): Promise<PaymentBindingData>;
|
|
262
|
+
prices(mode?: PaymentMode): Promise<PaymentPriceData[]>;
|
|
263
|
+
createPrice(input: CreatePaymentPriceInput, mode?: PaymentMode): Promise<PaymentPriceData>;
|
|
264
|
+
prepareActivation(mode?: PaymentMode): Promise<PaymentActivationReview>;
|
|
265
|
+
activate(reviewId: string, mode?: PaymentMode): Promise<PaymentBindingData>;
|
|
266
|
+
disable(mode?: PaymentMode): Promise<PaymentBindingData>;
|
|
267
|
+
checkout(input: CreatePaymentCheckoutInput, mode?: PaymentMode): Promise<PaymentTransactionData>;
|
|
268
|
+
subscribe(input: CreatePaymentCheckoutInput, mode?: PaymentMode): Promise<PaymentSubscriptionData>;
|
|
269
|
+
subscriptions(mode?: PaymentMode): Promise<PaymentSubscriptionData[]>;
|
|
270
|
+
subscription(id: string, mode?: PaymentMode): Promise<PaymentSubscriptionData>;
|
|
271
|
+
cancelSubscription(id: string, mode?: PaymentMode): Promise<PaymentSubscriptionData>;
|
|
272
|
+
subscriptionPortal(id: string, input: PaymentSubscriptionPortalInput, mode?: PaymentMode): Promise<{
|
|
273
|
+
url: string;
|
|
274
|
+
}>;
|
|
275
|
+
transactions(mode?: PaymentMode): Promise<PaymentTransactionData[]>;
|
|
276
|
+
refund(id: string, input: RefundPaymentInput, mode?: PaymentMode): Promise<PaymentRefundResult>;
|
|
277
|
+
}
|
|
278
|
+
declare const paymentEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
279
|
+
id: z.ZodString;
|
|
280
|
+
type: z.ZodLiteral<"payment.updated">;
|
|
281
|
+
mode: z.ZodEnum<{
|
|
282
|
+
test: "test";
|
|
283
|
+
live: "live";
|
|
284
|
+
}>;
|
|
285
|
+
transaction: z.ZodObject<{
|
|
286
|
+
subscriptionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
287
|
+
periodStart: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
288
|
+
periodEnd: z.ZodDefault<z.ZodNullable<z.ZodString>>;
|
|
289
|
+
id: z.ZodString;
|
|
290
|
+
bindingId: z.ZodString;
|
|
291
|
+
orderId: z.ZodString;
|
|
292
|
+
priceId: z.ZodString;
|
|
293
|
+
amount: z.ZodNumber;
|
|
294
|
+
currency: z.ZodString;
|
|
295
|
+
feeAmount: z.ZodNumber;
|
|
296
|
+
revision: z.ZodNumber;
|
|
297
|
+
refundedAmount: z.ZodNumber;
|
|
298
|
+
status: z.ZodEnum<{
|
|
299
|
+
pending: "pending";
|
|
300
|
+
failed: "failed";
|
|
301
|
+
paid: "paid";
|
|
302
|
+
refunded: "refunded";
|
|
303
|
+
partially_refunded: "partially_refunded";
|
|
304
|
+
}>;
|
|
305
|
+
checkoutUrl: z.ZodNullable<z.ZodString>;
|
|
306
|
+
deliveredAt: z.ZodNullable<z.ZodString>;
|
|
307
|
+
}, z.core.$strip>;
|
|
308
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
309
|
+
id: z.ZodString;
|
|
310
|
+
type: z.ZodLiteral<"subscription.updated">;
|
|
311
|
+
mode: z.ZodEnum<{
|
|
312
|
+
test: "test";
|
|
313
|
+
live: "live";
|
|
314
|
+
}>;
|
|
315
|
+
subscription: z.ZodObject<{
|
|
316
|
+
id: z.ZodString;
|
|
317
|
+
bindingId: z.ZodString;
|
|
318
|
+
priceId: z.ZodString;
|
|
319
|
+
orderId: z.ZodString;
|
|
320
|
+
status: z.ZodEnum<{
|
|
321
|
+
pending: "pending";
|
|
322
|
+
incomplete: "incomplete";
|
|
323
|
+
incomplete_expired: "incomplete_expired";
|
|
324
|
+
trialing: "trialing";
|
|
325
|
+
active: "active";
|
|
326
|
+
past_due: "past_due";
|
|
327
|
+
canceled: "canceled";
|
|
328
|
+
unpaid: "unpaid";
|
|
329
|
+
paused: "paused";
|
|
330
|
+
}>;
|
|
331
|
+
billingInterval: z.ZodEnum<{
|
|
332
|
+
month: "month";
|
|
333
|
+
year: "year";
|
|
334
|
+
}>;
|
|
335
|
+
paidThrough: z.ZodNullable<z.ZodString>;
|
|
336
|
+
cancelAtPeriodEnd: z.ZodBoolean;
|
|
337
|
+
checkoutUrl: z.ZodNullable<z.ZodString>;
|
|
338
|
+
revision: z.ZodNumber;
|
|
339
|
+
}, z.core.$strip>;
|
|
340
|
+
}, z.core.$strip>], "type">;
|
|
341
|
+
export type PaymentEvent = z.infer<typeof paymentEventSchema>;
|
|
342
|
+
/** Verify the original bytes before parsing JSON. Use only in trusted server code. */
|
|
343
|
+
export declare function verifyPaymentEvent(body: string, signature: string, token: string, mode: PaymentMode, now?: number): Promise<PaymentEvent>;
|
|
344
|
+
export declare class PaymentRuntime {
|
|
345
|
+
private readonly client;
|
|
346
|
+
private readonly appCredential?;
|
|
347
|
+
constructor(client: Client, appCredential?: {
|
|
348
|
+
token: string;
|
|
349
|
+
mode: PaymentMode;
|
|
350
|
+
} | undefined);
|
|
351
|
+
/** Verify the phpsandbox-signature header against the configured app environment. */
|
|
352
|
+
verifyEvent(body: string, signature: string): Promise<PaymentEvent>;
|
|
353
|
+
checkout(input: CreatePaymentCheckoutInput): Promise<PaymentTransactionData>;
|
|
354
|
+
transaction(id: string): Promise<PaymentTransactionData>;
|
|
355
|
+
subscribe(input: CreatePaymentCheckoutInput): Promise<PaymentSubscriptionData>;
|
|
356
|
+
subscription(id: string): Promise<PaymentSubscriptionData>;
|
|
357
|
+
cancelSubscription(id: string): Promise<PaymentSubscriptionData>;
|
|
358
|
+
subscriptionPortal(id: string, input: PaymentSubscriptionPortalInput): Promise<{
|
|
359
|
+
url: string;
|
|
360
|
+
}>;
|
|
361
|
+
prices(): Promise<PaymentPriceData[]>;
|
|
362
|
+
}
|
|
363
|
+
export {};
|
package/dist/payments.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const DEFAULT_PAYMENT_CALLBACK_PATH = '/api/phpsandbox-payments/events';
|
|
3
|
+
export const paymentModeSchema = z.enum(['test', 'live']);
|
|
4
|
+
const accountSchema = z.object({
|
|
5
|
+
id: z.string(),
|
|
6
|
+
mode: paymentModeSchema,
|
|
7
|
+
chargesEnabled: z.boolean(),
|
|
8
|
+
payoutsEnabled: z.boolean(),
|
|
9
|
+
detailsSubmitted: z.boolean(),
|
|
10
|
+
requirements: z.array(z.string()),
|
|
11
|
+
disabledReason: z.string().nullable(),
|
|
12
|
+
});
|
|
13
|
+
const accessSchema = z.object({
|
|
14
|
+
subscribed: z.boolean(),
|
|
15
|
+
liveAllowed: z.boolean(),
|
|
16
|
+
testConfigured: z.boolean(),
|
|
17
|
+
liveConfigured: z.boolean(),
|
|
18
|
+
feeBasisPoints: z.number().int().min(0).max(10000).nullable(),
|
|
19
|
+
canRead: z.boolean(),
|
|
20
|
+
canManage: z.boolean(),
|
|
21
|
+
canRefund: z.boolean(),
|
|
22
|
+
recoveryEndsAt: z.string().nullable().default(null),
|
|
23
|
+
});
|
|
24
|
+
const applicationsSchema = z.object({
|
|
25
|
+
notebookId: z.string(),
|
|
26
|
+
applications: z.array(z.object({
|
|
27
|
+
id: z.string(),
|
|
28
|
+
url: z.string().nullable(),
|
|
29
|
+
healthy: z.boolean(),
|
|
30
|
+
passwordProtected: z.boolean(),
|
|
31
|
+
deployedAt: z.string().datetime().nullable(),
|
|
32
|
+
})),
|
|
33
|
+
});
|
|
34
|
+
const bindingSchema = z.object({
|
|
35
|
+
target: z.enum(['preview', 'publication']).optional(),
|
|
36
|
+
id: z.string(),
|
|
37
|
+
notebookId: z.string(),
|
|
38
|
+
applicationId: z.string().nullable(),
|
|
39
|
+
mode: paymentModeSchema,
|
|
40
|
+
revision: z.number().int(),
|
|
41
|
+
accountId: z.string(),
|
|
42
|
+
enabled: z.boolean(),
|
|
43
|
+
callbackUrl: z.string().nullable(),
|
|
44
|
+
originUrl: z.string().nullable(),
|
|
45
|
+
runtimeConfigured: z.boolean(),
|
|
46
|
+
});
|
|
47
|
+
const priceSchema = z.object({
|
|
48
|
+
billingInterval: z.enum(['month', 'year']).nullable().default(null),
|
|
49
|
+
id: z.string(),
|
|
50
|
+
name: z.string(),
|
|
51
|
+
amount: z.number().int(),
|
|
52
|
+
currency: z.string(),
|
|
53
|
+
bindingId: z.string(),
|
|
54
|
+
});
|
|
55
|
+
const transactionSchema = z.object({
|
|
56
|
+
subscriptionId: z.string().nullable().default(null),
|
|
57
|
+
periodStart: z.string().datetime({ offset: true }).nullable().default(null),
|
|
58
|
+
periodEnd: z.string().datetime({ offset: true }).nullable().default(null),
|
|
59
|
+
id: z.string(),
|
|
60
|
+
bindingId: z.string(),
|
|
61
|
+
orderId: z.string(),
|
|
62
|
+
priceId: z.string(),
|
|
63
|
+
amount: z.number().int(),
|
|
64
|
+
currency: z.string(),
|
|
65
|
+
feeAmount: z.number().int(),
|
|
66
|
+
revision: z.number().int(),
|
|
67
|
+
refundedAmount: z.number().int(),
|
|
68
|
+
status: z.enum(['pending', 'paid', 'failed', 'refunded', 'partially_refunded']),
|
|
69
|
+
checkoutUrl: z.string().nullable(),
|
|
70
|
+
deliveredAt: z.string().nullable(),
|
|
71
|
+
});
|
|
72
|
+
const subscriptionSchema = z.object({
|
|
73
|
+
id: z.string(),
|
|
74
|
+
bindingId: z.string(),
|
|
75
|
+
priceId: z.string(),
|
|
76
|
+
orderId: z.string(),
|
|
77
|
+
status: z.enum([
|
|
78
|
+
'pending',
|
|
79
|
+
'incomplete',
|
|
80
|
+
'incomplete_expired',
|
|
81
|
+
'trialing',
|
|
82
|
+
'active',
|
|
83
|
+
'past_due',
|
|
84
|
+
'canceled',
|
|
85
|
+
'unpaid',
|
|
86
|
+
'paused',
|
|
87
|
+
]),
|
|
88
|
+
billingInterval: z.enum(['month', 'year']),
|
|
89
|
+
paidThrough: z.string().datetime({ offset: true }).nullable(),
|
|
90
|
+
cancelAtPeriodEnd: z.boolean(),
|
|
91
|
+
checkoutUrl: z.string().nullable(),
|
|
92
|
+
revision: z.number().int(),
|
|
93
|
+
});
|
|
94
|
+
const portalSchema = z.object({ url: z.string().url() });
|
|
95
|
+
const refundResultSchema = transactionSchema.extend({
|
|
96
|
+
refund: z.object({
|
|
97
|
+
id: z.string(),
|
|
98
|
+
amount: z.number().int().positive(),
|
|
99
|
+
status: z.enum(['pending', 'succeeded', 'failed', 'canceled', 'requires_action']),
|
|
100
|
+
}),
|
|
101
|
+
});
|
|
102
|
+
const activationSchema = z.object({
|
|
103
|
+
revision: z.number().int(),
|
|
104
|
+
reviewId: z.string(),
|
|
105
|
+
expiresAt: z.string(),
|
|
106
|
+
blockers: z.array(z.string()),
|
|
107
|
+
binding: bindingSchema,
|
|
108
|
+
account: accountSchema,
|
|
109
|
+
feeBasisPoints: z.number().int().nullable(),
|
|
110
|
+
prices: z.array(priceSchema),
|
|
111
|
+
});
|
|
112
|
+
export class PaymentsApi {
|
|
113
|
+
constructor(client) {
|
|
114
|
+
this.client = client;
|
|
115
|
+
this.runtime = new PaymentRuntime(client);
|
|
116
|
+
}
|
|
117
|
+
forNotebook(notebookId) {
|
|
118
|
+
return new NotebookPayments(notebookId, this.client);
|
|
119
|
+
}
|
|
120
|
+
async applications(notebookId) {
|
|
121
|
+
return applicationsSchema.parse((await this.client.get(`/notebook/${encodeURIComponent(notebookId)}/payments/applications`)).data);
|
|
122
|
+
}
|
|
123
|
+
async access() {
|
|
124
|
+
return accessSchema.parse((await this.client.get('/platform/payments/access')).data);
|
|
125
|
+
}
|
|
126
|
+
async accounts() {
|
|
127
|
+
return z.array(accountSchema).parse((await this.client.get('/platform/payments/accounts')).data);
|
|
128
|
+
}
|
|
129
|
+
async createAccount(mode, country) {
|
|
130
|
+
return accountSchema.parse((await this.client.put(`/platform/payments/accounts/${mode}`, { country })).data);
|
|
131
|
+
}
|
|
132
|
+
async onboarding(mode) {
|
|
133
|
+
return z
|
|
134
|
+
.object({ url: z.string().url(), expiresAt: z.string() })
|
|
135
|
+
.parse((await this.client.post(`/platform/payments/accounts/${mode}/onboarding`, {})).data);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export class NotebookPayments {
|
|
139
|
+
constructor(notebookId, client) {
|
|
140
|
+
this.notebookId = notebookId;
|
|
141
|
+
this.client = client;
|
|
142
|
+
}
|
|
143
|
+
path(mode) {
|
|
144
|
+
return `/platform/payments/sandboxes/${encodeURIComponent(this.notebookId)}/${mode}`;
|
|
145
|
+
}
|
|
146
|
+
async get(mode = 'test') {
|
|
147
|
+
return bindingSchema.nullable().parse((await this.client.get(this.path(mode))).data);
|
|
148
|
+
}
|
|
149
|
+
async configure(input, mode = 'test') {
|
|
150
|
+
return bindingSchema.parse((await this.client.put(this.path(mode), input)).data);
|
|
151
|
+
}
|
|
152
|
+
async provision(mode = 'test') {
|
|
153
|
+
return bindingSchema.parse((await this.client.post(`${this.path(mode)}/provision`, {})).data);
|
|
154
|
+
}
|
|
155
|
+
async prices(mode = 'test') {
|
|
156
|
+
return z.array(priceSchema).parse((await this.client.get(`${this.path(mode)}/prices`)).data);
|
|
157
|
+
}
|
|
158
|
+
async createPrice(input, mode = 'test') {
|
|
159
|
+
return priceSchema.parse((await this.client.post(`${this.path(mode)}/prices`, input)).data);
|
|
160
|
+
}
|
|
161
|
+
async prepareActivation(mode = 'test') {
|
|
162
|
+
return activationSchema.parse((await this.client.post(`${this.path(mode)}/activation`, {})).data);
|
|
163
|
+
}
|
|
164
|
+
async activate(reviewId, mode = 'test') {
|
|
165
|
+
return bindingSchema.parse((await this.client.post(`${this.path(mode)}/activate`, { reviewId })).data);
|
|
166
|
+
}
|
|
167
|
+
async disable(mode = 'test') {
|
|
168
|
+
return bindingSchema.parse((await this.client.post(`${this.path(mode)}/disable`, {})).data);
|
|
169
|
+
}
|
|
170
|
+
async checkout(input, mode = 'test') {
|
|
171
|
+
return transactionSchema.parse((await this.client.post(`${this.path(mode)}/checkouts`, input)).data);
|
|
172
|
+
}
|
|
173
|
+
async subscribe(input, mode = 'test') {
|
|
174
|
+
return subscriptionSchema.parse((await this.client.post(`${this.path(mode)}/subscriptions`, input)).data);
|
|
175
|
+
}
|
|
176
|
+
async subscriptions(mode = 'test') {
|
|
177
|
+
return z.array(subscriptionSchema).parse((await this.client.get(`${this.path(mode)}/subscriptions`)).data);
|
|
178
|
+
}
|
|
179
|
+
async subscription(id, mode = 'test') {
|
|
180
|
+
return subscriptionSchema.parse((await this.client.get(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}`)).data);
|
|
181
|
+
}
|
|
182
|
+
async cancelSubscription(id, mode = 'test') {
|
|
183
|
+
return subscriptionSchema.parse((await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data);
|
|
184
|
+
}
|
|
185
|
+
async subscriptionPortal(id, input, mode = 'test') {
|
|
186
|
+
return portalSchema.parse((await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/portal`, input)).data);
|
|
187
|
+
}
|
|
188
|
+
async transactions(mode = 'test') {
|
|
189
|
+
return z.array(transactionSchema).parse((await this.client.get(`${this.path(mode)}/transactions`)).data);
|
|
190
|
+
}
|
|
191
|
+
async refund(id, input, mode = 'test') {
|
|
192
|
+
return refundResultSchema.parse((await this.client.post(`${this.path(mode)}/transactions/${encodeURIComponent(id)}/refund`, input)).data);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const paymentEventSchema = z.discriminatedUnion('type', [
|
|
196
|
+
z.object({
|
|
197
|
+
id: z.string(),
|
|
198
|
+
type: z.literal('payment.updated'),
|
|
199
|
+
mode: paymentModeSchema,
|
|
200
|
+
transaction: transactionSchema,
|
|
201
|
+
}),
|
|
202
|
+
z.object({
|
|
203
|
+
id: z.string(),
|
|
204
|
+
type: z.literal('subscription.updated'),
|
|
205
|
+
mode: paymentModeSchema,
|
|
206
|
+
subscription: subscriptionSchema,
|
|
207
|
+
}),
|
|
208
|
+
]);
|
|
209
|
+
/** Verify the original bytes before parsing JSON. Use only in trusted server code. */
|
|
210
|
+
export async function verifyPaymentEvent(body, signature, token, mode, now = Date.now()) {
|
|
211
|
+
if (!/^(?:hpay|happ)_[a-f0-9]{64}$/.test(token)) {
|
|
212
|
+
throw new Error('Invalid application payment credential.');
|
|
213
|
+
}
|
|
214
|
+
const match = /^t=(\d+),v1=([a-f0-9]{64})$/.exec(signature);
|
|
215
|
+
const timestamp = match?.[1];
|
|
216
|
+
const digest = match?.[2];
|
|
217
|
+
if (!timestamp || !digest || Math.abs(now - Number(timestamp) * 1000) > 300000 || body.length > 262144) {
|
|
218
|
+
throw new Error('Invalid or expired payment signature.');
|
|
219
|
+
}
|
|
220
|
+
const encoder = new TextEncoder();
|
|
221
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(token), { name: 'HMAC', hash: 'SHA-256' }, false, ['verify']);
|
|
222
|
+
const bytes = Uint8Array.from(digest.match(/../g) ?? [], (byte) => Number.parseInt(byte, 16));
|
|
223
|
+
if (!(await crypto.subtle.verify('HMAC', key, bytes, encoder.encode(`${timestamp}.${body}`)))) {
|
|
224
|
+
throw new Error('Invalid payment signature.');
|
|
225
|
+
}
|
|
226
|
+
const event = paymentEventSchema.parse(JSON.parse(body));
|
|
227
|
+
if (event.mode !== mode) {
|
|
228
|
+
throw new Error('Payment event mode does not match this application.');
|
|
229
|
+
}
|
|
230
|
+
return event;
|
|
231
|
+
}
|
|
232
|
+
export class PaymentRuntime {
|
|
233
|
+
constructor(client, appCredential) {
|
|
234
|
+
this.client = client;
|
|
235
|
+
this.appCredential = appCredential;
|
|
236
|
+
}
|
|
237
|
+
/** Verify the phpsandbox-signature header against the configured app environment. */
|
|
238
|
+
async verifyEvent(body, signature) {
|
|
239
|
+
if (!this.appCredential) {
|
|
240
|
+
throw new Error('Initialize the server app client with PHPSandbox.app() to verify payment events.');
|
|
241
|
+
}
|
|
242
|
+
return verifyPaymentEvent(body, signature, this.appCredential.token, this.appCredential.mode);
|
|
243
|
+
}
|
|
244
|
+
async checkout(input) {
|
|
245
|
+
return transactionSchema.parse((await this.client.post('/platform/payments/runtime/checkouts', input)).data);
|
|
246
|
+
}
|
|
247
|
+
async transaction(id) {
|
|
248
|
+
return transactionSchema.parse((await this.client.get(`/platform/payments/runtime/transactions/${encodeURIComponent(id)}`)).data);
|
|
249
|
+
}
|
|
250
|
+
async subscribe(input) {
|
|
251
|
+
return subscriptionSchema.parse((await this.client.post('/platform/payments/runtime/subscriptions', input)).data);
|
|
252
|
+
}
|
|
253
|
+
async subscription(id) {
|
|
254
|
+
return subscriptionSchema.parse((await this.client.get(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}`)).data);
|
|
255
|
+
}
|
|
256
|
+
async cancelSubscription(id) {
|
|
257
|
+
return subscriptionSchema.parse((await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data);
|
|
258
|
+
}
|
|
259
|
+
async subscriptionPortal(id, input) {
|
|
260
|
+
return portalSchema.parse((await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/portal`, input)).data);
|
|
261
|
+
}
|
|
262
|
+
async prices() {
|
|
263
|
+
return z.array(priceSchema).parse((await this.client.get('/platform/payments/runtime/prices')).data);
|
|
264
|
+
}
|
|
265
|
+
}
|