@base44-preview/sdk 0.8.63-pr.393.32eac0e → 0.8.63-pr.393.4d87834

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ export type { AppModule, AppPublicSettings, AppPublicSettingsResponse, } from ".
15
15
  export type { McpModule, McpConsentAction, McpConsentInfo, McpConsentResponse, McpConsentTool, } from "./modules/mcp.types.js";
16
16
  export type { ActorsModule, ActorClient, ActorRef, Connection, ActorSubscription, ActorConnectOptions, ActorNameRegistry, ActorRegistry, ActorConnectable, ActorMethods, ServiceRoleActorRef, ServiceRoleActorsModule, } from "./modules/actors.types.js";
17
17
  export type { SsoModule, SsoAccessTokenResponse } from "./modules/sso.types.js";
18
- export type { PaymentsModule, ServiceRolePaymentsModule, PaymentsErrorCode, PaymentsRecurringFrequency, CheckoutBuyer, CheckoutFreeTrial, CheckoutItem, CheckoutItemRecurring, CheckoutPresentation, CheckoutShipping, CheckoutElement, CheckoutMount, MountableCheckout, CreateCheckoutParams, CreateCheckoutResult, CompleteCheckoutResult, EmbeddedCheckout, PayPalCheckoutOnClick, PayPalEmbeddedCheckout, StripeEmbeddedCheckout, MountCheckoutOptions, ListPurchasesParams, ServiceRoleListPurchasesParams, ListPurchasesResult, Purchase, PurchaseItem, PurchaseStatus, RefundParams, ShippingAddress, RefundResult, CancelSubscriptionOptions, SubscriptionResult, } from "./modules/payments.types.js";
18
+ export type { PaymentsModule, ServiceRolePaymentsModule, PaymentsErrorCode, PaymentsRecurringFrequency, CheckoutBuyer, CheckoutFreeTrial, CheckoutItem, CheckoutItemRecurring, CheckoutPresentation, CheckoutShipping, CheckoutElement, CheckoutMount, MountableCheckout, CreateCheckoutParams, CreateCheckoutResult, CompleteCheckoutResult, EmbeddedCheckout, PayPalCheckoutOnClick, PaddleCheckoutOnClick, CheckoutOnClick, PayPalEmbeddedCheckout, StripeEmbeddedCheckout, MountCheckoutOptions, ListPurchasesParams, ServiceRoleListPurchasesParams, ListPurchasesResult, Purchase, PurchaseItem, PurchaseStatus, RefundParams, ShippingAddress, RefundResult, CancelSubscriptionOptions, SubscriptionResult, } from "./modules/payments.types.js";
19
19
  export type { BrowserModule, BrowserSession, BrowserSessionOptions, } from "./modules/browser.types.js";
20
20
  export type { VoiceModule, VoiceSession, VoiceSessionMessage, VoiceSessionOptions, } from "./modules/voice.types.js";
21
21
  export { Actor, type Conn, type ActorConnectionIdentity, ActorError, type ActorCallIdentity, type ActorCallContext, } from "./actor.js";
@@ -1,4 +1,4 @@
1
- import type { CheckoutElement, CheckoutMount, CompleteCheckoutResult, MountableCheckout, MountCheckoutOptions, PayPalCheckoutOnClick, PayPalEmbeddedCheckout } from "./payments.types.js";
1
+ import type { CheckoutElement, CheckoutMount, CompleteCheckoutResult, MountableCheckout, MountCheckoutOptions, CheckoutOnClick, PayPalEmbeddedCheckout } from "./payments.types.js";
2
2
  type ScriptProvider = "paypal" | "stripe" | "paddle";
3
3
  /** @internal */
4
4
  export type LoadScript = (src: string) => Promise<void>;
@@ -39,5 +39,5 @@ export declare function preloadCheckout(provider: ScriptProvider, deps: Pick<Mou
39
39
  * Mounts the embedded checkout that createCheckout() returned.
40
40
  * @internal
41
41
  */
42
- export declare function mountCheckout(element: CheckoutElement, checkout: MountableCheckout | PayPalCheckoutOnClick, options: MountCheckoutOptions, deps: MountCheckoutDeps): Promise<CheckoutMount>;
42
+ export declare function mountCheckout(element: CheckoutElement, checkout: MountableCheckout | CheckoutOnClick, options: MountCheckoutOptions, deps: MountCheckoutDeps): Promise<CheckoutMount>;
43
43
  export {};
@@ -240,8 +240,8 @@ checkout, options, deps) {
240
240
  // Paddle.js takes one token per page and reports every checkout's events to the callback set then.
241
241
  let paddleToken = null;
242
242
  let onPaddleEvent = null;
243
- async function mountPaddle(element, scriptUrl, embedded, checkoutId, options, deps) {
244
- const Paddle = await loadGlobal(scriptUrl, () => browserGlobals().Paddle, deps.loadScript);
243
+ /** Sets Paddle.js up for this seller on first use; a page serves one seller's token. */
244
+ function initializePaddle(Paddle, embedded) {
245
245
  if (paddleToken === null) {
246
246
  if (embedded.environment === "sandbox")
247
247
  Paddle.Environment.set("sandbox");
@@ -251,11 +251,21 @@ async function mountPaddle(element, scriptUrl, embedded, checkoutId, options, de
251
251
  else if (paddleToken !== embedded.client_token) {
252
252
  throw new Error("Paddle is already set up on this page for another seller. Reload the page to check out.");
253
253
  }
254
+ }
255
+ /**
256
+ * Draws a pay button that opens Paddle's checkout. `resolve` is the checkout itself, or what an
257
+ * on-click checkout calls inside the click to create it: Paddle's checkout opens in the page, not a
258
+ * popup, so it can open after the checkout is created and the buyer still clicks once.
259
+ */
260
+ async function mountPaddle(element, scriptUrl, resolve, options, deps) {
261
+ const Paddle = await loadGlobal(scriptUrl, () => browserGlobals().Paddle, deps.loadScript);
254
262
  let destroyed = false;
255
263
  let open = false;
264
+ let starting = false;
256
265
  let completing = false;
257
266
  // Set once the payment goes through: the transaction is paid, so the button stays off.
258
267
  let paid = false;
268
+ let checkoutId;
259
269
  const notify = (callback) => {
260
270
  if (destroyed)
261
271
  return;
@@ -272,6 +282,8 @@ async function mountPaddle(element, scriptUrl, embedded, checkoutId, options, de
272
282
  button.disabled = true;
273
283
  let result;
274
284
  try {
285
+ if (!checkoutId)
286
+ throw new Error("Paddle completed a checkout that wasn't created");
275
287
  // Paddle charged inside its own window; this asks the backend, which reads Paddle, whether it did.
276
288
  result = await deps.completeCheckout(checkoutId);
277
289
  }
@@ -298,30 +310,40 @@ async function mountPaddle(element, scriptUrl, embedded, checkoutId, options, de
298
310
  button.type = "button";
299
311
  button.textContent = "Pay";
300
312
  button.setAttribute("data-base44-checkout", "paddle");
301
- const onClick = () => {
302
- if (paid || completing || open || button.disabled)
313
+ const onClick = async () => {
314
+ if (paid || completing || open || starting || button.disabled)
303
315
  return;
304
- open = true;
305
- onPaddleEvent = (event) => void handle(event);
316
+ starting = true;
306
317
  try {
318
+ const target = await resolve();
319
+ initializePaddle(Paddle, target.embedded);
320
+ checkoutId = target.checkoutId;
321
+ onPaddleEvent = (event) => void handle(event);
322
+ open = true;
307
323
  Paddle.Checkout.open({
308
- transactionId: embedded.transaction_id,
309
- ...(embedded.customer_email ? { customer: { email: embedded.customer_email } } : {}),
324
+ transactionId: target.embedded.transaction_id,
325
+ // The app prices discounts itself through createCheckout, so Paddle's own coupon field would be a second one.
326
+ settings: { showAddDiscounts: false },
327
+ ...(target.embedded.customer_email ? { customer: { email: target.embedded.customer_email } } : {}),
310
328
  });
311
329
  }
312
330
  catch (error) {
313
331
  open = false;
314
332
  notify(() => { var _a; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error); });
315
333
  }
334
+ finally {
335
+ starting = false;
336
+ }
316
337
  };
317
- button.addEventListener("click", onClick);
338
+ const listener = () => void onClick();
339
+ button.addEventListener("click", listener);
318
340
  element.appendChild(button);
319
341
  return {
320
342
  destroy() {
321
343
  if (destroyed)
322
344
  return;
323
345
  destroyed = true;
324
- button.removeEventListener("click", onClick);
346
+ button.removeEventListener("click", listener);
325
347
  button.remove();
326
348
  if (open)
327
349
  Paddle.Checkout.close();
@@ -332,6 +354,13 @@ async function mountPaddle(element, scriptUrl, embedded, checkoutId, options, de
332
354
  },
333
355
  };
334
356
  }
357
+ function paddleTarget(checkout) {
358
+ const embedded = checkout === null || checkout === void 0 ? void 0 : checkout.embedded;
359
+ if (!checkout || (embedded === null || embedded === void 0 ? void 0 : embedded.provider) !== "paddle") {
360
+ throw new Error("createCheckout() returned a checkout that isn't an embedded Paddle checkout.");
361
+ }
362
+ return { checkoutId: checkout.checkout_id, embedded };
363
+ }
335
364
  /** The new checkout's id, refused unless it is the checkout the button was drawn for. */
336
365
  function onClickCheckoutId(spec, button) {
337
366
  return async () => {
@@ -376,6 +405,12 @@ export async function mountCheckout(element, checkout, options, deps) {
376
405
  throw new Error("mountCheckout() requires an element to render in");
377
406
  }
378
407
  if ("createCheckout" in checkout) {
408
+ if (checkout.provider === "paddle") {
409
+ if (typeof checkout.createCheckout !== "function")
410
+ throw new Error("A Paddle checkout on click needs createCheckout()");
411
+ const create = checkout.createCheckout;
412
+ return mountPaddle(element, await scriptUrlFor("paddle", deps), async () => paddleTarget(await create()), options, deps);
413
+ }
379
414
  return mountPayPalOnClick(element, checkout, options, deps);
380
415
  }
381
416
  const embedded = checkout === null || checkout === void 0 ? void 0 : checkout.embedded;
@@ -387,8 +422,10 @@ export async function mountCheckout(element, checkout, options, deps) {
387
422
  return mountStripe(element, await scriptUrlFor("stripe", deps), embedded, deps);
388
423
  case "paypal":
389
424
  return mountPayPal(element, await scriptUrlFor("paypal", deps), checkout.currency, embedded, checkout.checkout_id, options, deps);
390
- case "paddle":
391
- return mountPaddle(element, await scriptUrlFor("paddle", deps), embedded, checkout.checkout_id, options, deps);
425
+ case "paddle": {
426
+ const target = paddleTarget(checkout);
427
+ return mountPaddle(element, await scriptUrlFor("paddle", deps), () => Promise.resolve(target), options, deps);
428
+ }
392
429
  default:
393
430
  throw new Error(`Embedded checkout isn't supported for provider ${embedded.provider}`);
394
431
  }
@@ -384,6 +384,21 @@ export interface PayPalCheckoutOnClick {
384
384
  */
385
385
  createCheckout: () => Promise<MountableCheckout>;
386
386
  }
387
+ /**
388
+ * A Paddle checkout that [`mountCheckout()`](#mountcheckout) creates when the buyer clicks Paddle's button, so the buyer clicks once and no checkout is created for visitors who don't buy.
389
+ */
390
+ export interface PaddleCheckoutOnClick {
391
+ /** Payment provider. */
392
+ provider: "paddle";
393
+ /**
394
+ * Creates the checkout when the buyer clicks, usually by calling the backend function that calls [`createCheckout()`](#createcheckout). Resolve with its result, an embedded Paddle checkout. Reject to stop; `onError` receives the error.
395
+ */
396
+ createCheckout: () => Promise<MountableCheckout>;
397
+ }
398
+ /**
399
+ * A checkout that [`mountCheckout()`](#mountcheckout) creates when the buyer clicks: PayPal or Paddle.
400
+ */
401
+ export type CheckoutOnClick = PayPalCheckoutOnClick | PaddleCheckoutOnClick;
387
402
  /**
388
403
  * A mounted embedded checkout.
389
404
  */
@@ -446,7 +461,7 @@ export interface PaymentsModule {
446
461
  /**
447
462
  * Renders an embedded checkout inside an element on the page.
448
463
  *
449
- * Pass the result of a {@linkcode ServiceRolePaymentsModule.createCheckout | createCheckout()} call made with `presentation: 'embedded'`. Because only the service role can create checkouts, create it in a backend function and return the result to the page. For PayPal, you can instead pass a {@linkcode PayPalCheckoutOnClick} to show the button before any checkout exists. The SDK creates the checkout only when the buyer clicks the button, so visitors who don't buy don't leave unused checkouts behind.
464
+ * Pass the result of a {@linkcode ServiceRolePaymentsModule.createCheckout | createCheckout()} call made with `presentation: 'embedded'`. Because only the service role can create checkouts, create it in a backend function and return the result to the page. For PayPal or Paddle, you can instead pass a {@linkcode PayPalCheckoutOnClick} or {@linkcode PaddleCheckoutOnClick} to show the button before any checkout exists. The SDK creates the checkout only when the buyer clicks the button, so visitors who don't buy don't leave unused checkouts behind.
450
465
  *
451
466
  * The SDK loads the provider's library and renders the checkout for you:
452
467
  * - PayPal: Renders a PayPal button. When the buyer approves the payment, the SDK calls {@linkcode completeCheckout | completeCheckout()} and then `onComplete`.
@@ -459,7 +474,7 @@ export interface PaymentsModule {
459
474
  * The checkout's callbacks run in the buyer's browser, where they can be faked. Grant access to what the buyer paid for only after your backend confirms the purchase, for example with [`listPurchases()`](#service-role-methods) in a backend function.
460
475
  *
461
476
  * @param element - Element to render the checkout in. It should be empty.
462
- * @param checkout - The result of {@linkcode ServiceRolePaymentsModule.createCheckout | createCheckout()}, or for PayPal a {@linkcode PayPalCheckoutOnClick} that creates it when the buyer clicks.
477
+ * @param checkout - The result of {@linkcode ServiceRolePaymentsModule.createCheckout | createCheckout()}, or for PayPal or Paddle a {@linkcode CheckoutOnClick} that creates it when the buyer clicks.
463
478
  * @param options - Callbacks for the checkout's outcome.
464
479
  * @returns Promise resolving to the mounted checkout once it renders.
465
480
  *
@@ -524,7 +539,7 @@ export interface PaymentsModule {
524
539
  * }
525
540
  * ```
526
541
  */
527
- mountCheckout(element: CheckoutElement, checkout: MountableCheckout | PayPalCheckoutOnClick, options?: MountCheckoutOptions): Promise<CheckoutMount>;
542
+ mountCheckout(element: CheckoutElement, checkout: MountableCheckout | CheckoutOnClick, options?: MountCheckoutOptions): Promise<CheckoutMount>;
528
543
  /**
529
544
  * Completes a PayPal checkout after the buyer approves it.
530
545
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.63-pr.393.32eac0e",
3
+ "version": "0.8.63-pr.393.4d87834",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",