@betterstore/react 0.3.77 → 0.3.79
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/CHANGELOG.md +12 -0
- package/dist/components/checkout-embed/checkout-form.d.ts +2 -4
- package/dist/components/checkout-embed/steps/payment/form.d.ts +2 -1
- package/dist/components/checkout-embed/steps/summary/discount-code.d.ts +1 -2
- package/dist/components/checkout-embed/steps/summary/index.d.ts +1 -2
- package/dist/index.cjs.js +37 -50
- package/dist/index.mjs +37 -50
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -16,11 +16,9 @@ interface CheckoutFormProps {
|
|
|
16
16
|
locale?: StripeElementLocale;
|
|
17
17
|
setShippingCost: (cost: number) => void;
|
|
18
18
|
exchangeRate: number;
|
|
19
|
-
setCheckout: (checkout: any) => void;
|
|
20
|
-
setPaymentSecret: (paymentSecret: string) => void;
|
|
21
|
-
setPublicKey: (publicKey: string) => void;
|
|
22
19
|
paymentSecret: string | null;
|
|
23
20
|
publicKey: string | null;
|
|
21
|
+
paymentComponentKey: number;
|
|
24
22
|
}
|
|
25
|
-
export default function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate,
|
|
23
|
+
export default function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate, paymentSecret, publicKey, paymentComponentKey, }: CheckoutFormProps): React.JSX.Element;
|
|
26
24
|
export {};
|
|
@@ -15,6 +15,7 @@ interface PaymentFormProps {
|
|
|
15
15
|
fonts?: Fonts;
|
|
16
16
|
locale?: StripeElementLocale;
|
|
17
17
|
publicKey: string | null;
|
|
18
|
+
paymentComponentKey: number;
|
|
18
19
|
}
|
|
19
|
-
export default function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, }: PaymentFormProps): React.JSX.Element;
|
|
20
|
+
export default function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, paymentComponentKey, }: PaymentFormProps): React.JSX.Element;
|
|
20
21
|
export {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
export default function DiscountCode({ applyDiscountCode,
|
|
2
|
+
export default function DiscountCode({ applyDiscountCode, }: {
|
|
3
3
|
applyDiscountCode: (code: string) => Promise<void>;
|
|
4
|
-
revalidateDiscounts: () => Promise<void>;
|
|
5
4
|
}): React.JSX.Element;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CheckoutSession, LineItem } from "@betterstore/sdk";
|
|
2
2
|
import React from "react";
|
|
3
|
-
export default function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode,
|
|
3
|
+
export default function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode, removeDiscount, }: {
|
|
4
4
|
appliedDiscounts: CheckoutSession["appliedDiscounts"];
|
|
5
5
|
lineItems: LineItem[];
|
|
6
6
|
shipping?: number | null;
|
|
@@ -9,6 +9,5 @@ export default function CheckoutSummary({ appliedDiscounts, lineItems, shipping,
|
|
|
9
9
|
exchangeRate: number;
|
|
10
10
|
onCancel: () => void;
|
|
11
11
|
applyDiscountCode: (code: string) => Promise<void>;
|
|
12
|
-
revalidateDiscounts: () => Promise<void>;
|
|
13
12
|
removeDiscount: (id: string) => Promise<void>;
|
|
14
13
|
}): React.JSX.Element;
|
package/dist/index.cjs.js
CHANGED
|
@@ -35081,15 +35081,9 @@ function PaymentElement({ paymentSecret, publicKey, checkoutAppearance, locale,
|
|
|
35081
35081
|
}
|
|
35082
35082
|
var PaymentElement$1 = React.memo(PaymentElement);
|
|
35083
35083
|
|
|
35084
|
-
function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, }) {
|
|
35084
|
+
function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, paymentComponentKey, }) {
|
|
35085
35085
|
const [isSubmitting, setIsSubmitting] = React.useState(false);
|
|
35086
|
-
const [key, setKey] = React.useState(0);
|
|
35087
35086
|
const { t } = useTranslation();
|
|
35088
|
-
React.useEffect(() => {
|
|
35089
|
-
if (paymentSecret) {
|
|
35090
|
-
setKey((prev) => prev + 1);
|
|
35091
|
-
}
|
|
35092
|
-
}, [paymentSecret]);
|
|
35093
35087
|
return (React.createElement("div", { className: "space-y-6" },
|
|
35094
35088
|
React.createElement("div", null,
|
|
35095
35089
|
React.createElement("h2", { className: "mb-2" }, t("CheckoutEmbed.Payment.title")),
|
|
@@ -35116,7 +35110,7 @@ function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack,
|
|
|
35116
35110
|
" \u00B7 ",
|
|
35117
35111
|
shippingPrice)),
|
|
35118
35112
|
React.createElement(Button, { variant: "link", size: "link", onClick: onBack }, t("CheckoutEmbed.Shipping.change")))),
|
|
35119
|
-
React.createElement("div", { className: "mt-8" }, paymentSecret && (React.createElement(PaymentElement$1, { key:
|
|
35113
|
+
React.createElement("div", { className: "mt-8" }, paymentSecret && (React.createElement(PaymentElement$1, { key: paymentComponentKey, fonts: fonts, checkoutAppearance: convertCheckoutAppearanceToStripeAppearance(checkoutAppearance, fonts), locale: locale, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, setSubmitting: setIsSubmitting, publicKey: publicKey },
|
|
35120
35114
|
React.createElement("div", { className: "flex justify-between items-center pt-8" },
|
|
35121
35115
|
React.createElement(Button, { type: "button", variant: "ghost", onClick: onBack },
|
|
35122
35116
|
React.createElement(ChevronLeft, null),
|
|
@@ -35293,10 +35287,9 @@ const resetFormStore = (formData) => {
|
|
|
35293
35287
|
localStorage.setItem("checkout", JSON.stringify(Object.assign(Object.assign({}, currentState), { state: Object.assign(Object.assign({}, currentState.state), { step: "customer", formData: { customer: formData.customer } }) })));
|
|
35294
35288
|
};
|
|
35295
35289
|
|
|
35296
|
-
function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate,
|
|
35290
|
+
function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate, paymentSecret, publicKey, paymentComponentKey, }) {
|
|
35297
35291
|
const { formData, setFormData, step, setStep, checkoutId: storedCheckoutId, setCheckoutId, } = useFormStore();
|
|
35298
35292
|
const [shippingRates, setShippingRates] = React.useState([]);
|
|
35299
|
-
const paymentSecretPromiseRef = React.useRef(null);
|
|
35300
35293
|
const validateStep = React.useCallback(() => {
|
|
35301
35294
|
if (step === "customer")
|
|
35302
35295
|
return;
|
|
@@ -35450,31 +35443,9 @@ function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl,
|
|
|
35450
35443
|
const handleDoubleBack = () => {
|
|
35451
35444
|
setStep("customer");
|
|
35452
35445
|
};
|
|
35453
|
-
React.useEffect(() => {
|
|
35454
|
-
const generatePaymentSecret = () => __awaiter(this, void 0, void 0, function* () {
|
|
35455
|
-
try {
|
|
35456
|
-
const { paymentSecret, publicKey, checkoutSession: newCheckout, } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35457
|
-
setPaymentSecret(paymentSecret);
|
|
35458
|
-
setPublicKey(publicKey);
|
|
35459
|
-
setCheckout(newCheckout);
|
|
35460
|
-
}
|
|
35461
|
-
catch (error) {
|
|
35462
|
-
console.error("Failed to generate payment secret:", error);
|
|
35463
|
-
onError();
|
|
35464
|
-
}
|
|
35465
|
-
finally {
|
|
35466
|
-
paymentSecretPromiseRef.current = null;
|
|
35467
|
-
}
|
|
35468
|
-
});
|
|
35469
|
-
if (step === "payment" &&
|
|
35470
|
-
!paymentSecret &&
|
|
35471
|
-
!paymentSecretPromiseRef.current) {
|
|
35472
|
-
paymentSecretPromiseRef.current = generatePaymentSecret();
|
|
35473
|
-
}
|
|
35474
|
-
}, [paymentSecret]);
|
|
35475
35446
|
const renderStep = () => {
|
|
35476
35447
|
if (step === "payment" && formData.customer && formData.shipping) {
|
|
35477
|
-
return (React.createElement(PaymentForm, { locale: locale, fonts: fonts, checkoutAppearance: checkoutAppearance, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, onBack: handleBack, onDoubleBack: handleDoubleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), shippingName: formData.shipping.name, shippingPrice: storeHelpers.formatPrice(formData.shipping.price, currency, exchangeRate), publicKey: publicKey }));
|
|
35448
|
+
return (React.createElement(PaymentForm, { paymentComponentKey: paymentComponentKey, locale: locale, fonts: fonts, checkoutAppearance: checkoutAppearance, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, onBack: handleBack, onDoubleBack: handleDoubleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), shippingName: formData.shipping.name, shippingPrice: storeHelpers.formatPrice(formData.shipping.price, currency, exchangeRate), publicKey: publicKey }));
|
|
35478
35449
|
}
|
|
35479
35450
|
if (step === "shipping" && formData.customer) {
|
|
35480
35451
|
return (React.createElement(ShippingMethodForm, { setFormData: setFormData, formData: formData, shippingRates: shippingRates, initialData: formData.shipping, onSubmit: handleShippingSubmit, onBack: handleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), currency: currency, exchangeRate: exchangeRate, locale: locale, countryCode: formData.customer.address.countryCode }));
|
|
@@ -35503,7 +35474,7 @@ function InputGroupLoading({ className }) {
|
|
|
35503
35474
|
React.createElement(Skeleton, { className: "w-full h-10" })));
|
|
35504
35475
|
}
|
|
35505
35476
|
|
|
35506
|
-
function DiscountCode({ applyDiscountCode,
|
|
35477
|
+
function DiscountCode({ applyDiscountCode, }) {
|
|
35507
35478
|
const { t } = useTranslation();
|
|
35508
35479
|
const [discountCode, setDiscountCode] = React.useState("");
|
|
35509
35480
|
const [isLoading, setIsLoading] = React.useState(false);
|
|
@@ -35514,7 +35485,6 @@ function DiscountCode({ applyDiscountCode, revalidateDiscounts, }) {
|
|
|
35514
35485
|
setIsLoading(true);
|
|
35515
35486
|
try {
|
|
35516
35487
|
yield applyDiscountCode(discountCode);
|
|
35517
|
-
// await revalidateDiscounts();
|
|
35518
35488
|
setDiscountCode("");
|
|
35519
35489
|
toast.success(t("CheckoutEmbed.Summary.discountCodeSuccess"));
|
|
35520
35490
|
}
|
|
@@ -35537,7 +35507,7 @@ function DiscountCode({ applyDiscountCode, revalidateDiscounts, }) {
|
|
|
35537
35507
|
error && React.createElement("p", { className: "text-red-500 mt-2 text-sm" }, error)));
|
|
35538
35508
|
}
|
|
35539
35509
|
|
|
35540
|
-
function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode,
|
|
35510
|
+
function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode, removeDiscount, }) {
|
|
35541
35511
|
var _a, _b;
|
|
35542
35512
|
const { formData } = useFormStore();
|
|
35543
35513
|
const [isOpen, setIsOpen] = React.useState(false);
|
|
@@ -35610,7 +35580,7 @@ function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency,
|
|
|
35610
35580
|
"hidden md:grid": !isOpen,
|
|
35611
35581
|
grid: isOpen,
|
|
35612
35582
|
}) },
|
|
35613
|
-
React.createElement(DiscountCode, { applyDiscountCode: applyDiscountCode
|
|
35583
|
+
React.createElement(DiscountCode, { applyDiscountCode: applyDiscountCode })))),
|
|
35614
35584
|
React.createElement("hr", { className: clsx("order-5 md:order-none", {
|
|
35615
35585
|
"hidden md:block": !isOpen,
|
|
35616
35586
|
block: isOpen,
|
|
@@ -35726,8 +35696,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35726
35696
|
const iframeRef = React.useRef(null);
|
|
35727
35697
|
React.useMemo(() => createI18nInstance(locale), []);
|
|
35728
35698
|
const { formData, step } = useFormStore();
|
|
35699
|
+
const paymentSecretPromiseRef = React.useRef(null);
|
|
35729
35700
|
const [paymentSecret, setPaymentSecret] = React.useState(null);
|
|
35730
35701
|
const [publicKey, setPublicKey] = React.useState(null);
|
|
35702
|
+
const [paymentComponentKey, setPaymentComponentKey] = React.useState(0);
|
|
35731
35703
|
const [checkout, setCheckout] = React.useState(null);
|
|
35732
35704
|
const [loading, setLoading] = React.useState(true);
|
|
35733
35705
|
React.useEffect(() => {
|
|
@@ -35776,16 +35748,35 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35776
35748
|
return;
|
|
35777
35749
|
setCheckout(Object.assign(Object.assign({}, checkout), { shipping: cost }));
|
|
35778
35750
|
};
|
|
35751
|
+
function generatePaymentSecret() {
|
|
35752
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35753
|
+
const { paymentSecret, publicKey, checkoutSession } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35754
|
+
setPaymentSecret(paymentSecret);
|
|
35755
|
+
setPublicKey(publicKey);
|
|
35756
|
+
setCheckout(checkoutSession);
|
|
35757
|
+
});
|
|
35758
|
+
}
|
|
35759
|
+
React.useEffect(() => {
|
|
35760
|
+
if (step === "payment" &&
|
|
35761
|
+
!paymentSecret &&
|
|
35762
|
+
!paymentSecretPromiseRef.current) {
|
|
35763
|
+
paymentSecretPromiseRef.current = generatePaymentSecret().finally(() => {
|
|
35764
|
+
paymentSecretPromiseRef.current = null;
|
|
35765
|
+
});
|
|
35766
|
+
}
|
|
35767
|
+
}, [paymentSecret, step]);
|
|
35779
35768
|
const applyDiscountCode = (code) => __awaiter(this, void 0, void 0, function* () {
|
|
35780
35769
|
const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
|
|
35781
35770
|
setCheckout(newCheckout);
|
|
35771
|
+
if (step === "payment") {
|
|
35772
|
+
yield generatePaymentSecret();
|
|
35773
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35774
|
+
}
|
|
35782
35775
|
});
|
|
35783
35776
|
const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
|
|
35784
35777
|
if (step === "payment") {
|
|
35785
|
-
|
|
35786
|
-
|
|
35787
|
-
setPublicKey(publicKey);
|
|
35788
|
-
setCheckout(checkoutSession);
|
|
35778
|
+
yield generatePaymentSecret();
|
|
35779
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35789
35780
|
}
|
|
35790
35781
|
else {
|
|
35791
35782
|
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
@@ -35794,14 +35785,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35794
35785
|
});
|
|
35795
35786
|
const removeDiscount = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
35796
35787
|
const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
|
|
35788
|
+
setCheckout(newCheckout);
|
|
35797
35789
|
if (step === "payment") {
|
|
35798
|
-
|
|
35799
|
-
|
|
35800
|
-
setPublicKey(publicKey);
|
|
35801
|
-
setCheckout(checkoutSession);
|
|
35802
|
-
}
|
|
35803
|
-
else {
|
|
35804
|
-
setCheckout(newCheckout);
|
|
35790
|
+
yield generatePaymentSecret();
|
|
35791
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35805
35792
|
}
|
|
35806
35793
|
});
|
|
35807
35794
|
React.useEffect(() => {
|
|
@@ -35815,10 +35802,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35815
35802
|
return (React.createElement(IframeWrapper, { iframeRef: iframeRef },
|
|
35816
35803
|
React.createElement("div", { className: "checkout-embed h-max gap-6 md:gap-0 py-4 md:py-12 flex flex-col md:grid md:grid-cols-7 " },
|
|
35817
35804
|
React.createElement(Appearance, { appearance: appearance, fonts: config.fonts, iframeRef: iframeRef }),
|
|
35818
|
-
React.createElement("div", { className: "md:col-span-4 px-4 h-max md:px-8" }, loading ? (React.createElement(CheckoutFormLoading, null)) : (React.createElement(CheckoutForm, { locale: locale, setShippingCost: setShippingCost, storeClient: storeClient, fonts: config.fonts, checkoutAppearance: appearance, currency: (_a = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _a !== void 0 ? _a : "", customer: checkout === null || checkout === void 0 ? void 0 : checkout.customer, cancelUrl: cancelUrl, checkoutId: checkoutId, clientSecret: clientSecret, onSuccess: onSuccess, onError: onError, exchangeRate: (_b = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _b !== void 0 ? _b : 1,
|
|
35805
|
+
React.createElement("div", { className: "md:col-span-4 px-4 h-max md:px-8" }, loading ? (React.createElement(CheckoutFormLoading, null)) : (React.createElement(CheckoutForm, { locale: locale, setShippingCost: setShippingCost, storeClient: storeClient, fonts: config.fonts, checkoutAppearance: appearance, currency: (_a = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _a !== void 0 ? _a : "", customer: checkout === null || checkout === void 0 ? void 0 : checkout.customer, cancelUrl: cancelUrl, checkoutId: checkoutId, clientSecret: clientSecret, onSuccess: onSuccess, onError: onError, exchangeRate: (_b = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _b !== void 0 ? _b : 1, publicKey: publicKey, paymentSecret: paymentSecret, paymentComponentKey: paymentComponentKey }))),
|
|
35819
35806
|
React.createElement("div", { className: "md:col-span-3 px-4 md:px-8 h-max order-first md:order-last" },
|
|
35820
35807
|
React.createElement(Toaster, null),
|
|
35821
|
-
loading ? (React.createElement(CheckoutSummaryLoading, null)) : (React.createElement(CheckoutSummary, { currency: (_c = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _c !== void 0 ? _c : "", lineItems: (_d = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) !== null && _d !== void 0 ? _d : [], shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax, onCancel: onCancel, exchangeRate: (_e = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _e !== void 0 ? _e : 1, applyDiscountCode: applyDiscountCode, appliedDiscounts: (_f = checkout === null || checkout === void 0 ? void 0 : checkout.appliedDiscounts) !== null && _f !== void 0 ? _f : [],
|
|
35808
|
+
loading ? (React.createElement(CheckoutSummaryLoading, null)) : (React.createElement(CheckoutSummary, { currency: (_c = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _c !== void 0 ? _c : "", lineItems: (_d = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) !== null && _d !== void 0 ? _d : [], shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax, onCancel: onCancel, exchangeRate: (_e = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _e !== void 0 ? _e : 1, applyDiscountCode: applyDiscountCode, appliedDiscounts: (_f = checkout === null || checkout === void 0 ? void 0 : checkout.appliedDiscounts) !== null && _f !== void 0 ? _f : [], removeDiscount: removeDiscount }))))));
|
|
35822
35809
|
}
|
|
35823
35810
|
const CheckoutEmbed = React.memo(CheckoutEmbedComponent);
|
|
35824
35811
|
|
package/dist/index.mjs
CHANGED
|
@@ -35061,15 +35061,9 @@ function PaymentElement({ paymentSecret, publicKey, checkoutAppearance, locale,
|
|
|
35061
35061
|
}
|
|
35062
35062
|
var PaymentElement$1 = memo(PaymentElement);
|
|
35063
35063
|
|
|
35064
|
-
function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, }) {
|
|
35064
|
+
function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack, contactEmail, shippingAddress, shippingName, shippingPrice, checkoutAppearance, fonts, locale, publicKey, paymentComponentKey, }) {
|
|
35065
35065
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
35066
|
-
const [key, setKey] = useState(0);
|
|
35067
35066
|
const { t } = useTranslation();
|
|
35068
|
-
useEffect(() => {
|
|
35069
|
-
if (paymentSecret) {
|
|
35070
|
-
setKey((prev) => prev + 1);
|
|
35071
|
-
}
|
|
35072
|
-
}, [paymentSecret]);
|
|
35073
35067
|
return (React__default.createElement("div", { className: "space-y-6" },
|
|
35074
35068
|
React__default.createElement("div", null,
|
|
35075
35069
|
React__default.createElement("h2", { className: "mb-2" }, t("CheckoutEmbed.Payment.title")),
|
|
@@ -35096,7 +35090,7 @@ function PaymentForm({ paymentSecret, onSuccess, onError, onBack, onDoubleBack,
|
|
|
35096
35090
|
" \u00B7 ",
|
|
35097
35091
|
shippingPrice)),
|
|
35098
35092
|
React__default.createElement(Button, { variant: "link", size: "link", onClick: onBack }, t("CheckoutEmbed.Shipping.change")))),
|
|
35099
|
-
React__default.createElement("div", { className: "mt-8" }, paymentSecret && (React__default.createElement(PaymentElement$1, { key:
|
|
35093
|
+
React__default.createElement("div", { className: "mt-8" }, paymentSecret && (React__default.createElement(PaymentElement$1, { key: paymentComponentKey, fonts: fonts, checkoutAppearance: convertCheckoutAppearanceToStripeAppearance(checkoutAppearance, fonts), locale: locale, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, setSubmitting: setIsSubmitting, publicKey: publicKey },
|
|
35100
35094
|
React__default.createElement("div", { className: "flex justify-between items-center pt-8" },
|
|
35101
35095
|
React__default.createElement(Button, { type: "button", variant: "ghost", onClick: onBack },
|
|
35102
35096
|
React__default.createElement(ChevronLeft, null),
|
|
@@ -35273,10 +35267,9 @@ const resetFormStore = (formData) => {
|
|
|
35273
35267
|
localStorage.setItem("checkout", JSON.stringify(Object.assign(Object.assign({}, currentState), { state: Object.assign(Object.assign({}, currentState.state), { step: "customer", formData: { customer: formData.customer } }) })));
|
|
35274
35268
|
};
|
|
35275
35269
|
|
|
35276
|
-
function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate,
|
|
35270
|
+
function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl, clientSecret, customer, currency, checkoutAppearance, fonts, locale, setShippingCost, exchangeRate, paymentSecret, publicKey, paymentComponentKey, }) {
|
|
35277
35271
|
const { formData, setFormData, step, setStep, checkoutId: storedCheckoutId, setCheckoutId, } = useFormStore();
|
|
35278
35272
|
const [shippingRates, setShippingRates] = useState([]);
|
|
35279
|
-
const paymentSecretPromiseRef = useRef(null);
|
|
35280
35273
|
const validateStep = useCallback(() => {
|
|
35281
35274
|
if (step === "customer")
|
|
35282
35275
|
return;
|
|
@@ -35430,31 +35423,9 @@ function CheckoutForm({ storeClient, checkoutId, onSuccess, onError, cancelUrl,
|
|
|
35430
35423
|
const handleDoubleBack = () => {
|
|
35431
35424
|
setStep("customer");
|
|
35432
35425
|
};
|
|
35433
|
-
useEffect(() => {
|
|
35434
|
-
const generatePaymentSecret = () => __awaiter(this, void 0, void 0, function* () {
|
|
35435
|
-
try {
|
|
35436
|
-
const { paymentSecret, publicKey, checkoutSession: newCheckout, } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35437
|
-
setPaymentSecret(paymentSecret);
|
|
35438
|
-
setPublicKey(publicKey);
|
|
35439
|
-
setCheckout(newCheckout);
|
|
35440
|
-
}
|
|
35441
|
-
catch (error) {
|
|
35442
|
-
console.error("Failed to generate payment secret:", error);
|
|
35443
|
-
onError();
|
|
35444
|
-
}
|
|
35445
|
-
finally {
|
|
35446
|
-
paymentSecretPromiseRef.current = null;
|
|
35447
|
-
}
|
|
35448
|
-
});
|
|
35449
|
-
if (step === "payment" &&
|
|
35450
|
-
!paymentSecret &&
|
|
35451
|
-
!paymentSecretPromiseRef.current) {
|
|
35452
|
-
paymentSecretPromiseRef.current = generatePaymentSecret();
|
|
35453
|
-
}
|
|
35454
|
-
}, [paymentSecret]);
|
|
35455
35426
|
const renderStep = () => {
|
|
35456
35427
|
if (step === "payment" && formData.customer && formData.shipping) {
|
|
35457
|
-
return (React__default.createElement(PaymentForm, { locale: locale, fonts: fonts, checkoutAppearance: checkoutAppearance, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, onBack: handleBack, onDoubleBack: handleDoubleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), shippingName: formData.shipping.name, shippingPrice: storeHelpers.formatPrice(formData.shipping.price, currency, exchangeRate), publicKey: publicKey }));
|
|
35428
|
+
return (React__default.createElement(PaymentForm, { paymentComponentKey: paymentComponentKey, locale: locale, fonts: fonts, checkoutAppearance: checkoutAppearance, paymentSecret: paymentSecret, onSuccess: onSuccess, onError: onError, onBack: handleBack, onDoubleBack: handleDoubleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), shippingName: formData.shipping.name, shippingPrice: storeHelpers.formatPrice(formData.shipping.price, currency, exchangeRate), publicKey: publicKey }));
|
|
35458
35429
|
}
|
|
35459
35430
|
if (step === "shipping" && formData.customer) {
|
|
35460
35431
|
return (React__default.createElement(ShippingMethodForm, { setFormData: setFormData, formData: formData, shippingRates: shippingRates, initialData: formData.shipping, onSubmit: handleShippingSubmit, onBack: handleBack, contactEmail: formData.customer.email, shippingAddress: formatAddress(formData.customer.address), currency: currency, exchangeRate: exchangeRate, locale: locale, countryCode: formData.customer.address.countryCode }));
|
|
@@ -35483,7 +35454,7 @@ function InputGroupLoading({ className }) {
|
|
|
35483
35454
|
React__default.createElement(Skeleton, { className: "w-full h-10" })));
|
|
35484
35455
|
}
|
|
35485
35456
|
|
|
35486
|
-
function DiscountCode({ applyDiscountCode,
|
|
35457
|
+
function DiscountCode({ applyDiscountCode, }) {
|
|
35487
35458
|
const { t } = useTranslation();
|
|
35488
35459
|
const [discountCode, setDiscountCode] = useState("");
|
|
35489
35460
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -35494,7 +35465,6 @@ function DiscountCode({ applyDiscountCode, revalidateDiscounts, }) {
|
|
|
35494
35465
|
setIsLoading(true);
|
|
35495
35466
|
try {
|
|
35496
35467
|
yield applyDiscountCode(discountCode);
|
|
35497
|
-
// await revalidateDiscounts();
|
|
35498
35468
|
setDiscountCode("");
|
|
35499
35469
|
toast.success(t("CheckoutEmbed.Summary.discountCodeSuccess"));
|
|
35500
35470
|
}
|
|
@@ -35517,7 +35487,7 @@ function DiscountCode({ applyDiscountCode, revalidateDiscounts, }) {
|
|
|
35517
35487
|
error && React__default.createElement("p", { className: "text-red-500 mt-2 text-sm" }, error)));
|
|
35518
35488
|
}
|
|
35519
35489
|
|
|
35520
|
-
function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode,
|
|
35490
|
+
function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency, onCancel, exchangeRate, applyDiscountCode, removeDiscount, }) {
|
|
35521
35491
|
var _a, _b;
|
|
35522
35492
|
const { formData } = useFormStore();
|
|
35523
35493
|
const [isOpen, setIsOpen] = useState(false);
|
|
@@ -35590,7 +35560,7 @@ function CheckoutSummary({ appliedDiscounts, lineItems, shipping, tax, currency,
|
|
|
35590
35560
|
"hidden md:grid": !isOpen,
|
|
35591
35561
|
grid: isOpen,
|
|
35592
35562
|
}) },
|
|
35593
|
-
React__default.createElement(DiscountCode, { applyDiscountCode: applyDiscountCode
|
|
35563
|
+
React__default.createElement(DiscountCode, { applyDiscountCode: applyDiscountCode })))),
|
|
35594
35564
|
React__default.createElement("hr", { className: clsx("order-5 md:order-none", {
|
|
35595
35565
|
"hidden md:block": !isOpen,
|
|
35596
35566
|
block: isOpen,
|
|
@@ -35706,8 +35676,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35706
35676
|
const iframeRef = React__default.useRef(null);
|
|
35707
35677
|
React__default.useMemo(() => createI18nInstance(locale), []);
|
|
35708
35678
|
const { formData, step } = useFormStore();
|
|
35679
|
+
const paymentSecretPromiseRef = useRef(null);
|
|
35709
35680
|
const [paymentSecret, setPaymentSecret] = useState(null);
|
|
35710
35681
|
const [publicKey, setPublicKey] = useState(null);
|
|
35682
|
+
const [paymentComponentKey, setPaymentComponentKey] = useState(0);
|
|
35711
35683
|
const [checkout, setCheckout] = useState(null);
|
|
35712
35684
|
const [loading, setLoading] = useState(true);
|
|
35713
35685
|
useEffect(() => {
|
|
@@ -35756,16 +35728,35 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35756
35728
|
return;
|
|
35757
35729
|
setCheckout(Object.assign(Object.assign({}, checkout), { shipping: cost }));
|
|
35758
35730
|
};
|
|
35731
|
+
function generatePaymentSecret() {
|
|
35732
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35733
|
+
const { paymentSecret, publicKey, checkoutSession } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35734
|
+
setPaymentSecret(paymentSecret);
|
|
35735
|
+
setPublicKey(publicKey);
|
|
35736
|
+
setCheckout(checkoutSession);
|
|
35737
|
+
});
|
|
35738
|
+
}
|
|
35739
|
+
useEffect(() => {
|
|
35740
|
+
if (step === "payment" &&
|
|
35741
|
+
!paymentSecret &&
|
|
35742
|
+
!paymentSecretPromiseRef.current) {
|
|
35743
|
+
paymentSecretPromiseRef.current = generatePaymentSecret().finally(() => {
|
|
35744
|
+
paymentSecretPromiseRef.current = null;
|
|
35745
|
+
});
|
|
35746
|
+
}
|
|
35747
|
+
}, [paymentSecret, step]);
|
|
35759
35748
|
const applyDiscountCode = (code) => __awaiter(this, void 0, void 0, function* () {
|
|
35760
35749
|
const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
|
|
35761
35750
|
setCheckout(newCheckout);
|
|
35751
|
+
if (step === "payment") {
|
|
35752
|
+
yield generatePaymentSecret();
|
|
35753
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35754
|
+
}
|
|
35762
35755
|
});
|
|
35763
35756
|
const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
|
|
35764
35757
|
if (step === "payment") {
|
|
35765
|
-
|
|
35766
|
-
|
|
35767
|
-
setPublicKey(publicKey);
|
|
35768
|
-
setCheckout(checkoutSession);
|
|
35758
|
+
yield generatePaymentSecret();
|
|
35759
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35769
35760
|
}
|
|
35770
35761
|
else {
|
|
35771
35762
|
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
@@ -35774,14 +35765,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35774
35765
|
});
|
|
35775
35766
|
const removeDiscount = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
35776
35767
|
const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
|
|
35768
|
+
setCheckout(newCheckout);
|
|
35777
35769
|
if (step === "payment") {
|
|
35778
|
-
|
|
35779
|
-
|
|
35780
|
-
setPublicKey(publicKey);
|
|
35781
|
-
setCheckout(checkoutSession);
|
|
35782
|
-
}
|
|
35783
|
-
else {
|
|
35784
|
-
setCheckout(newCheckout);
|
|
35770
|
+
yield generatePaymentSecret();
|
|
35771
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35785
35772
|
}
|
|
35786
35773
|
});
|
|
35787
35774
|
useEffect(() => {
|
|
@@ -35795,10 +35782,10 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35795
35782
|
return (React__default.createElement(IframeWrapper, { iframeRef: iframeRef },
|
|
35796
35783
|
React__default.createElement("div", { className: "checkout-embed h-max gap-6 md:gap-0 py-4 md:py-12 flex flex-col md:grid md:grid-cols-7 " },
|
|
35797
35784
|
React__default.createElement(Appearance, { appearance: appearance, fonts: config.fonts, iframeRef: iframeRef }),
|
|
35798
|
-
React__default.createElement("div", { className: "md:col-span-4 px-4 h-max md:px-8" }, loading ? (React__default.createElement(CheckoutFormLoading, null)) : (React__default.createElement(CheckoutForm, { locale: locale, setShippingCost: setShippingCost, storeClient: storeClient, fonts: config.fonts, checkoutAppearance: appearance, currency: (_a = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _a !== void 0 ? _a : "", customer: checkout === null || checkout === void 0 ? void 0 : checkout.customer, cancelUrl: cancelUrl, checkoutId: checkoutId, clientSecret: clientSecret, onSuccess: onSuccess, onError: onError, exchangeRate: (_b = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _b !== void 0 ? _b : 1,
|
|
35785
|
+
React__default.createElement("div", { className: "md:col-span-4 px-4 h-max md:px-8" }, loading ? (React__default.createElement(CheckoutFormLoading, null)) : (React__default.createElement(CheckoutForm, { locale: locale, setShippingCost: setShippingCost, storeClient: storeClient, fonts: config.fonts, checkoutAppearance: appearance, currency: (_a = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _a !== void 0 ? _a : "", customer: checkout === null || checkout === void 0 ? void 0 : checkout.customer, cancelUrl: cancelUrl, checkoutId: checkoutId, clientSecret: clientSecret, onSuccess: onSuccess, onError: onError, exchangeRate: (_b = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _b !== void 0 ? _b : 1, publicKey: publicKey, paymentSecret: paymentSecret, paymentComponentKey: paymentComponentKey }))),
|
|
35799
35786
|
React__default.createElement("div", { className: "md:col-span-3 px-4 md:px-8 h-max order-first md:order-last" },
|
|
35800
35787
|
React__default.createElement(Toaster, null),
|
|
35801
|
-
loading ? (React__default.createElement(CheckoutSummaryLoading, null)) : (React__default.createElement(CheckoutSummary, { currency: (_c = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _c !== void 0 ? _c : "", lineItems: (_d = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) !== null && _d !== void 0 ? _d : [], shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax, onCancel: onCancel, exchangeRate: (_e = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _e !== void 0 ? _e : 1, applyDiscountCode: applyDiscountCode, appliedDiscounts: (_f = checkout === null || checkout === void 0 ? void 0 : checkout.appliedDiscounts) !== null && _f !== void 0 ? _f : [],
|
|
35788
|
+
loading ? (React__default.createElement(CheckoutSummaryLoading, null)) : (React__default.createElement(CheckoutSummary, { currency: (_c = checkout === null || checkout === void 0 ? void 0 : checkout.currency) !== null && _c !== void 0 ? _c : "", lineItems: (_d = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) !== null && _d !== void 0 ? _d : [], shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax, onCancel: onCancel, exchangeRate: (_e = checkout === null || checkout === void 0 ? void 0 : checkout.exchangeRate) !== null && _e !== void 0 ? _e : 1, applyDiscountCode: applyDiscountCode, appliedDiscounts: (_f = checkout === null || checkout === void 0 ? void 0 : checkout.appliedDiscounts) !== null && _f !== void 0 ? _f : [], removeDiscount: removeDiscount }))))));
|
|
35802
35789
|
}
|
|
35803
35790
|
const CheckoutEmbed = memo(CheckoutEmbedComponent);
|
|
35804
35791
|
|