@paddle/better-auth 0.0.0-stage → 0.1.1-rc.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.
- package/LICENSE +21 -0
- package/README.md +549 -2
- package/dist/client.d.ts +59 -0
- package/dist/client.js +77 -0
- package/dist/index.d.ts +549 -0
- package/dist/index.js +756 -0
- package/dist/react/index.d.ts +30 -0
- package/dist/react/index.js +90 -0
- package/package.json +75 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
interface UsePaddleCheckoutOptions {
|
|
4
|
+
clientToken: string;
|
|
5
|
+
environment?: "sandbox" | "production";
|
|
6
|
+
displayMode?: "overlay" | "inline";
|
|
7
|
+
theme?: "light" | "dark";
|
|
8
|
+
frameTarget?: string;
|
|
9
|
+
onCompleted?: (data: unknown) => void;
|
|
10
|
+
onError?: (data: unknown) => void;
|
|
11
|
+
onClosed?: () => void;
|
|
12
|
+
}
|
|
13
|
+
type CheckoutStatus = "idle" | "loading" | "open" | "completed" | "error";
|
|
14
|
+
declare function usePaddleCheckout(options: UsePaddleCheckoutOptions): {
|
|
15
|
+
open: (transactionId: string) => Promise<void>;
|
|
16
|
+
status: CheckoutStatus;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
interface PaddleCheckoutProps extends UsePaddleCheckoutOptions {
|
|
20
|
+
/** Query param carrying the transaction id. Must match the server plugin's checkoutParam. Default "txn". */
|
|
21
|
+
paramName?: string;
|
|
22
|
+
/** Rendered when the URL has no transaction id. */
|
|
23
|
+
fallback?: React.ReactNode;
|
|
24
|
+
}
|
|
25
|
+
declare function PaddleCheckout({ paramName, fallback, ...options }: PaddleCheckoutProps): react.JSX.Element | null;
|
|
26
|
+
|
|
27
|
+
declare function getTransactionIdFromUrl(search: string, paramName?: string): string | null;
|
|
28
|
+
|
|
29
|
+
export { PaddleCheckout, getTransactionIdFromUrl, usePaddleCheckout };
|
|
30
|
+
export type { CheckoutStatus, PaddleCheckoutProps, UsePaddleCheckoutOptions };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// src/react/paddle-checkout.tsx
|
|
2
|
+
import { useEffect, useRef as useRef2, useState as useState2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/react/url.ts
|
|
5
|
+
function getTransactionIdFromUrl(search, paramName = "txn") {
|
|
6
|
+
const value = new URLSearchParams(search).get(paramName);
|
|
7
|
+
return value && /^txn_[a-z0-9]+$/i.test(value) ? value : null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/react/use-paddle-checkout.ts
|
|
11
|
+
import { useCallback, useRef, useState } from "react";
|
|
12
|
+
import { initializePaddle } from "@paddle/paddle-js";
|
|
13
|
+
function usePaddleCheckout(options) {
|
|
14
|
+
const paddleRef = useRef(null);
|
|
15
|
+
const [status, setStatus] = useState("idle");
|
|
16
|
+
const optionsRef = useRef(options);
|
|
17
|
+
optionsRef.current = options;
|
|
18
|
+
const open = useCallback(async (transactionId) => {
|
|
19
|
+
setStatus("loading");
|
|
20
|
+
try {
|
|
21
|
+
const opts = optionsRef.current;
|
|
22
|
+
if (!paddleRef.current) {
|
|
23
|
+
paddleRef.current = await initializePaddle({
|
|
24
|
+
token: opts.clientToken,
|
|
25
|
+
environment: opts.environment,
|
|
26
|
+
eventCallback: (event) => {
|
|
27
|
+
if (event.name === "checkout.completed") {
|
|
28
|
+
setStatus("completed");
|
|
29
|
+
optionsRef.current.onCompleted?.(event.data);
|
|
30
|
+
} else if (event.name === "checkout.error") {
|
|
31
|
+
setStatus("error");
|
|
32
|
+
optionsRef.current.onError?.(event.data);
|
|
33
|
+
} else if (event.name === "checkout.closed") {
|
|
34
|
+
optionsRef.current.onClosed?.();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}) ?? null;
|
|
38
|
+
}
|
|
39
|
+
if (!paddleRef.current) throw new Error("Failed to initialize Paddle.js");
|
|
40
|
+
paddleRef.current.Checkout.open({
|
|
41
|
+
transactionId,
|
|
42
|
+
settings: {
|
|
43
|
+
displayMode: opts.displayMode ?? "overlay",
|
|
44
|
+
theme: opts.theme,
|
|
45
|
+
...opts.displayMode === "inline" ? {
|
|
46
|
+
frameTarget: opts.frameTarget ?? "paddle-checkout-frame",
|
|
47
|
+
frameInitialHeight: 450,
|
|
48
|
+
frameStyle: "width: 100%; min-width: 312px; background-color: transparent; border: none;"
|
|
49
|
+
} : {}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
setStatus("open");
|
|
53
|
+
} catch (error) {
|
|
54
|
+
setStatus("error");
|
|
55
|
+
optionsRef.current.onError?.(error);
|
|
56
|
+
}
|
|
57
|
+
}, []);
|
|
58
|
+
return { open, status };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/react/paddle-checkout.tsx
|
|
62
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
63
|
+
function PaddleCheckout({ paramName, fallback, ...options }) {
|
|
64
|
+
const { open, status } = usePaddleCheckout(options);
|
|
65
|
+
const opened = useRef2(false);
|
|
66
|
+
const [transactionId, setTransactionId] = useState2(null);
|
|
67
|
+
const [checked, setChecked] = useState2(false);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
setTransactionId(getTransactionIdFromUrl(window.location.search, paramName));
|
|
70
|
+
setChecked(true);
|
|
71
|
+
}, [paramName]);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (transactionId && !opened.current) {
|
|
74
|
+
opened.current = true;
|
|
75
|
+
void open(transactionId);
|
|
76
|
+
}
|
|
77
|
+
}, [transactionId, open]);
|
|
78
|
+
if (options.displayMode === "inline") {
|
|
79
|
+
return /* @__PURE__ */ jsx("div", { className: options.frameTarget ?? "paddle-checkout-frame", "data-status": status });
|
|
80
|
+
}
|
|
81
|
+
if (!checked || transactionId) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return /* @__PURE__ */ jsx(Fragment, { children: fallback ?? /* @__PURE__ */ jsx("p", { children: "No transaction to check out. Start from the pricing page." }) });
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
PaddleCheckout,
|
|
88
|
+
getTransactionIdFromUrl,
|
|
89
|
+
usePaddleCheckout
|
|
90
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paddle/better-auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1-rc.0",
|
|
4
|
+
"description": "Paddle Billing plugin for Better Auth",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/PaddleHQ/better-auth.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"paddle",
|
|
13
|
+
"better-auth",
|
|
14
|
+
"billing",
|
|
15
|
+
"subscriptions"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
20
|
+
"packageManager": "pnpm@11.1.3",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./client": {
|
|
33
|
+
"types": "./dist/client.d.ts",
|
|
34
|
+
"import": "./dist/client.js"
|
|
35
|
+
},
|
|
36
|
+
"./react": {
|
|
37
|
+
"types": "./dist/react/index.d.ts",
|
|
38
|
+
"import": "./dist/react/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:watch": "vitest",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@paddle/paddle-js": ">=1.0.0",
|
|
49
|
+
"@paddle/paddle-node-sdk": ">=3.0.0",
|
|
50
|
+
"better-auth": ">=1.3.0",
|
|
51
|
+
"react": ">=18"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@paddle/paddle-js": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"react": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"zod": "^4.6.7"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@paddle/paddle-js": "^1.6.6",
|
|
66
|
+
"@paddle/paddle-node-sdk": "^3.11.1",
|
|
67
|
+
"@testing-library/react": "^16.3.3",
|
|
68
|
+
"@types/react": "^19.3.0",
|
|
69
|
+
"better-auth": "^1.7.19",
|
|
70
|
+
"happy-dom": "^21.0.4",
|
|
71
|
+
"react": "^19.3.1",
|
|
72
|
+
"react-dom": "^19.3.1",
|
|
73
|
+
"tsup": "^9.0.0",
|
|
74
|
+
"typescript": "^5.7.3",
|
|
75
|
+
"vitest": "^5.1.0"
|
|
76
|
+
}
|
|
77
|
+
}
|