@phpsandbox/sdk 0.3.5 → 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.
@@ -20152,6 +20152,280 @@ function sandboxIntegrationId(integration) {
20152
20152
  return typeof integration === "string" ? integration : integration.id;
20153
20153
  }
20154
20154
 
20155
+ // src/payments.ts
20156
+ var DEFAULT_PAYMENT_CALLBACK_PATH = "/api/phpsandbox-payments/events";
20157
+ var paymentModeSchema = external_exports.enum(["test", "live"]);
20158
+ var accountSchema = external_exports.object({
20159
+ id: external_exports.string(),
20160
+ mode: paymentModeSchema,
20161
+ chargesEnabled: external_exports.boolean(),
20162
+ payoutsEnabled: external_exports.boolean(),
20163
+ detailsSubmitted: external_exports.boolean(),
20164
+ requirements: external_exports.array(external_exports.string()),
20165
+ disabledReason: external_exports.string().nullable()
20166
+ });
20167
+ var accessSchema = external_exports.object({
20168
+ subscribed: external_exports.boolean(),
20169
+ liveAllowed: external_exports.boolean(),
20170
+ testConfigured: external_exports.boolean(),
20171
+ liveConfigured: external_exports.boolean(),
20172
+ feeBasisPoints: external_exports.number().int().min(0).max(1e4).nullable(),
20173
+ canRead: external_exports.boolean(),
20174
+ canManage: external_exports.boolean(),
20175
+ canRefund: external_exports.boolean(),
20176
+ recoveryEndsAt: external_exports.string().nullable().default(null)
20177
+ });
20178
+ var applicationsSchema = external_exports.object({
20179
+ notebookId: external_exports.string(),
20180
+ applications: external_exports.array(
20181
+ external_exports.object({
20182
+ id: external_exports.string(),
20183
+ url: external_exports.string().nullable(),
20184
+ healthy: external_exports.boolean(),
20185
+ passwordProtected: external_exports.boolean(),
20186
+ deployedAt: external_exports.string().datetime().nullable()
20187
+ })
20188
+ )
20189
+ });
20190
+ var bindingSchema = external_exports.object({
20191
+ id: external_exports.string(),
20192
+ notebookId: external_exports.string(),
20193
+ applicationId: external_exports.string().nullable(),
20194
+ mode: paymentModeSchema,
20195
+ revision: external_exports.number().int(),
20196
+ accountId: external_exports.string(),
20197
+ enabled: external_exports.boolean(),
20198
+ callbackUrl: external_exports.string().nullable(),
20199
+ originUrl: external_exports.string().nullable(),
20200
+ runtimeConfigured: external_exports.boolean()
20201
+ });
20202
+ var priceSchema = external_exports.object({
20203
+ billingInterval: external_exports.enum(["month", "year"]).nullable().default(null),
20204
+ id: external_exports.string(),
20205
+ name: external_exports.string(),
20206
+ amount: external_exports.number().int(),
20207
+ currency: external_exports.string(),
20208
+ bindingId: external_exports.string()
20209
+ });
20210
+ var transactionSchema = external_exports.object({
20211
+ subscriptionId: external_exports.string().nullable().default(null),
20212
+ periodStart: external_exports.string().datetime({ offset: true }).nullable().default(null),
20213
+ periodEnd: external_exports.string().datetime({ offset: true }).nullable().default(null),
20214
+ id: external_exports.string(),
20215
+ bindingId: external_exports.string(),
20216
+ orderId: external_exports.string(),
20217
+ priceId: external_exports.string(),
20218
+ amount: external_exports.number().int(),
20219
+ currency: external_exports.string(),
20220
+ feeAmount: external_exports.number().int(),
20221
+ revision: external_exports.number().int(),
20222
+ refundedAmount: external_exports.number().int(),
20223
+ status: external_exports.enum(["pending", "paid", "failed", "refunded", "partially_refunded"]),
20224
+ checkoutUrl: external_exports.string().nullable(),
20225
+ deliveredAt: external_exports.string().nullable()
20226
+ });
20227
+ var subscriptionSchema = external_exports.object({
20228
+ id: external_exports.string(),
20229
+ bindingId: external_exports.string(),
20230
+ priceId: external_exports.string(),
20231
+ orderId: external_exports.string(),
20232
+ status: external_exports.enum([
20233
+ "pending",
20234
+ "incomplete",
20235
+ "incomplete_expired",
20236
+ "trialing",
20237
+ "active",
20238
+ "past_due",
20239
+ "canceled",
20240
+ "unpaid",
20241
+ "paused"
20242
+ ]),
20243
+ billingInterval: external_exports.enum(["month", "year"]),
20244
+ paidThrough: external_exports.string().datetime({ offset: true }).nullable(),
20245
+ cancelAtPeriodEnd: external_exports.boolean(),
20246
+ checkoutUrl: external_exports.string().nullable(),
20247
+ revision: external_exports.number().int()
20248
+ });
20249
+ var portalSchema = external_exports.object({ url: external_exports.string().url() });
20250
+ var refundResultSchema = transactionSchema.extend({
20251
+ refund: external_exports.object({
20252
+ id: external_exports.string(),
20253
+ amount: external_exports.number().int().positive(),
20254
+ status: external_exports.enum(["pending", "succeeded", "failed", "canceled", "requires_action"])
20255
+ })
20256
+ });
20257
+ var activationSchema = external_exports.object({
20258
+ revision: external_exports.number().int(),
20259
+ reviewId: external_exports.string(),
20260
+ expiresAt: external_exports.string(),
20261
+ blockers: external_exports.array(external_exports.string()),
20262
+ binding: bindingSchema,
20263
+ account: accountSchema,
20264
+ feeBasisPoints: external_exports.number().int().nullable(),
20265
+ prices: external_exports.array(priceSchema)
20266
+ });
20267
+ var PaymentsApi = class {
20268
+ constructor(client) {
20269
+ this.client = client;
20270
+ this.runtime = new PaymentRuntime(client);
20271
+ }
20272
+ forNotebook(notebookId) {
20273
+ return new NotebookPayments(notebookId, this.client);
20274
+ }
20275
+ async applications(notebookId) {
20276
+ return applicationsSchema.parse(
20277
+ (await this.client.get(`/notebook/${encodeURIComponent(notebookId)}/payments/applications`)).data
20278
+ );
20279
+ }
20280
+ async access() {
20281
+ return accessSchema.parse((await this.client.get("/platform/payments/access")).data);
20282
+ }
20283
+ async accounts() {
20284
+ return external_exports.array(accountSchema).parse((await this.client.get("/platform/payments/accounts")).data);
20285
+ }
20286
+ async createAccount(mode, country) {
20287
+ return accountSchema.parse((await this.client.put(`/platform/payments/accounts/${mode}`, { country })).data);
20288
+ }
20289
+ async onboarding(mode) {
20290
+ return external_exports.object({ url: external_exports.string().url(), expiresAt: external_exports.string() }).parse((await this.client.post(`/platform/payments/accounts/${mode}/onboarding`, {})).data);
20291
+ }
20292
+ };
20293
+ var NotebookPayments = class {
20294
+ constructor(notebookId, client) {
20295
+ this.notebookId = notebookId;
20296
+ this.client = client;
20297
+ }
20298
+ path(mode) {
20299
+ return `/platform/payments/sandboxes/${encodeURIComponent(this.notebookId)}/${mode}`;
20300
+ }
20301
+ async get(mode = "test") {
20302
+ return bindingSchema.nullable().parse((await this.client.get(this.path(mode))).data);
20303
+ }
20304
+ async configure(input, mode = "test") {
20305
+ return bindingSchema.parse((await this.client.put(this.path(mode), input)).data);
20306
+ }
20307
+ async provision(mode = "test") {
20308
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/provision`, {})).data);
20309
+ }
20310
+ async prices(mode = "test") {
20311
+ return external_exports.array(priceSchema).parse((await this.client.get(`${this.path(mode)}/prices`)).data);
20312
+ }
20313
+ async createPrice(input, mode = "test") {
20314
+ return priceSchema.parse((await this.client.post(`${this.path(mode)}/prices`, input)).data);
20315
+ }
20316
+ async prepareActivation(mode = "test") {
20317
+ return activationSchema.parse((await this.client.post(`${this.path(mode)}/activation`, {})).data);
20318
+ }
20319
+ async activate(reviewId, mode = "test") {
20320
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/activate`, { reviewId })).data);
20321
+ }
20322
+ async disable(mode = "test") {
20323
+ return bindingSchema.parse((await this.client.post(`${this.path(mode)}/disable`, {})).data);
20324
+ }
20325
+ async checkout(input, mode = "test") {
20326
+ return transactionSchema.parse((await this.client.post(`${this.path(mode)}/checkouts`, input)).data);
20327
+ }
20328
+ async subscribe(input, mode = "test") {
20329
+ return subscriptionSchema.parse((await this.client.post(`${this.path(mode)}/subscriptions`, input)).data);
20330
+ }
20331
+ async subscriptions(mode = "test") {
20332
+ return external_exports.array(subscriptionSchema).parse((await this.client.get(`${this.path(mode)}/subscriptions`)).data);
20333
+ }
20334
+ async subscription(id, mode = "test") {
20335
+ return subscriptionSchema.parse(
20336
+ (await this.client.get(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}`)).data
20337
+ );
20338
+ }
20339
+ async cancelSubscription(id, mode = "test") {
20340
+ return subscriptionSchema.parse(
20341
+ (await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data
20342
+ );
20343
+ }
20344
+ async subscriptionPortal(id, input, mode = "test") {
20345
+ return portalSchema.parse(
20346
+ (await this.client.post(`${this.path(mode)}/subscriptions/${encodeURIComponent(id)}/portal`, input)).data
20347
+ );
20348
+ }
20349
+ async transactions(mode = "test") {
20350
+ return external_exports.array(transactionSchema).parse((await this.client.get(`${this.path(mode)}/transactions`)).data);
20351
+ }
20352
+ async refund(id, input, mode = "test") {
20353
+ return refundResultSchema.parse(
20354
+ (await this.client.post(`${this.path(mode)}/transactions/${encodeURIComponent(id)}/refund`, input)).data
20355
+ );
20356
+ }
20357
+ };
20358
+ var paymentEventSchema = external_exports.discriminatedUnion("type", [
20359
+ external_exports.object({
20360
+ id: external_exports.string(),
20361
+ type: external_exports.literal("payment.updated"),
20362
+ mode: paymentModeSchema,
20363
+ transaction: transactionSchema
20364
+ }),
20365
+ external_exports.object({
20366
+ id: external_exports.string(),
20367
+ type: external_exports.literal("subscription.updated"),
20368
+ mode: paymentModeSchema,
20369
+ subscription: subscriptionSchema
20370
+ })
20371
+ ]);
20372
+ async function verifyPaymentEvent(body, signature, token, mode, now = Date.now()) {
20373
+ if (!/^hpay_[a-f0-9]{64}$/.test(token)) {
20374
+ throw new Error("Invalid application payment credential.");
20375
+ }
20376
+ const match = /^t=(\d+),v1=([a-f0-9]{64})$/.exec(signature);
20377
+ const timestamp = match?.[1];
20378
+ const digest = match?.[2];
20379
+ if (!timestamp || !digest || Math.abs(now - Number(timestamp) * 1e3) > 3e5 || body.length > 262144) {
20380
+ throw new Error("Invalid or expired payment signature.");
20381
+ }
20382
+ const encoder = new TextEncoder();
20383
+ const key = await crypto.subtle.importKey("raw", encoder.encode(token), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
20384
+ const bytes = Uint8Array.from(digest.match(/../g) ?? [], (byte) => Number.parseInt(byte, 16));
20385
+ if (!await crypto.subtle.verify("HMAC", key, bytes, encoder.encode(`${timestamp}.${body}`))) {
20386
+ throw new Error("Invalid payment signature.");
20387
+ }
20388
+ const event = paymentEventSchema.parse(JSON.parse(body));
20389
+ if (event.mode !== mode) {
20390
+ throw new Error("Payment event mode does not match this application.");
20391
+ }
20392
+ return event;
20393
+ }
20394
+ var PaymentRuntime = class {
20395
+ constructor(client) {
20396
+ this.client = client;
20397
+ }
20398
+ async checkout(input) {
20399
+ return transactionSchema.parse((await this.client.post("/platform/payments/runtime/checkouts", input)).data);
20400
+ }
20401
+ async transaction(id) {
20402
+ return transactionSchema.parse(
20403
+ (await this.client.get(`/platform/payments/runtime/transactions/${encodeURIComponent(id)}`)).data
20404
+ );
20405
+ }
20406
+ async subscribe(input) {
20407
+ return subscriptionSchema.parse((await this.client.post("/platform/payments/runtime/subscriptions", input)).data);
20408
+ }
20409
+ async subscription(id) {
20410
+ return subscriptionSchema.parse(
20411
+ (await this.client.get(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}`)).data
20412
+ );
20413
+ }
20414
+ async cancelSubscription(id) {
20415
+ return subscriptionSchema.parse(
20416
+ (await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/cancel`, {})).data
20417
+ );
20418
+ }
20419
+ async subscriptionPortal(id, input) {
20420
+ return portalSchema.parse(
20421
+ (await this.client.post(`/platform/payments/runtime/subscriptions/${encodeURIComponent(id)}/portal`, input)).data
20422
+ );
20423
+ }
20424
+ async prices() {
20425
+ return external_exports.array(priceSchema).parse((await this.client.get("/platform/payments/runtime/prices")).data);
20426
+ }
20427
+ };
20428
+
20155
20429
  // src/index.ts
20156
20430
  function normalizeNotebookSecretInputs(input) {
20157
20431
  if (Array.isArray(input)) {
@@ -20202,6 +20476,7 @@ var ClientImplementation = class {
20202
20476
  this.notebook = new NotebookApi(this);
20203
20477
  this.servers = new ServerApi(this);
20204
20478
  this.integrations = new IntegrationApi(this);
20479
+ this.payments = new PaymentsApi(this);
20205
20480
  this.options = { ...options };
20206
20481
  this.runtimeTransportValue = runtimeTransport;
20207
20482
  this.baseUrl = normalizeApiBaseUrl(url2);
@@ -20342,6 +20617,7 @@ var PHPSandbox = class _PHPSandbox {
20342
20617
  this.publications = client.publications;
20343
20618
  this.servers = client.servers;
20344
20619
  this.integrations = client.integrations;
20620
+ this.payments = client.payments;
20345
20621
  }
20346
20622
  async whoami() {
20347
20623
  const { data } = await this.client.get("/auth/whoami");
@@ -20408,6 +20684,7 @@ var _NotebookInstance = class _NotebookInstance {
20408
20684
  this.config = new Config(this);
20409
20685
  this.secrets = new NotebookSecrets(client, this.data.id);
20410
20686
  this.integrations = new SandboxIntegrationApi(this.data.id, client);
20687
+ this.payments = new NotebookPayments(this.data.id, client);
20411
20688
  this.preview = new NotebookPreview(client, this.data.id);
20412
20689
  this.mail = new NotebookMail(client, this.data.id);
20413
20690
  this.feedback = new Feedback(client, this.data.id);
@@ -20740,14 +21017,18 @@ function formatQueryString(params) {
20740
21017
  return query === "" ? "" : `?${query}`;
20741
21018
  }
20742
21019
  export {
21020
+ DEFAULT_PAYMENT_CALLBACK_PATH,
20743
21021
  FileChangeFilter,
20744
21022
  FileChangeType,
20745
21023
  FileType,
20746
21024
  LspConnection,
20747
21025
  NotebookInstance,
21026
+ NotebookPayments,
20748
21027
  NotebookPublication,
20749
21028
  PHPSandbox,
20750
21029
  PHPSandboxError,
21030
+ PaymentRuntime,
21031
+ PaymentsApi,
20751
21032
  PublicationApi,
20752
21033
  PublicationDomains,
20753
21034
  PublicationInstance,
@@ -20759,7 +21040,9 @@ export {
20759
21040
  laravelCloudSetupForRepublish,
20760
21041
  notebookBuiltinServices,
20761
21042
  notebookKnownServices,
20762
- readPublicationStream
21043
+ paymentModeSchema,
21044
+ readPublicationStream,
21045
+ verifyPaymentEvent
20763
21046
  };
20764
21047
  /*! Bundled license information:
20765
21048