@phpsandbox/sdk 0.3.4 → 0.4.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.
@@ -29,14 +29,18 @@ var PHPSandbox = (() => {
29
29
  // src/index.ts
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
+ DEFAULT_PAYMENT_CALLBACK_PATH: () => DEFAULT_PAYMENT_CALLBACK_PATH,
32
33
  FileChangeFilter: () => FileChangeFilter,
33
34
  FileChangeType: () => FileChangeType,
34
35
  FileType: () => FileType,
35
36
  LspConnection: () => LspConnection,
36
37
  NotebookInstance: () => NotebookInstance,
38
+ NotebookPayments: () => NotebookPayments,
37
39
  NotebookPublication: () => NotebookPublication,
38
40
  PHPSandbox: () => PHPSandbox,
39
41
  PHPSandboxError: () => PHPSandboxError,
42
+ PaymentRuntime: () => PaymentRuntime,
43
+ PaymentsApi: () => PaymentsApi,
40
44
  PublicationApi: () => PublicationApi,
41
45
  PublicationDomains: () => PublicationDomains,
42
46
  PublicationInstance: () => PublicationInstance,
@@ -48,7 +52,9 @@ var PHPSandbox = (() => {
48
52
  laravelCloudSetupForRepublish: () => laravelCloudSetupForRepublish,
49
53
  notebookBuiltinServices: () => notebookBuiltinServices,
50
54
  notebookKnownServices: () => notebookKnownServices,
51
- readPublicationStream: () => readPublicationStream
55
+ paymentModeSchema: () => paymentModeSchema,
56
+ readPublicationStream: () => readPublicationStream,
57
+ verifyPaymentEvent: () => verifyPaymentEvent
52
58
  });
53
59
 
54
60
  // node_modules/zod/v4/classic/external.js
@@ -19715,6 +19721,15 @@ ${JSON.stringify(metadata, null, 2)}` : "";
19715
19721
  constructor(client) {
19716
19722
  this.client = client;
19717
19723
  }
19724
+ async inspectInvitation(id, token) {
19725
+ return (await this.client.post(`/platform/server-invitations/${encodeURIComponent(id)}/inspect`, { token })).data;
19726
+ }
19727
+ async acceptInvitation(id, token) {
19728
+ return (await this.client.post(`/platform/server-invitations/${encodeURIComponent(id)}/accept`, { token })).data;
19729
+ }
19730
+ async declineInvitation(id, token) {
19731
+ return (await this.client.post(`/platform/server-invitations/${encodeURIComponent(id)}/decline`, { token })).data;
19732
+ }
19718
19733
  async list(options = {}) {
19719
19734
  const query = options.page === void 0 ? "" : `?${new URLSearchParams({ page: String(options.page) }).toString()}`;
19720
19735
  return this.client.get(`/servers${query}`);
@@ -19740,17 +19755,40 @@ ${JSON.stringify(metadata, null, 2)}` : "";
19740
19755
  const query = options.page === void 0 ? "" : `?${new URLSearchParams({ page: String(options.page) }).toString()}`;
19741
19756
  return this.client.get(`/servers/${encodeURIComponent(this.data.id)}/members${query}`);
19742
19757
  }
19743
- async share(email3) {
19744
- return (await this.client.post(`/servers/${encodeURIComponent(this.data.id)}/members`, { email: email3 })).data;
19745
- }
19746
- async shareWithOrganization(organizationId) {
19747
- return (await this.client.post(`/servers/${encodeURIComponent(this.data.id)}/members`, { organizationId })).data;
19758
+ async share(target) {
19759
+ if (target.email !== void 0) {
19760
+ return (await this.client.post(`/platform/servers/${encodeURIComponent(this.data.id)}/invitations`, {
19761
+ email: target.email,
19762
+ requestId: target.requestId ?? crypto.randomUUID()
19763
+ })).data;
19764
+ }
19765
+ const body = { organizationId: target.organizationId, requestId: target.requestId };
19766
+ return (await this.client.post(`/servers/${encodeURIComponent(this.data.id)}/members`, body)).data;
19748
19767
  }
19749
19768
  async revokeAccess(memberId) {
19750
19769
  await this.client.delete(
19751
19770
  `/servers/${encodeURIComponent(this.data.id)}/members/${encodeURIComponent(String(memberId))}`
19752
19771
  );
19753
19772
  }
19773
+ async invitations(options = {}) {
19774
+ const query = options.page === void 0 ? "" : `?${new URLSearchParams({ page: String(options.page) }).toString()}`;
19775
+ return this.client.get(`/platform/servers/${encodeURIComponent(this.data.id)}/invitations${query}`);
19776
+ }
19777
+ async cancelInvitation(id) {
19778
+ const response = await this.client.delete(
19779
+ `/platform/servers/${encodeURIComponent(this.data.id)}/invitations/${encodeURIComponent(id)}`
19780
+ );
19781
+ if (response.data === void 0) {
19782
+ throw new PHPSandboxError("Missing invitation cancellation response.");
19783
+ }
19784
+ return response.data;
19785
+ }
19786
+ async resendInvitation(id) {
19787
+ return (await this.client.post(
19788
+ `/platform/servers/${encodeURIComponent(this.data.id)}/invitations/${encodeURIComponent(id)}/resend`,
19789
+ {}
19790
+ )).data;
19791
+ }
19754
19792
  refresh() {
19755
19793
  return this.client.servers.get(this.data.id);
19756
19794
  }
@@ -20159,6 +20197,280 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20159
20197
  return typeof integration === "string" ? integration : integration.id;
20160
20198
  }
20161
20199
 
20200
+ // src/payments.ts
20201
+ var DEFAULT_PAYMENT_CALLBACK_PATH = "/api/phpsandbox-payments/events";
20202
+ var paymentModeSchema = external_exports.enum(["test", "live"]);
20203
+ var accountSchema = external_exports.object({
20204
+ id: external_exports.string(),
20205
+ mode: paymentModeSchema,
20206
+ chargesEnabled: external_exports.boolean(),
20207
+ payoutsEnabled: external_exports.boolean(),
20208
+ detailsSubmitted: external_exports.boolean(),
20209
+ requirements: external_exports.array(external_exports.string()),
20210
+ disabledReason: external_exports.string().nullable()
20211
+ });
20212
+ var accessSchema = external_exports.object({
20213
+ subscribed: external_exports.boolean(),
20214
+ liveAllowed: external_exports.boolean(),
20215
+ testConfigured: external_exports.boolean(),
20216
+ liveConfigured: external_exports.boolean(),
20217
+ feeBasisPoints: external_exports.number().int().min(0).max(1e4).nullable(),
20218
+ canRead: external_exports.boolean(),
20219
+ canManage: external_exports.boolean(),
20220
+ canRefund: external_exports.boolean(),
20221
+ recoveryEndsAt: external_exports.string().nullable().default(null)
20222
+ });
20223
+ var applicationsSchema = external_exports.object({
20224
+ notebookId: external_exports.string(),
20225
+ applications: external_exports.array(
20226
+ external_exports.object({
20227
+ id: external_exports.string(),
20228
+ url: external_exports.string().nullable(),
20229
+ healthy: external_exports.boolean(),
20230
+ passwordProtected: external_exports.boolean(),
20231
+ deployedAt: external_exports.string().datetime().nullable()
20232
+ })
20233
+ )
20234
+ });
20235
+ var bindingSchema = external_exports.object({
20236
+ id: external_exports.string(),
20237
+ notebookId: external_exports.string(),
20238
+ applicationId: external_exports.string().nullable(),
20239
+ mode: paymentModeSchema,
20240
+ revision: external_exports.number().int(),
20241
+ accountId: external_exports.string(),
20242
+ enabled: external_exports.boolean(),
20243
+ callbackUrl: external_exports.string().nullable(),
20244
+ originUrl: external_exports.string().nullable(),
20245
+ runtimeConfigured: external_exports.boolean()
20246
+ });
20247
+ var priceSchema = external_exports.object({
20248
+ billingInterval: external_exports.enum(["month", "year"]).nullable().default(null),
20249
+ id: external_exports.string(),
20250
+ name: external_exports.string(),
20251
+ amount: external_exports.number().int(),
20252
+ currency: external_exports.string(),
20253
+ bindingId: external_exports.string()
20254
+ });
20255
+ var transactionSchema = external_exports.object({
20256
+ subscriptionId: external_exports.string().nullable().default(null),
20257
+ periodStart: external_exports.string().datetime({ offset: true }).nullable().default(null),
20258
+ periodEnd: external_exports.string().datetime({ offset: true }).nullable().default(null),
20259
+ id: external_exports.string(),
20260
+ bindingId: external_exports.string(),
20261
+ orderId: external_exports.string(),
20262
+ priceId: external_exports.string(),
20263
+ amount: external_exports.number().int(),
20264
+ currency: external_exports.string(),
20265
+ feeAmount: external_exports.number().int(),
20266
+ revision: external_exports.number().int(),
20267
+ refundedAmount: external_exports.number().int(),
20268
+ status: external_exports.enum(["pending", "paid", "failed", "refunded", "partially_refunded"]),
20269
+ checkoutUrl: external_exports.string().nullable(),
20270
+ deliveredAt: external_exports.string().nullable()
20271
+ });
20272
+ var subscriptionSchema = external_exports.object({
20273
+ id: external_exports.string(),
20274
+ bindingId: external_exports.string(),
20275
+ priceId: external_exports.string(),
20276
+ orderId: external_exports.string(),
20277
+ status: external_exports.enum([
20278
+ "pending",
20279
+ "incomplete",
20280
+ "incomplete_expired",
20281
+ "trialing",
20282
+ "active",
20283
+ "past_due",
20284
+ "canceled",
20285
+ "unpaid",
20286
+ "paused"
20287
+ ]),
20288
+ billingInterval: external_exports.enum(["month", "year"]),
20289
+ paidThrough: external_exports.string().datetime({ offset: true }).nullable(),
20290
+ cancelAtPeriodEnd: external_exports.boolean(),
20291
+ checkoutUrl: external_exports.string().nullable(),
20292
+ revision: external_exports.number().int()
20293
+ });
20294
+ var portalSchema = external_exports.object({ url: external_exports.string().url() });
20295
+ var refundResultSchema = transactionSchema.extend({
20296
+ refund: external_exports.object({
20297
+ id: external_exports.string(),
20298
+ amount: external_exports.number().int().positive(),
20299
+ status: external_exports.enum(["pending", "succeeded", "failed", "canceled", "requires_action"])
20300
+ })
20301
+ });
20302
+ var activationSchema = external_exports.object({
20303
+ revision: external_exports.number().int(),
20304
+ reviewId: external_exports.string(),
20305
+ expiresAt: external_exports.string(),
20306
+ blockers: external_exports.array(external_exports.string()),
20307
+ binding: bindingSchema,
20308
+ account: accountSchema,
20309
+ feeBasisPoints: external_exports.number().int().nullable(),
20310
+ prices: external_exports.array(priceSchema)
20311
+ });
20312
+ var PaymentsApi = class {
20313
+ constructor(client) {
20314
+ this.client = client;
20315
+ this.runtime = new PaymentRuntime(client);
20316
+ }
20317
+ forNotebook(notebookId) {
20318
+ return new NotebookPayments(notebookId, this.client);
20319
+ }
20320
+ async applications(notebookId) {
20321
+ return applicationsSchema.parse(
20322
+ (await this.client.get(`/notebook/${encodeURIComponent(notebookId)}/payments/applications`)).data
20323
+ );
20324
+ }
20325
+ async access() {
20326
+ return accessSchema.parse((await this.client.get("/platform/payments/access")).data);
20327
+ }
20328
+ async accounts() {
20329
+ return external_exports.array(accountSchema).parse((await this.client.get("/platform/payments/accounts")).data);
20330
+ }
20331
+ async createAccount(mode, country) {
20332
+ return accountSchema.parse((await this.client.put(`/platform/payments/accounts/${mode}`, { country })).data);
20333
+ }
20334
+ async onboarding(mode) {
20335
+ return external_exports.object({ url: external_exports.string().url(), expiresAt: external_exports.string() }).parse((await this.client.post(`/platform/payments/accounts/${mode}/onboarding`, {})).data);
20336
+ }
20337
+ };
20338
+ var NotebookPayments = class {
20339
+ constructor(notebookId, client) {
20340
+ this.notebookId = notebookId;
20341
+ this.client = client;
20342
+ }
20343
+ path(mode) {
20344
+ return `/platform/payments/sandboxes/${encodeURIComponent(this.notebookId)}/${mode}`;
20345
+ }
20346
+ async get(mode = "test") {
20347
+ return bindingSchema.nullable().parse((await this.client.get(this.path(mode))).data);
20348
+ }
20349
+ async configure(input, mode = "test") {
20350
+ return bindingSchema.parse((await this.client.put(this.path(mode), input)).data);
20351
+ }
20352
+ async provision(mode = "test") {
20353
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/provision`, {})).data);
20354
+ }
20355
+ async prices(mode = "test") {
20356
+ return external_exports.array(priceSchema).parse((await this.client.get(`${this.path(mode)}/prices`)).data);
20357
+ }
20358
+ async createPrice(input, mode = "test") {
20359
+ return priceSchema.parse((await this.client.post(`${this.path(mode)}/prices`, input)).data);
20360
+ }
20361
+ async prepareActivation(mode = "test") {
20362
+ return activationSchema.parse((await this.client.post(`${this.path(mode)}/activation`, {})).data);
20363
+ }
20364
+ async activate(reviewId, mode = "test") {
20365
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/activate`, { reviewId })).data);
20366
+ }
20367
+ async disable(mode = "test") {
20368
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/disable`, {})).data);
20369
+ }
20370
+ async checkout(input, mode = "test") {
20371
+ return transactionSchema.parse((await this.client.post(`${this.path(mode)}/checkouts`, input)).data);
20372
+ }
20373
+ async subscribe(input, mode = "test") {
20374
+ return subscriptionSchema.parse((await this.client.post(`${this.path(mode)}/subscriptions`, input)).data);
20375
+ }
20376
+ async subscriptions(mode = "test") {
20377
+ return external_exports.array(subscriptionSchema).parse((await this.client.get(`${this.path(mode)}/subscriptions`)).data);
20378
+ }
20379
+ async subscription(id, mode = "test") {
20380
+ return subscriptionSchema.parse(
20381
+ (await this.client.get(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}`)).data
20382
+ );
20383
+ }
20384
+ async cancelSubscription(id, mode = "test") {
20385
+ return subscriptionSchema.parse(
20386
+ (await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data
20387
+ );
20388
+ }
20389
+ async subscriptionPortal(id, input, mode = "test") {
20390
+ return portalSchema.parse(
20391
+ (await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/portal`, input)).data
20392
+ );
20393
+ }
20394
+ async transactions(mode = "test") {
20395
+ return external_exports.array(transactionSchema).parse((await this.client.get(`${this.path(mode)}/transactions`)).data);
20396
+ }
20397
+ async refund(id, input, mode = "test") {
20398
+ return refundResultSchema.parse(
20399
+ (await this.client.post(`${this.path(mode)}/transactions/${encodeURIComponent(id)}/refund`, input)).data
20400
+ );
20401
+ }
20402
+ };
20403
+ var paymentEventSchema = external_exports.discriminatedUnion("type", [
20404
+ external_exports.object({
20405
+ id: external_exports.string(),
20406
+ type: external_exports.literal("payment.updated"),
20407
+ mode: paymentModeSchema,
20408
+ transaction: transactionSchema
20409
+ }),
20410
+ external_exports.object({
20411
+ id: external_exports.string(),
20412
+ type: external_exports.literal("subscription.updated"),
20413
+ mode: paymentModeSchema,
20414
+ subscription: subscriptionSchema
20415
+ })
20416
+ ]);
20417
+ async function verifyPaymentEvent(body, signature, token, mode, now = Date.now()) {
20418
+ if (!/^hpay_[a-f0-9]{64}$/.test(token)) {
20419
+ throw new Error("Invalid application payment credential.");
20420
+ }
20421
+ const match = /^t=(\d+),v1=([a-f0-9]{64})$/.exec(signature);
20422
+ const timestamp = match?.[1];
20423
+ const digest = match?.[2];
20424
+ if (!timestamp || !digest || Math.abs(now - Number(timestamp) * 1e3) > 3e5 || body.length > 262144) {
20425
+ throw new Error("Invalid or expired payment signature.");
20426
+ }
20427
+ const encoder = new TextEncoder();
20428
+ const key = await crypto.subtle.importKey("raw", encoder.encode(token), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
20429
+ const bytes = Uint8Array.from(digest.match(/../g) ?? [], (byte) => Number.parseInt(byte, 16));
20430
+ if (!await crypto.subtle.verify("HMAC", key, bytes, encoder.encode(`${timestamp}.${body}`))) {
20431
+ throw new Error("Invalid payment signature.");
20432
+ }
20433
+ const event = paymentEventSchema.parse(JSON.parse(body));
20434
+ if (event.mode !== mode) {
20435
+ throw new Error("Payment event mode does not match this application.");
20436
+ }
20437
+ return event;
20438
+ }
20439
+ var PaymentRuntime = class {
20440
+ constructor(client) {
20441
+ this.client = client;
20442
+ }
20443
+ async checkout(input) {
20444
+ return transactionSchema.parse((await this.client.post("/platform/payments/runtime/checkouts", input)).data);
20445
+ }
20446
+ async transaction(id) {
20447
+ return transactionSchema.parse(
20448
+ (await this.client.get(`/platform/payments/runtime/transactions/${encodeURIComponent(id)}`)).data
20449
+ );
20450
+ }
20451
+ async subscribe(input) {
20452
+ return subscriptionSchema.parse((await this.client.post("/platform/payments/runtime/subscriptions", input)).data);
20453
+ }
20454
+ async subscription(id) {
20455
+ return subscriptionSchema.parse(
20456
+ (await this.client.get(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}`)).data
20457
+ );
20458
+ }
20459
+ async cancelSubscription(id) {
20460
+ return subscriptionSchema.parse(
20461
+ (await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data
20462
+ );
20463
+ }
20464
+ async subscriptionPortal(id, input) {
20465
+ return portalSchema.parse(
20466
+ (await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/portal`, input)).data
20467
+ );
20468
+ }
20469
+ async prices() {
20470
+ return external_exports.array(priceSchema).parse((await this.client.get("/platform/payments/runtime/prices")).data);
20471
+ }
20472
+ };
20473
+
20162
20474
  // src/index.ts
20163
20475
  function normalizeNotebookSecretInputs(input) {
20164
20476
  if (Array.isArray(input)) {
@@ -20209,6 +20521,7 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20209
20521
  this.notebook = new NotebookApi(this);
20210
20522
  this.servers = new ServerApi(this);
20211
20523
  this.integrations = new IntegrationApi(this);
20524
+ this.payments = new PaymentsApi(this);
20212
20525
  this.options = { ...options };
20213
20526
  this.runtimeTransportValue = runtimeTransport;
20214
20527
  this.baseUrl = normalizeApiBaseUrl(url2);
@@ -20289,7 +20602,7 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20289
20602
  })
20290
20603
  );
20291
20604
  if (!response.ok) {
20292
- throw await responseError(response);
20605
+ throw await responseError(response, path.startsWith("/platform/") ? "platform" : "core");
20293
20606
  }
20294
20607
  if (response.status === 204) {
20295
20608
  return { data: void 0 };
@@ -20322,10 +20635,10 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20322
20635
  }
20323
20636
  }
20324
20637
  };
20325
- async function responseError(response) {
20638
+ async function responseError(response, source = "core") {
20326
20639
  const body = await response.text();
20327
20640
  try {
20328
- return remoteError("core", errorPayload(body === "" ? void 0 : JSON.parse(body)), response);
20641
+ return remoteError(source, errorPayload(body === "" ? void 0 : JSON.parse(body)), response);
20329
20642
  } catch {
20330
20643
  return new TransportError(
20331
20644
  `PHPSandbox API returned an invalid error response (HTTP ${response.status}).`,
@@ -20349,6 +20662,7 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20349
20662
  this.publications = client.publications;
20350
20663
  this.servers = client.servers;
20351
20664
  this.integrations = client.integrations;
20665
+ this.payments = client.payments;
20352
20666
  }
20353
20667
  async whoami() {
20354
20668
  const { data } = await this.client.get("/auth/whoami");
@@ -20415,6 +20729,7 @@ ${JSON.stringify(metadata, null, 2)}` : "";
20415
20729
  this.config = new Config(this);
20416
20730
  this.secrets = new NotebookSecrets(client, this.data.id);
20417
20731
  this.integrations = new SandboxIntegrationApi(this.data.id, client);
20732
+ this.payments = new NotebookPayments(this.data.id, client);
20418
20733
  this.preview = new NotebookPreview(client, this.data.id);
20419
20734
  this.mail = new NotebookMail(client, this.data.id);
20420
20735
  this.feedback = new Feedback(client, this.data.id);