@lavapayments/checkout 0.1.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/dist/index.d.mts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +116 -0
- package/dist/index.mjs +91 -0
- package/package.json +43 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function useLavaCheckout({ onSuccess, onCancel, onError, }: {
|
|
2
|
+
onSuccess?: (args: {
|
|
3
|
+
checkoutSessionId: string;
|
|
4
|
+
connectionId: string;
|
|
5
|
+
}) => void;
|
|
6
|
+
onCancel?: (args: {
|
|
7
|
+
checkoutSessionId: string;
|
|
8
|
+
}) => void;
|
|
9
|
+
onError?: (args: {
|
|
10
|
+
checkoutSessionId: string;
|
|
11
|
+
error: string;
|
|
12
|
+
}) => void;
|
|
13
|
+
}): {
|
|
14
|
+
open: (checkoutSessionToken: string) => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useLavaCheckout };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function useLavaCheckout({ onSuccess, onCancel, onError, }: {
|
|
2
|
+
onSuccess?: (args: {
|
|
3
|
+
checkoutSessionId: string;
|
|
4
|
+
connectionId: string;
|
|
5
|
+
}) => void;
|
|
6
|
+
onCancel?: (args: {
|
|
7
|
+
checkoutSessionId: string;
|
|
8
|
+
}) => void;
|
|
9
|
+
onError?: (args: {
|
|
10
|
+
checkoutSessionId: string;
|
|
11
|
+
error: string;
|
|
12
|
+
}) => void;
|
|
13
|
+
}): {
|
|
14
|
+
open: (checkoutSessionToken: string) => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useLavaCheckout };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useLavaCheckout: () => useLavaCheckout
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
function useLavaCheckout({
|
|
28
|
+
onSuccess,
|
|
29
|
+
onCancel,
|
|
30
|
+
onError
|
|
31
|
+
}) {
|
|
32
|
+
const [checkoutSessionToken, setCheckoutSessionToken] = (0, import_react.useState)(null);
|
|
33
|
+
(0, import_react.useEffect)(() => {
|
|
34
|
+
if (!checkoutSessionToken) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
let checkoutSession = null;
|
|
38
|
+
try {
|
|
39
|
+
checkoutSession = JSON.parse(
|
|
40
|
+
atob(checkoutSessionToken)
|
|
41
|
+
);
|
|
42
|
+
if (!checkoutSession.secret || !checkoutSession.base) {
|
|
43
|
+
throw new Error("Invalid checkout session token");
|
|
44
|
+
}
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (typeof onError === "function") {
|
|
47
|
+
onError({
|
|
48
|
+
checkoutSessionId: checkoutSessionToken,
|
|
49
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
let iframe = document.createElement("iframe");
|
|
55
|
+
iframe.src = `${checkoutSession.base}embed/checkout/${checkoutSession.secret}`;
|
|
56
|
+
iframe.style.width = "100%";
|
|
57
|
+
iframe.style.height = "100%";
|
|
58
|
+
iframe.style.margin = "auto";
|
|
59
|
+
iframe.style.border = "none";
|
|
60
|
+
iframe.style.position = "fixed";
|
|
61
|
+
iframe.style.top = "0";
|
|
62
|
+
iframe.style.left = "0";
|
|
63
|
+
iframe.style.right = "0";
|
|
64
|
+
iframe.style.bottom = "0";
|
|
65
|
+
iframe.style.backgroundColor = "transparent";
|
|
66
|
+
iframe.style.zIndex = "2147483647";
|
|
67
|
+
iframe.referrerPolicy = "no-referrer";
|
|
68
|
+
iframe.setAttribute(
|
|
69
|
+
"sandbox",
|
|
70
|
+
"allow-forms allow-scripts allow-same-origin"
|
|
71
|
+
);
|
|
72
|
+
document.body.appendChild(iframe);
|
|
73
|
+
function handleMessage(e) {
|
|
74
|
+
switch (e.data.type) {
|
|
75
|
+
case "lava_checkout_success":
|
|
76
|
+
if (typeof onSuccess === "function") {
|
|
77
|
+
onSuccess({
|
|
78
|
+
checkoutSessionId: e.data.checkoutSessionId,
|
|
79
|
+
connectionId: e.data.connectionId
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
setCheckoutSessionToken(null);
|
|
83
|
+
break;
|
|
84
|
+
case "lava_checkout_cancel":
|
|
85
|
+
if (typeof onCancel === "function") {
|
|
86
|
+
onCancel({ checkoutSessionId: e.data.checkoutSessionId });
|
|
87
|
+
}
|
|
88
|
+
setCheckoutSessionToken(null);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function onBeforeUnload(e) {
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
e.returnValue = "";
|
|
95
|
+
return "Your checkout is not complete. Are you sure you want to leave?";
|
|
96
|
+
}
|
|
97
|
+
window.addEventListener("message", handleMessage, false);
|
|
98
|
+
window.addEventListener("beforeunload", onBeforeUnload, false);
|
|
99
|
+
return () => {
|
|
100
|
+
window.removeEventListener("message", handleMessage);
|
|
101
|
+
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
102
|
+
if (iframe) {
|
|
103
|
+
document.body.removeChild(iframe);
|
|
104
|
+
iframe = null;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}, [checkoutSessionToken]);
|
|
108
|
+
const open = (0, import_react.useCallback)((checkoutSessionToken2) => {
|
|
109
|
+
setCheckoutSessionToken(checkoutSessionToken2);
|
|
110
|
+
}, []);
|
|
111
|
+
return { open };
|
|
112
|
+
}
|
|
113
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
114
|
+
0 && (module.exports = {
|
|
115
|
+
useLavaCheckout
|
|
116
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { useEffect, useCallback, useState } from "react";
|
|
3
|
+
function useLavaCheckout({
|
|
4
|
+
onSuccess,
|
|
5
|
+
onCancel,
|
|
6
|
+
onError
|
|
7
|
+
}) {
|
|
8
|
+
const [checkoutSessionToken, setCheckoutSessionToken] = useState(null);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (!checkoutSessionToken) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
let checkoutSession = null;
|
|
14
|
+
try {
|
|
15
|
+
checkoutSession = JSON.parse(
|
|
16
|
+
atob(checkoutSessionToken)
|
|
17
|
+
);
|
|
18
|
+
if (!checkoutSession.secret || !checkoutSession.base) {
|
|
19
|
+
throw new Error("Invalid checkout session token");
|
|
20
|
+
}
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (typeof onError === "function") {
|
|
23
|
+
onError({
|
|
24
|
+
checkoutSessionId: checkoutSessionToken,
|
|
25
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
let iframe = document.createElement("iframe");
|
|
31
|
+
iframe.src = `${checkoutSession.base}embed/checkout/${checkoutSession.secret}`;
|
|
32
|
+
iframe.style.width = "100%";
|
|
33
|
+
iframe.style.height = "100%";
|
|
34
|
+
iframe.style.margin = "auto";
|
|
35
|
+
iframe.style.border = "none";
|
|
36
|
+
iframe.style.position = "fixed";
|
|
37
|
+
iframe.style.top = "0";
|
|
38
|
+
iframe.style.left = "0";
|
|
39
|
+
iframe.style.right = "0";
|
|
40
|
+
iframe.style.bottom = "0";
|
|
41
|
+
iframe.style.backgroundColor = "transparent";
|
|
42
|
+
iframe.style.zIndex = "2147483647";
|
|
43
|
+
iframe.referrerPolicy = "no-referrer";
|
|
44
|
+
iframe.setAttribute(
|
|
45
|
+
"sandbox",
|
|
46
|
+
"allow-forms allow-scripts allow-same-origin"
|
|
47
|
+
);
|
|
48
|
+
document.body.appendChild(iframe);
|
|
49
|
+
function handleMessage(e) {
|
|
50
|
+
switch (e.data.type) {
|
|
51
|
+
case "lava_checkout_success":
|
|
52
|
+
if (typeof onSuccess === "function") {
|
|
53
|
+
onSuccess({
|
|
54
|
+
checkoutSessionId: e.data.checkoutSessionId,
|
|
55
|
+
connectionId: e.data.connectionId
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
setCheckoutSessionToken(null);
|
|
59
|
+
break;
|
|
60
|
+
case "lava_checkout_cancel":
|
|
61
|
+
if (typeof onCancel === "function") {
|
|
62
|
+
onCancel({ checkoutSessionId: e.data.checkoutSessionId });
|
|
63
|
+
}
|
|
64
|
+
setCheckoutSessionToken(null);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function onBeforeUnload(e) {
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
e.returnValue = "";
|
|
71
|
+
return "Your checkout is not complete. Are you sure you want to leave?";
|
|
72
|
+
}
|
|
73
|
+
window.addEventListener("message", handleMessage, false);
|
|
74
|
+
window.addEventListener("beforeunload", onBeforeUnload, false);
|
|
75
|
+
return () => {
|
|
76
|
+
window.removeEventListener("message", handleMessage);
|
|
77
|
+
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
78
|
+
if (iframe) {
|
|
79
|
+
document.body.removeChild(iframe);
|
|
80
|
+
iframe = null;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}, [checkoutSessionToken]);
|
|
84
|
+
const open = useCallback((checkoutSessionToken2) => {
|
|
85
|
+
setCheckoutSessionToken(checkoutSessionToken2);
|
|
86
|
+
}, []);
|
|
87
|
+
return { open };
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
useLavaCheckout
|
|
91
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lavapayments/checkout",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Lava Checkout",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsup --dts --format cjs,esm src/index.ts",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"lint": "eslint src/",
|
|
16
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
17
|
+
"prepublishOnly": "npm run build",
|
|
18
|
+
"clean": "rm -rf dist"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"billing",
|
|
22
|
+
"checkout",
|
|
23
|
+
"payments"
|
|
24
|
+
],
|
|
25
|
+
"author": "Lava",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
29
|
+
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^20.0.0",
|
|
33
|
+
"@types/eslint": "^8.0.0",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
35
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
36
|
+
"eslint": "^8.0.0",
|
|
37
|
+
"prettier": "^3.0.0",
|
|
38
|
+
"react": "^17.0.2",
|
|
39
|
+
"react-dom": "^17.0.2",
|
|
40
|
+
"tsup": "^8.0.0",
|
|
41
|
+
"typescript": "^5.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|