@base44-preview/sdk 0.8.63-pr.392.a02dae3 → 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,
|
|
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,5 @@
|
|
|
1
|
-
import type { CheckoutElement, CheckoutMount, CompleteCheckoutResult, MountableCheckout, MountCheckoutOptions,
|
|
1
|
+
import type { CheckoutElement, CheckoutMount, CompleteCheckoutResult, MountableCheckout, MountCheckoutOptions, CheckoutOnClick, PayPalEmbeddedCheckout } from "./payments.types.js";
|
|
2
|
+
type ScriptProvider = "paypal" | "stripe" | "paddle";
|
|
2
3
|
/** @internal */
|
|
3
4
|
export type LoadScript = (src: string) => Promise<void>;
|
|
4
5
|
/**
|
|
@@ -13,7 +14,7 @@ export interface PaymentsCheckoutConfig {
|
|
|
13
14
|
stripe?: {
|
|
14
15
|
script_url?: string;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
+
paddle?: {
|
|
17
18
|
script_url?: string;
|
|
18
19
|
};
|
|
19
20
|
}
|
|
@@ -33,10 +34,10 @@ export declare const loadScript: LoadScript;
|
|
|
33
34
|
* Fetches the payments config and loads the provider's script ahead of mountCheckout().
|
|
34
35
|
* @internal
|
|
35
36
|
*/
|
|
36
|
-
export declare function preloadCheckout(provider:
|
|
37
|
+
export declare function preloadCheckout(provider: ScriptProvider, deps: Pick<MountCheckoutDeps, "getConfig" | "loadScript">): Promise<void>;
|
|
37
38
|
/**
|
|
38
39
|
* Mounts the embedded checkout that createCheckout() returned.
|
|
39
40
|
* @internal
|
|
40
41
|
*/
|
|
41
|
-
export declare function mountCheckout(element: CheckoutElement, checkout: MountableCheckout |
|
|
42
|
+
export declare function mountCheckout(element: CheckoutElement, checkout: MountableCheckout | CheckoutOnClick, options: MountCheckoutOptions, deps: MountCheckoutDeps): Promise<CheckoutMount>;
|
|
42
43
|
export {};
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
const ALLOWED_SCRIPT_HOSTS = {
|
|
3
3
|
paypal: ["www.paypal.com", "www.sandbox.paypal.com"],
|
|
4
4
|
stripe: ["js.stripe.com"],
|
|
5
|
-
|
|
5
|
+
paddle: ["cdn.paddle.com"],
|
|
6
6
|
};
|
|
7
|
+
const PROVIDER_NAMES = { paypal: "PayPal", stripe: "Stripe", paddle: "Paddle" };
|
|
7
8
|
async function scriptUrlFor(provider, deps) {
|
|
8
9
|
var _a, _b;
|
|
9
10
|
const value = (_b = (_a = (await deps.getConfig())) === null || _a === void 0 ? void 0 : _a[provider]) === null || _b === void 0 ? void 0 : _b.script_url;
|
|
@@ -20,8 +21,7 @@ async function scriptUrlFor(provider, deps) {
|
|
|
20
21
|
url.username !== "" ||
|
|
21
22
|
url.password !== "" ||
|
|
22
23
|
!ALLOWED_SCRIPT_HOSTS[provider].includes(url.hostname)) {
|
|
23
|
-
|
|
24
|
-
throw new Error(`The payments config has no valid ${name} script URL`);
|
|
24
|
+
throw new Error(`The payments config has no valid ${PROVIDER_NAMES[provider]} script URL`);
|
|
25
25
|
}
|
|
26
26
|
return url.href;
|
|
27
27
|
}
|
|
@@ -237,70 +237,35 @@ checkout, options, deps) {
|
|
|
237
237
|
},
|
|
238
238
|
};
|
|
239
239
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
});
|
|
251
|
-
if (!response.ok) {
|
|
252
|
-
throw new Error(`The Wix ${body.grantType} token request failed with status ${response.status}`);
|
|
240
|
+
// Paddle.js takes one token per page and reports every checkout's events to the callback set then.
|
|
241
|
+
let paddleToken = null;
|
|
242
|
+
let onPaddleEvent = null;
|
|
243
|
+
/** Sets Paddle.js up for this seller on first use; a page serves one seller's token. */
|
|
244
|
+
function initializePaddle(Paddle, embedded) {
|
|
245
|
+
if (paddleToken === null) {
|
|
246
|
+
if (embedded.environment === "sandbox")
|
|
247
|
+
Paddle.Environment.set("sandbox");
|
|
248
|
+
Paddle.Initialize({ token: embedded.client_token, eventCallback: (event) => onPaddleEvent === null || onPaddleEvent === void 0 ? void 0 : onPaddleEvent(event) });
|
|
249
|
+
paddleToken = embedded.client_token;
|
|
253
250
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
accessToken: data.access_token,
|
|
257
|
-
refreshToken: data.refresh_token,
|
|
258
|
-
expiresAt: Date.now() + data.expires_in * 1000,
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
async function renewWixTokens(clientId, tokens) {
|
|
262
|
-
if (tokens) {
|
|
263
|
-
try {
|
|
264
|
-
return await requestWixTokens({ refresh_token: tokens.refreshToken, grantType: "refresh_token" });
|
|
265
|
-
}
|
|
266
|
-
catch (_a) {
|
|
267
|
-
// The refresh token expired or was revoked, so a new visitor is minted.
|
|
268
|
-
}
|
|
251
|
+
else if (paddleToken !== embedded.client_token) {
|
|
252
|
+
throw new Error("Paddle is already set up on this page for another seller. Reload the page to check out.");
|
|
269
253
|
}
|
|
270
|
-
return requestWixTokens({ clientId, grantType: "anonymous" });
|
|
271
254
|
}
|
|
272
|
-
/**
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (renew && visitor.tokens)
|
|
280
|
-
visitor.tokens = { ...visitor.tokens, expiresAt: 0 };
|
|
281
|
-
const { tokens } = visitor;
|
|
282
|
-
if (tokens && tokens.expiresAt - WIX_TOKEN_MARGIN_MS > Date.now()) {
|
|
283
|
-
return tokens.accessToken;
|
|
284
|
-
}
|
|
285
|
-
(_b = visitor.pending) !== null && _b !== void 0 ? _b : (visitor.pending = renewWixTokens(clientId, tokens)
|
|
286
|
-
.then((renewed) => (visitor.tokens = renewed))
|
|
287
|
-
.finally(() => (visitor.pending = undefined)));
|
|
288
|
-
return (await visitor.pending).accessToken;
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
async function mountWix(element, scriptUrl, checkoutId, clientId, options, deps) {
|
|
292
|
-
var _a;
|
|
293
|
-
const wpe = await loadGlobal(scriptUrl, () => browserGlobals().wpe, deps.loadScript);
|
|
294
|
-
const locale = (((_a = document.documentElement) === null || _a === void 0 ? void 0 : _a.lang) || navigator.language || "en").split("-")[0];
|
|
295
|
-
const getAccessToken = wixVisitorAccessToken(clientId);
|
|
296
|
-
const paymentElement = wpe({ getAccessToken, locale }).create("checkout", {
|
|
297
|
-
intentId: checkoutId,
|
|
298
|
-
});
|
|
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);
|
|
299
262
|
let destroyed = false;
|
|
300
|
-
let
|
|
301
|
-
let
|
|
302
|
-
|
|
303
|
-
|
|
263
|
+
let open = false;
|
|
264
|
+
let starting = false;
|
|
265
|
+
let completing = false;
|
|
266
|
+
// Set once the payment goes through: the transaction is paid, so the button stays off.
|
|
267
|
+
let paid = false;
|
|
268
|
+
let checkoutId;
|
|
304
269
|
const notify = (callback) => {
|
|
305
270
|
if (destroyed)
|
|
306
271
|
return;
|
|
@@ -311,61 +276,91 @@ async function mountWix(element, scriptUrl, checkoutId, clientId, options, deps)
|
|
|
311
276
|
console.error("[Base44 SDK] A mountCheckout() callback threw", error);
|
|
312
277
|
}
|
|
313
278
|
};
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
catch (error) {
|
|
329
|
-
box.remove();
|
|
330
|
-
paymentElement.destroy();
|
|
331
|
-
throw error;
|
|
332
|
-
}
|
|
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;
|
|
279
|
+
const handle = async (event) => {
|
|
280
|
+
if (event.name === "checkout.completed") {
|
|
281
|
+
completing = true;
|
|
282
|
+
button.disabled = true;
|
|
283
|
+
let result;
|
|
284
|
+
try {
|
|
285
|
+
if (!checkoutId)
|
|
286
|
+
throw new Error("Paddle completed a checkout that wasn't created");
|
|
287
|
+
// Paddle charged inside its own window; this asks the backend, which reads Paddle, whether it did.
|
|
288
|
+
result = await deps.completeCheckout(checkoutId);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
completing = false;
|
|
292
|
+
button.disabled = false;
|
|
352
293
|
notify(() => { var _a; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error); });
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
completing = false;
|
|
297
|
+
paid = true;
|
|
298
|
+
notify(() => { var _a; return (_a = options.onComplete) === null || _a === void 0 ? void 0 : _a.call(options, result); });
|
|
299
|
+
}
|
|
300
|
+
else if (event.name === "checkout.closed") {
|
|
301
|
+
open = false;
|
|
302
|
+
if (!paid && !completing)
|
|
303
|
+
notify(() => { var _a; return (_a = options.onCancel) === null || _a === void 0 ? void 0 : _a.call(options); });
|
|
304
|
+
}
|
|
305
|
+
else if (event.name === "checkout.error") {
|
|
306
|
+
notify(() => { var _a, _b; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, (_b = event.error) !== null && _b !== void 0 ? _b : event); });
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
const button = document.createElement("button");
|
|
310
|
+
button.type = "button";
|
|
311
|
+
button.textContent = "Pay";
|
|
312
|
+
button.setAttribute("data-base44-checkout", "paddle");
|
|
313
|
+
const onClick = async () => {
|
|
314
|
+
if (paid || completing || open || starting || button.disabled)
|
|
315
|
+
return;
|
|
316
|
+
starting = true;
|
|
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;
|
|
323
|
+
Paddle.Checkout.open({
|
|
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 } } : {}),
|
|
356
328
|
});
|
|
357
|
-
}
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
open = false;
|
|
332
|
+
notify(() => { var _a; return (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error); });
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
starting = false;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
const listener = () => void onClick();
|
|
339
|
+
button.addEventListener("click", listener);
|
|
340
|
+
element.appendChild(button);
|
|
341
|
+
return {
|
|
358
342
|
destroy() {
|
|
359
343
|
if (destroyed)
|
|
360
344
|
return;
|
|
361
345
|
destroyed = true;
|
|
362
|
-
|
|
363
|
-
|
|
346
|
+
button.removeEventListener("click", listener);
|
|
347
|
+
button.remove();
|
|
348
|
+
if (open)
|
|
349
|
+
Paddle.Checkout.close();
|
|
350
|
+
},
|
|
351
|
+
setDisabled(disabled) {
|
|
352
|
+
if (!completing && !paid)
|
|
353
|
+
button.disabled = disabled;
|
|
364
354
|
},
|
|
365
|
-
// The app owns the Pay button, so it disables its own.
|
|
366
|
-
setDisabled: () => undefined,
|
|
367
355
|
};
|
|
368
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
|
+
}
|
|
369
364
|
/** The new checkout's id, refused unless it is the checkout the button was drawn for. */
|
|
370
365
|
function onClickCheckoutId(spec, button) {
|
|
371
366
|
return async () => {
|
|
@@ -388,7 +383,7 @@ function onClickCheckoutId(spec, button) {
|
|
|
388
383
|
export async function preloadCheckout(provider, deps) {
|
|
389
384
|
if (typeof window === "undefined" || typeof document === "undefined")
|
|
390
385
|
return;
|
|
391
|
-
const read = () =>
|
|
386
|
+
const read = () => provider === "paypal" ? browserGlobals().paypal : provider === "paddle" ? browserGlobals().Paddle : browserGlobals().Stripe;
|
|
392
387
|
try {
|
|
393
388
|
if (!read()) {
|
|
394
389
|
await deps.loadScript(await scriptUrlFor(provider, deps));
|
|
@@ -410,6 +405,12 @@ export async function mountCheckout(element, checkout, options, deps) {
|
|
|
410
405
|
throw new Error("mountCheckout() requires an element to render in");
|
|
411
406
|
}
|
|
412
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
|
+
}
|
|
413
414
|
return mountPayPalOnClick(element, checkout, options, deps);
|
|
414
415
|
}
|
|
415
416
|
const embedded = checkout === null || checkout === void 0 ? void 0 : checkout.embedded;
|
|
@@ -421,11 +422,10 @@ export async function mountCheckout(element, checkout, options, deps) {
|
|
|
421
422
|
return mountStripe(element, await scriptUrlFor("stripe", deps), embedded, deps);
|
|
422
423
|
case "paypal":
|
|
423
424
|
return mountPayPal(element, await scriptUrlFor("paypal", deps), checkout.currency, embedded, checkout.checkout_id, options, deps);
|
|
424
|
-
case "
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
return mountWix(element, await scriptUrlFor("wix", deps), checkout.checkout_id, embedded.client_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
|
+
}
|
|
429
429
|
default:
|
|
430
430
|
throw new Error(`Embedded checkout isn't supported for provider ${embedded.provider}`);
|
|
431
431
|
}
|
|
@@ -111,7 +111,7 @@ export interface CreateCheckoutParams {
|
|
|
111
111
|
metadata?: Record<string, string>;
|
|
112
112
|
/** Key that makes retries return the same checkout instead of creating another, 1 to 200 characters. Not supported for Base44 Payments or PayPal subscriptions. */
|
|
113
113
|
idempotency_key?: string;
|
|
114
|
-
/** How the buyer pays. Defaults to `'redirect'`. PayPal apps must pass `'embedded'`. */
|
|
114
|
+
/** How the buyer pays. Defaults to `'redirect'`. PayPal apps must pass `'embedded'`, and Base44 Payments supports only `'redirect'`. */
|
|
115
115
|
presentation?: CheckoutPresentation;
|
|
116
116
|
/**
|
|
117
117
|
* Shipping address and fee. Allowed only when at least one item is `shippable`. Supported for PayPal one-time checkouts only. Other providers and subscriptions reject it with [`invalid_request`](#paymentserrorcode).
|
|
@@ -147,18 +147,24 @@ export interface PayPalEmbeddedCheckout {
|
|
|
147
147
|
intent: "capture" | "subscription";
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
|
-
* Data that
|
|
150
|
+
* Data that Paddle's checkout needs to open.
|
|
151
151
|
*/
|
|
152
|
-
export interface
|
|
152
|
+
export interface PaddleEmbeddedCheckout {
|
|
153
153
|
/** Payment provider. */
|
|
154
|
-
provider: "
|
|
155
|
-
/**
|
|
156
|
-
|
|
154
|
+
provider: "paddle";
|
|
155
|
+
/** Paddle client-side token of the app's seller account. Public by design. */
|
|
156
|
+
client_token: string;
|
|
157
|
+
/** ID of the Paddle transaction the buyer pays. */
|
|
158
|
+
transaction_id: string;
|
|
159
|
+
/** Paddle environment the transaction lives in. */
|
|
160
|
+
environment: "sandbox" | "production";
|
|
161
|
+
/** Buyer email to prefill, when the checkout named one. */
|
|
162
|
+
customer_email?: string | null;
|
|
157
163
|
}
|
|
158
164
|
/**
|
|
159
165
|
* Data that an embedded checkout needs to render, by provider.
|
|
160
166
|
*/
|
|
161
|
-
export type EmbeddedCheckout = StripeEmbeddedCheckout | PayPalEmbeddedCheckout |
|
|
167
|
+
export type EmbeddedCheckout = StripeEmbeddedCheckout | PayPalEmbeddedCheckout | PaddleEmbeddedCheckout;
|
|
162
168
|
/**
|
|
163
169
|
* Result of [`createCheckout()`](#createcheckout).
|
|
164
170
|
*
|
|
@@ -329,23 +335,19 @@ export type PaymentsErrorCode = "forbidden" | "unauthenticated" | "not_found" |
|
|
|
329
335
|
*/
|
|
330
336
|
export interface MountCheckoutOptions {
|
|
331
337
|
/**
|
|
332
|
-
* Called after the buyer approves a PayPal payment, or pays
|
|
338
|
+
* Called after the buyer approves a PayPal payment, or pays in Paddle's checkout, and the SDK completes the checkout. Use it to update the page, not to grant access.
|
|
333
339
|
*
|
|
334
340
|
* 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).
|
|
335
341
|
*/
|
|
336
342
|
onComplete?: (result: CompleteCheckoutResult) => void;
|
|
337
|
-
/** Called when the buyer closes the PayPal window without paying
|
|
343
|
+
/** Called when the buyer closes the PayPal or Paddle window without paying. The button stays mounted so the buyer can try again. */
|
|
338
344
|
onCancel?: () => void;
|
|
339
345
|
/**
|
|
340
|
-
* Called when a PayPal
|
|
346
|
+
* Called when a PayPal checkout fails after it's mounted, including a declined payment with the [`payment_declined`](#paymentserrorcode) code. The button stays mounted so the buyer can try again. If it keeps failing with `payment_declined`, create a new checkout and mount it. Stripe shows its own errors inside the checkout.
|
|
341
347
|
*
|
|
342
|
-
* The error is a `Base44Error` when completing the checkout fails, or
|
|
348
|
+
* The error is a `Base44Error` when completing the checkout fails, or PayPal's own error object when the PayPal window fails.
|
|
343
349
|
*/
|
|
344
350
|
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;
|
|
349
351
|
}
|
|
350
352
|
/**
|
|
351
353
|
* An element on the page to render the checkout in, such as the result of `document.getElementById()`.
|
|
@@ -382,22 +384,29 @@ export interface PayPalCheckoutOnClick {
|
|
|
382
384
|
*/
|
|
383
385
|
createCheckout: () => Promise<MountableCheckout>;
|
|
384
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;
|
|
385
402
|
/**
|
|
386
403
|
* A mounted embedded checkout.
|
|
387
404
|
*/
|
|
388
405
|
export interface CheckoutMount {
|
|
389
406
|
/** 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. */
|
|
390
407
|
destroy: () => void;
|
|
391
|
-
/** Grays out
|
|
408
|
+
/** Grays out PayPal's or Paddle's 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 ignores it. */
|
|
392
409
|
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>;
|
|
401
410
|
}
|
|
402
411
|
/**
|
|
403
412
|
* Payments module for accepting one-time payments and subscriptions in your app through its connected payment provider.
|
|
@@ -416,7 +425,7 @@ export interface CheckoutMount {
|
|
|
416
425
|
* | | Stripe | PayPal | Base44 Payments |
|
|
417
426
|
* | :- | :- | :- | :- |
|
|
418
427
|
* | Redirect checkout | Yes | No | Yes |
|
|
419
|
-
* | Embedded checkout | Yes | Yes, required |
|
|
428
|
+
* | Embedded checkout | Yes | Yes, required | No |
|
|
420
429
|
* | Refunds | Yes | Yes | Yes |
|
|
421
430
|
* | Subscription cancellation | Yes, now or at period end | Yes, now only | Yes, now or at period end |
|
|
422
431
|
* | Subscription pause and resume | Yes | Yes | Yes |
|
|
@@ -425,7 +434,7 @@ export interface CheckoutMount {
|
|
|
425
434
|
*
|
|
426
435
|
* This module is available to use with a client in all authentication modes:
|
|
427
436
|
*
|
|
428
|
-
* - **Anonymous or user authentication** (`base44.payments`): Preloads and mounts embedded checkouts, completes PayPal
|
|
437
|
+
* - **Anonymous or user authentication** (`base44.payments`): Preloads and mounts embedded checkouts, completes PayPal checkouts, and lists the current user's own purchases.
|
|
429
438
|
* - **Service role authentication** (`base44.asServiceRole.payments`): Creates checkouts, lists any purchase, refunds payments, and changes subscriptions. Use the [service role methods](#service-role-methods) in backend functions.
|
|
430
439
|
*
|
|
431
440
|
* @groupDescription Methods
|
|
@@ -433,13 +442,13 @@ export interface CheckoutMount {
|
|
|
433
442
|
*/
|
|
434
443
|
export interface PaymentsModule {
|
|
435
444
|
/**
|
|
436
|
-
* Starts downloading the provider's JavaScript library, Stripe.js
|
|
445
|
+
* Starts downloading the provider's JavaScript library, Stripe.js or the PayPal JavaScript SDK, before you mount a checkout, so the checkout appears faster.
|
|
437
446
|
*
|
|
438
447
|
* 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.
|
|
439
448
|
*
|
|
440
449
|
* 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.
|
|
441
450
|
*
|
|
442
|
-
* @param provider - The provider the app is connected to.
|
|
451
|
+
* @param provider - The provider the app is connected to.
|
|
443
452
|
* @returns Promise resolving once the library has loaded or failed to load.
|
|
444
453
|
*
|
|
445
454
|
* @example
|
|
@@ -448,16 +457,15 @@ export interface PaymentsModule {
|
|
|
448
457
|
* base44.payments.preloadCheckout('paypal');
|
|
449
458
|
* ```
|
|
450
459
|
*/
|
|
451
|
-
preloadCheckout(provider: "paypal" | "stripe" | "
|
|
460
|
+
preloadCheckout(provider: "paypal" | "stripe" | "paddle"): Promise<void>;
|
|
452
461
|
/**
|
|
453
462
|
* Renders an embedded checkout inside an element on the page.
|
|
454
463
|
*
|
|
455
|
-
* 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.
|
|
456
465
|
*
|
|
457
466
|
* The SDK loads the provider's library and renders the checkout for you:
|
|
458
467
|
* - PayPal: Renders a PayPal button. When the buyer approves the payment, the SDK calls {@linkcode completeCheckout | completeCheckout()} and then `onComplete`.
|
|
459
468
|
* - 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.
|
|
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`.
|
|
461
469
|
*
|
|
462
470
|
* To make the checkout appear faster, load the library ahead of time with [`preloadCheckout()`](#preloadcheckout) when the page opens.
|
|
463
471
|
*
|
|
@@ -466,7 +474,7 @@ export interface PaymentsModule {
|
|
|
466
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.
|
|
467
475
|
*
|
|
468
476
|
* @param element - Element to render the checkout in. It should be empty.
|
|
469
|
-
* @param checkout - The result of {@linkcode ServiceRolePaymentsModule.createCheckout | createCheckout()}, or for PayPal a {@linkcode
|
|
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.
|
|
470
478
|
* @param options - Callbacks for the checkout's outcome.
|
|
471
479
|
* @returns Promise resolving to the mounted checkout once it renders.
|
|
472
480
|
*
|
|
@@ -509,27 +517,6 @@ export interface PaymentsModule {
|
|
|
509
517
|
* ```
|
|
510
518
|
*
|
|
511
519
|
* @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
|
|
533
520
|
* ```tsx
|
|
534
521
|
* // React component
|
|
535
522
|
* function Checkout({ checkout }: { checkout: CreateCheckoutResult }) {
|
|
@@ -552,13 +539,13 @@ export interface PaymentsModule {
|
|
|
552
539
|
* }
|
|
553
540
|
* ```
|
|
554
541
|
*/
|
|
555
|
-
mountCheckout(element: CheckoutElement, checkout: MountableCheckout |
|
|
542
|
+
mountCheckout(element: CheckoutElement, checkout: MountableCheckout | CheckoutOnClick, options?: MountCheckoutOptions): Promise<CheckoutMount>;
|
|
556
543
|
/**
|
|
557
|
-
* Completes a PayPal
|
|
544
|
+
* Completes a PayPal checkout after the buyer approves it.
|
|
558
545
|
*
|
|
559
546
|
* When you use {@linkcode mountCheckout | mountCheckout()}, it calls this for you. Call it directly only if you render your own PayPal button. Calling it again for the same checkout returns the same result.
|
|
560
547
|
*
|
|
561
|
-
* Only PayPal
|
|
548
|
+
* Only PayPal checkouts need completing. Other providers reject with [`complete_not_supported`](#paymentserrorcode).
|
|
562
549
|
*
|
|
563
550
|
* To complete the checkout from a backend function instead, use [`completeCheckout()`](#service-role-methods) with the service role.
|
|
564
551
|
*
|
|
@@ -689,11 +676,11 @@ export interface ServiceRolePaymentsModule {
|
|
|
689
676
|
*/
|
|
690
677
|
createCheckout(params: CreateCheckoutParams): Promise<CreateCheckoutResult>;
|
|
691
678
|
/**
|
|
692
|
-
* Completes a PayPal
|
|
679
|
+
* Completes a PayPal checkout from a backend function, after the buyer approves it.
|
|
693
680
|
*
|
|
694
681
|
* Use it when you render your own PayPal button and its `onApprove` handler sends the checkout ID to your backend. Calling it again for the same checkout returns the same result.
|
|
695
682
|
*
|
|
696
|
-
* Only PayPal
|
|
683
|
+
* Only PayPal checkouts need completing. Other providers reject with [`complete_not_supported`](#paymentserrorcode).
|
|
697
684
|
*
|
|
698
685
|
* @param checkoutId - The `checkout_id` from {@linkcode createCheckout | createCheckout()}.
|
|
699
686
|
* @returns Promise resolving to the checkout's payment status.
|