@base44-preview/sdk 0.8.63-pr.392.27e9c33 → 0.8.63-pr.392.a02dae3
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.
|
@@ -33,7 +33,7 @@ export declare const loadScript: LoadScript;
|
|
|
33
33
|
* Fetches the payments config and loads the provider's script ahead of mountCheckout().
|
|
34
34
|
* @internal
|
|
35
35
|
*/
|
|
36
|
-
export declare function preloadCheckout(provider: "paypal" | "stripe", deps: Pick<MountCheckoutDeps, "getConfig" | "loadScript">): Promise<void>;
|
|
36
|
+
export declare function preloadCheckout(provider: "paypal" | "stripe" | "wix", deps: Pick<MountCheckoutDeps, "getConfig" | "loadScript">): Promise<void>;
|
|
37
37
|
/**
|
|
38
38
|
* Mounts the embedded checkout that createCheckout() returned.
|
|
39
39
|
* @internal
|
|
@@ -301,8 +301,6 @@ async function mountWix(element, scriptUrl, checkoutId, clientId, options, deps)
|
|
|
301
301
|
let paying = false;
|
|
302
302
|
// Set once a payment succeeds or the element rejects one: either way it refuses another.
|
|
303
303
|
let finished = false;
|
|
304
|
-
// What the app last passed to setDisabled().
|
|
305
|
-
let disabled = false;
|
|
306
304
|
const notify = (callback) => {
|
|
307
305
|
if (destroyed)
|
|
308
306
|
return;
|
|
@@ -313,21 +311,14 @@ async function mountWix(element, scriptUrl, checkoutId, clientId, options, deps)
|
|
|
313
311
|
console.error("[Base44 SDK] A mountCheckout() callback threw", error);
|
|
314
312
|
}
|
|
315
313
|
};
|
|
316
|
-
const button = document.createElement("button");
|
|
317
|
-
button.type = "button";
|
|
318
|
-
button.className = "base44-checkout-pay";
|
|
319
|
-
button.textContent = "Pay";
|
|
320
|
-
// Off until the element can take the payment.
|
|
321
|
-
button.disabled = true;
|
|
322
314
|
paymentElement.on("ready", () => {
|
|
323
315
|
ready = true;
|
|
324
|
-
|
|
325
|
-
button.disabled = disabled;
|
|
316
|
+
notify(() => { var _a; return (_a = options.onReadyChange) === null || _a === void 0 ? void 0 : _a.call(options, true); });
|
|
326
317
|
});
|
|
327
318
|
// The element fires ready again once it recovers.
|
|
328
319
|
paymentElement.on("loaderror", () => {
|
|
329
320
|
ready = false;
|
|
330
|
-
|
|
321
|
+
notify(() => { var _a; return (_a = options.onReadyChange) === null || _a === void 0 ? void 0 : _a.call(options, false); });
|
|
331
322
|
});
|
|
332
323
|
const box = document.createElement("div");
|
|
333
324
|
element.appendChild(box);
|
|
@@ -339,49 +330,40 @@ async function mountWix(element, scriptUrl, checkoutId, clientId, options, deps)
|
|
|
339
330
|
paymentElement.destroy();
|
|
340
331
|
throw error;
|
|
341
332
|
}
|
|
342
|
-
const onClick = () => {
|
|
343
|
-
if (!ready || paying || finished || disabled)
|
|
344
|
-
return;
|
|
345
|
-
paying = true;
|
|
346
|
-
button.disabled = true;
|
|
347
|
-
// The executor runs now, so confirmPayment() is called inside the click, before any await.
|
|
348
|
-
new Promise((resolve) => resolve(paymentElement.confirmPayment()))
|
|
349
|
-
.then(async ({ status }) => {
|
|
350
|
-
if (status !== "succeeded") {
|
|
351
|
-
notify(() => { var _a; return (_a = options.onCancel) === null || _a === void 0 ? void 0 : _a.call(options); });
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
finished = true;
|
|
355
|
-
// Runs even after destroy(), so a paid order is still completed.
|
|
356
|
-
const result = await deps.completeCheckout(checkoutId);
|
|
357
|
-
notify(() => { var _a; return (_a = options.onComplete) === null || _a === void 0 ? void 0 : _a.call(options, result); });
|
|
358
|
-
})
|
|
359
|
-
.catch((error) => {
|
|
360
|
-
finished = true;
|
|
361
|
-
notify(() => { var _a; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error); });
|
|
362
|
-
})
|
|
363
|
-
.finally(() => {
|
|
364
|
-
paying = false;
|
|
365
|
-
button.disabled = !ready || finished || disabled;
|
|
366
|
-
});
|
|
367
|
-
};
|
|
368
|
-
button.addEventListener("click", onClick);
|
|
369
|
-
element.appendChild(button);
|
|
370
333
|
return {
|
|
334
|
+
pay() {
|
|
335
|
+
if (destroyed || !ready || paying || finished)
|
|
336
|
+
return Promise.resolve();
|
|
337
|
+
paying = true;
|
|
338
|
+
// The executor runs now, so confirmPayment() is called inside the app's click, before any await.
|
|
339
|
+
return new Promise((resolve) => resolve(paymentElement.confirmPayment()))
|
|
340
|
+
.then(async ({ status }) => {
|
|
341
|
+
if (status !== "succeeded") {
|
|
342
|
+
notify(() => { var _a; return (_a = options.onCancel) === null || _a === void 0 ? void 0 : _a.call(options); });
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
finished = true;
|
|
346
|
+
// Runs even after destroy(), so a paid order is still completed.
|
|
347
|
+
const result = await deps.completeCheckout(checkoutId);
|
|
348
|
+
notify(() => { var _a; return (_a = options.onComplete) === null || _a === void 0 ? void 0 : _a.call(options, result); });
|
|
349
|
+
})
|
|
350
|
+
.catch((error) => {
|
|
351
|
+
finished = true;
|
|
352
|
+
notify(() => { var _a; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error); });
|
|
353
|
+
})
|
|
354
|
+
.finally(() => {
|
|
355
|
+
paying = false;
|
|
356
|
+
});
|
|
357
|
+
},
|
|
371
358
|
destroy() {
|
|
372
359
|
if (destroyed)
|
|
373
360
|
return;
|
|
374
361
|
destroyed = true;
|
|
375
|
-
button.removeEventListener("click", onClick);
|
|
376
|
-
button.remove();
|
|
377
362
|
box.remove();
|
|
378
363
|
paymentElement.destroy();
|
|
379
364
|
},
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (ready && !paying && !finished)
|
|
383
|
-
button.disabled = value;
|
|
384
|
-
},
|
|
365
|
+
// The app owns the Pay button, so it disables its own.
|
|
366
|
+
setDisabled: () => undefined,
|
|
385
367
|
};
|
|
386
368
|
}
|
|
387
369
|
/** The new checkout's id, refused unless it is the checkout the button was drawn for. */
|
|
@@ -406,7 +388,7 @@ function onClickCheckoutId(spec, button) {
|
|
|
406
388
|
export async function preloadCheckout(provider, deps) {
|
|
407
389
|
if (typeof window === "undefined" || typeof document === "undefined")
|
|
408
390
|
return;
|
|
409
|
-
const read = () => (
|
|
391
|
+
const read = () => ({ paypal: browserGlobals().paypal, stripe: browserGlobals().Stripe, wix: browserGlobals().wpe })[provider];
|
|
410
392
|
try {
|
|
411
393
|
if (!read()) {
|
|
412
394
|
await deps.loadScript(await scriptUrlFor(provider, deps));
|
|
@@ -227,6 +227,10 @@ export interface Purchase {
|
|
|
227
227
|
metadata?: Record<string, string> | null;
|
|
228
228
|
/** ID of the subscription, for recurring purchases. */
|
|
229
229
|
subscription_id?: string | null;
|
|
230
|
+
/** Current state of the subscription, for Base44 Payments subscription purchases: `active`, `paused` or `canceled`. `null` while the provider retries a renewal, or when the state couldn't be read. */
|
|
231
|
+
subscription_status?: "active" | "paused" | "canceled" | null;
|
|
232
|
+
/** When the paid period ends, as an ISO 8601 date, for an `active` Base44 Payments subscription. After a cancel at period end it's when access ends. `null` otherwise. */
|
|
233
|
+
paid_until?: string | null;
|
|
230
234
|
/** Where the order ships, whether your app or the provider collected the address. Always `null` unless you call with the service role. Save it on your order record. */
|
|
231
235
|
shipping_address?: ShippingAddress | null;
|
|
232
236
|
/** Shipping fee in minor units, when the checkout charged one. */
|
|
@@ -330,14 +334,18 @@ export interface MountCheckoutOptions {
|
|
|
330
334
|
* Stripe doesn't call this. After a Stripe payment, the buyer returns to the checkout's `success_path` with a `session_id` query parameter. That parameter is the `checkout_id`. Show the result there with [`listPurchases()`](#listpurchases).
|
|
331
335
|
*/
|
|
332
336
|
onComplete?: (result: CompleteCheckoutResult) => void;
|
|
333
|
-
/** Called when the buyer closes the PayPal window without paying, or when a Base44 Payments payment doesn't go through, a declined one included. The button stays mounted
|
|
337
|
+
/** Called when the buyer closes the PayPal window without paying, or when a Base44 Payments payment doesn't go through, a declined one included. The buyer can try again: PayPal's button stays mounted, and for Base44 Payments call `pay()` again. */
|
|
334
338
|
onCancel?: () => void;
|
|
335
339
|
/**
|
|
336
|
-
* Called when a PayPal or Base44 Payments checkout fails after it's mounted. PayPal's button stays mounted so the buyer can try again.
|
|
340
|
+
* Called when a PayPal or Base44 Payments checkout fails after it's mounted. PayPal's button stays mounted so the buyer can try again. For Base44 Payments, `pay()` does nothing after this, since the payment element refuses another payment, so create a new checkout and mount it. For PayPal this includes a declined payment with the [`payment_declined`](#paymentserrorcode) code. If it keeps failing with `payment_declined`, create a new checkout and mount it. A declined Base44 Payments payment calls `onCancel` instead. Stripe shows its own errors inside the checkout.
|
|
337
341
|
*
|
|
338
342
|
* The error is a `Base44Error` when completing the checkout fails, or the provider's own error object when the PayPal window or the Wix Payment Element fails.
|
|
339
343
|
*/
|
|
340
344
|
onError?: (error: unknown) => void;
|
|
345
|
+
/**
|
|
346
|
+
* Base44 Payments only. Called with `true` when the payment element can take a payment, and with `false` if it stops loading. It can be called again with `true` once the element recovers. Enable your Pay button only while it's ready.
|
|
347
|
+
*/
|
|
348
|
+
onReadyChange?: (ready: boolean) => void;
|
|
341
349
|
}
|
|
342
350
|
/**
|
|
343
351
|
* An element on the page to render the checkout in, such as the result of `document.getElementById()`.
|
|
@@ -380,8 +388,16 @@ export interface PayPalCheckoutOnClick {
|
|
|
380
388
|
export interface CheckoutMount {
|
|
381
389
|
/** Removes the checkout from the page. Call it when the component that holds it unmounts. For Stripe, mounting another checkout destroys this one for you. */
|
|
382
390
|
destroy: () => void;
|
|
383
|
-
/** Grays out the PayPal
|
|
391
|
+
/** Grays out the PayPal button and ignores clicks, for example until a required form is filled in. After a payment completes the button stays off, so `setDisabled(false)` can't start a second payment. Stripe and Base44 Payments ignore it: Base44 Payments uses your own button, so disable that instead. */
|
|
384
392
|
setDisabled: (disabled: boolean) => void;
|
|
393
|
+
/**
|
|
394
|
+
* Base44 Payments only. Pays with what the buyer entered in the payment element. Call it from your own Pay button's click handler, synchronously and before any `await`, so the payment can open any window it needs.
|
|
395
|
+
*
|
|
396
|
+
* When the payment succeeds, the SDK calls {@linkcode PaymentsModule.completeCheckout | completeCheckout()} and then `onComplete`. Otherwise it calls `onCancel` or `onError`. It does nothing while the element isn't ready, while a payment is in progress, or after a payment succeeded or failed, so a second click can't charge twice.
|
|
397
|
+
*
|
|
398
|
+
* @returns Promise resolving once the payment and its callbacks finish. It never rejects.
|
|
399
|
+
*/
|
|
400
|
+
pay?: () => Promise<void>;
|
|
385
401
|
}
|
|
386
402
|
/**
|
|
387
403
|
* Payments module for accepting one-time payments and subscriptions in your app through its connected payment provider.
|
|
@@ -401,9 +417,9 @@ export interface CheckoutMount {
|
|
|
401
417
|
* | :- | :- | :- | :- |
|
|
402
418
|
* | Redirect checkout | Yes | No | Yes |
|
|
403
419
|
* | Embedded checkout | Yes | Yes, required | Yes |
|
|
404
|
-
* | Refunds | Yes | Yes |
|
|
405
|
-
* | Subscription cancellation | Yes, now or at period end | Yes, now only |
|
|
406
|
-
* | Subscription pause and resume | Yes | Yes |
|
|
420
|
+
* | Refunds | Yes | Yes | Yes |
|
|
421
|
+
* | Subscription cancellation | Yes, now or at period end | Yes, now only | Yes, now or at period end |
|
|
422
|
+
* | Subscription pause and resume | Yes | Yes | Yes |
|
|
407
423
|
*
|
|
408
424
|
* ## Authentication modes
|
|
409
425
|
*
|
|
@@ -417,13 +433,13 @@ export interface CheckoutMount {
|
|
|
417
433
|
*/
|
|
418
434
|
export interface PaymentsModule {
|
|
419
435
|
/**
|
|
420
|
-
* Starts downloading the provider's JavaScript library, Stripe.js
|
|
436
|
+
* Starts downloading the provider's JavaScript library, Stripe.js, the PayPal JavaScript SDK or the Wix Payment Element, before you mount a checkout, so the checkout appears faster.
|
|
421
437
|
*
|
|
422
438
|
* Call it when the checkout page or form opens, while the buyer is still filling it in. {@linkcode mountCheckout | mountCheckout()} needs the library to render the checkout, and downloads it itself if you didn't preload it.
|
|
423
439
|
*
|
|
424
440
|
* It never rejects, so you don't need to await it or catch errors. If the library can't load, `mountCheckout()` tries again and rejects with the error. Loading the library requires a browser environment and is skipped in the backend.
|
|
425
441
|
*
|
|
426
|
-
* @param provider - The provider the app is connected to.
|
|
442
|
+
* @param provider - The provider the app is connected to. Use `'wix'` for Base44 Payments.
|
|
427
443
|
* @returns Promise resolving once the library has loaded or failed to load.
|
|
428
444
|
*
|
|
429
445
|
* @example
|
|
@@ -432,7 +448,7 @@ export interface PaymentsModule {
|
|
|
432
448
|
* base44.payments.preloadCheckout('paypal');
|
|
433
449
|
* ```
|
|
434
450
|
*/
|
|
435
|
-
preloadCheckout(provider: "paypal" | "stripe"): Promise<void>;
|
|
451
|
+
preloadCheckout(provider: "paypal" | "stripe" | "wix"): Promise<void>;
|
|
436
452
|
/**
|
|
437
453
|
* Renders an embedded checkout inside an element on the page.
|
|
438
454
|
*
|
|
@@ -441,9 +457,9 @@ export interface PaymentsModule {
|
|
|
441
457
|
* The SDK loads the provider's library and renders the checkout for you:
|
|
442
458
|
* - PayPal: Renders a PayPal button. When the buyer approves the payment, the SDK calls {@linkcode completeCheckout | completeCheckout()} and then `onComplete`.
|
|
443
459
|
* - Stripe: Mounts Stripe Embedded Checkout. After paying, the buyer returns to the checkout's `success_path`. Stripe allows one embedded checkout per page, so mounting another Stripe checkout destroys the previous one.
|
|
444
|
-
* - Base44 Payments: Renders the Wix Payment Element
|
|
460
|
+
* - Base44 Payments: Renders the Wix Payment Element, with no button of its own. Add your own Pay button, enable it while `onReadyChange` reports the element is ready, and call the mount's `pay()` from its click handler. When the payment succeeds, the SDK calls {@linkcode completeCheckout | completeCheckout()} and then `onComplete`.
|
|
445
461
|
*
|
|
446
|
-
*
|
|
462
|
+
* To make the checkout appear faster, load the library ahead of time with [`preloadCheckout()`](#preloadcheckout) when the page opens.
|
|
447
463
|
*
|
|
448
464
|
* Requires a browser environment and can't be used in the backend. Rejects when the checkout isn't embedded, when an on-click PayPal checkout is mounted before the app's PayPal can take payments, when the payments config can't be fetched or has no valid script URL for the provider, when the provider's library fails to load, or when PayPal isn't available for the checkout's currency.
|
|
449
465
|
*
|
|
@@ -493,6 +509,27 @@ export interface PaymentsModule {
|
|
|
493
509
|
* ```
|
|
494
510
|
*
|
|
495
511
|
* @example
|
|
512
|
+
* ```typescript
|
|
513
|
+
* // Base44 Payments with your own Pay button
|
|
514
|
+
* const payButton = document.getElementById('pay') as HTMLButtonElement;
|
|
515
|
+
* payButton.disabled = true;
|
|
516
|
+
* const mount = await base44.payments.mountCheckout(
|
|
517
|
+
* document.getElementById('checkout')!,
|
|
518
|
+
* checkout,
|
|
519
|
+
* {
|
|
520
|
+
* onReadyChange: (ready) => (payButton.disabled = !ready),
|
|
521
|
+
* onComplete: () => navigate('/thank-you'),
|
|
522
|
+
* onCancel: () => (payButton.disabled = false),
|
|
523
|
+
* }
|
|
524
|
+
* );
|
|
525
|
+
*
|
|
526
|
+
* payButton.addEventListener('click', () => {
|
|
527
|
+
* payButton.disabled = true;
|
|
528
|
+
* mount.pay?.();
|
|
529
|
+
* });
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
496
533
|
* ```tsx
|
|
497
534
|
* // React component
|
|
498
535
|
* function Checkout({ checkout }: { checkout: CreateCheckoutResult }) {
|
|
@@ -715,8 +752,6 @@ export interface ServiceRolePaymentsModule {
|
|
|
715
752
|
*
|
|
716
753
|
* Retrying with the same `idempotency_key` and `amount` returns the original refund instead of refunding twice. When the call rejects with [`refund_failed`](#paymentserrorcode), the outcome is unknown, so retry it with the same values.
|
|
717
754
|
*
|
|
718
|
-
* Not supported for Base44 Payments.
|
|
719
|
-
*
|
|
720
755
|
* @param params - The payment, amount, and idempotency key.
|
|
721
756
|
* @returns Promise resolving to the refund.
|
|
722
757
|
*
|
|
@@ -737,7 +772,7 @@ export interface ServiceRolePaymentsModule {
|
|
|
737
772
|
/**
|
|
738
773
|
* Cancels a subscription, now or at the end of the current billing period.
|
|
739
774
|
*
|
|
740
|
-
*
|
|
775
|
+
* Stripe and Base44 Payments can cancel now or at the end of the period. PayPal can only cancel immediately, so `at_period_end` isn't supported there.
|
|
741
776
|
*
|
|
742
777
|
* @param subscriptionId - The `subscription_id` of the purchase.
|
|
743
778
|
* @param options - When to cancel.
|
|
@@ -755,8 +790,6 @@ export interface ServiceRolePaymentsModule {
|
|
|
755
790
|
/**
|
|
756
791
|
* Pauses a subscription's billing.
|
|
757
792
|
*
|
|
758
|
-
* Not supported for Base44 Payments.
|
|
759
|
-
*
|
|
760
793
|
* @param subscriptionId - The `subscription_id` of the purchase.
|
|
761
794
|
* @returns Promise resolving to the subscription's state.
|
|
762
795
|
*
|
|
@@ -770,8 +803,6 @@ export interface ServiceRolePaymentsModule {
|
|
|
770
803
|
/**
|
|
771
804
|
* Resumes a paused subscription's billing.
|
|
772
805
|
*
|
|
773
|
-
* Not supported for Base44 Payments.
|
|
774
|
-
*
|
|
775
806
|
* @param subscriptionId - The `subscription_id` of the purchase.
|
|
776
807
|
* @returns Promise resolving to the subscription's state.
|
|
777
808
|
*
|