@betterstore/react 0.3.79 → 0.3.81
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/index.cjs.js +74 -7
- package/dist/index.mjs +74 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.cjs.js
CHANGED
|
@@ -35750,33 +35750,58 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35750
35750
|
};
|
|
35751
35751
|
function generatePaymentSecret() {
|
|
35752
35752
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35753
|
+
console.log("[Payment Debug] Generating new payment secret");
|
|
35753
35754
|
const { paymentSecret, publicKey, checkoutSession } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35754
35755
|
setPaymentSecret(paymentSecret);
|
|
35755
35756
|
setPublicKey(publicKey);
|
|
35756
35757
|
setCheckout(checkoutSession);
|
|
35758
|
+
console.log("[Payment Debug] New payment secret generated");
|
|
35757
35759
|
});
|
|
35758
35760
|
}
|
|
35759
35761
|
React.useEffect(() => {
|
|
35760
35762
|
if (step === "payment" &&
|
|
35761
35763
|
!paymentSecret &&
|
|
35762
35764
|
!paymentSecretPromiseRef.current) {
|
|
35765
|
+
console.log("[Payment Debug] Initial payment secret generation triggered by useEffect");
|
|
35763
35766
|
paymentSecretPromiseRef.current = generatePaymentSecret().finally(() => {
|
|
35764
35767
|
paymentSecretPromiseRef.current = null;
|
|
35765
35768
|
});
|
|
35766
35769
|
}
|
|
35767
35770
|
}, [paymentSecret, step]);
|
|
35768
35771
|
const applyDiscountCode = (code) => __awaiter(this, void 0, void 0, function* () {
|
|
35772
|
+
console.log("[Payment Debug] Applying discount code:", code);
|
|
35769
35773
|
const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
|
|
35770
35774
|
setCheckout(newCheckout);
|
|
35771
35775
|
if (step === "payment") {
|
|
35772
|
-
|
|
35773
|
-
|
|
35776
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35777
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35778
|
+
console.log("[Payment Debug] Discount applied - New total:", newTotal, "Current total:", currentTotal);
|
|
35779
|
+
if (newTotal !== currentTotal) {
|
|
35780
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35781
|
+
yield generatePaymentSecret();
|
|
35782
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35783
|
+
}
|
|
35784
|
+
else {
|
|
35785
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35786
|
+
}
|
|
35774
35787
|
}
|
|
35775
35788
|
});
|
|
35776
35789
|
const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
|
|
35790
|
+
console.log("[Payment Debug] Revalidating discounts");
|
|
35777
35791
|
if (step === "payment") {
|
|
35778
|
-
yield
|
|
35779
|
-
|
|
35792
|
+
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
35793
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35794
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35795
|
+
console.log("[Payment Debug] Discounts revalidated - New total:", newTotal, "Current total:", currentTotal);
|
|
35796
|
+
if (newTotal !== currentTotal) {
|
|
35797
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35798
|
+
yield generatePaymentSecret();
|
|
35799
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35800
|
+
}
|
|
35801
|
+
else {
|
|
35802
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35803
|
+
}
|
|
35804
|
+
setCheckout(newCheckout);
|
|
35780
35805
|
}
|
|
35781
35806
|
else {
|
|
35782
35807
|
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
@@ -35784,20 +35809,62 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35784
35809
|
}
|
|
35785
35810
|
});
|
|
35786
35811
|
const removeDiscount = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
35812
|
+
console.log("[Payment Debug] Removing discount:", id);
|
|
35787
35813
|
const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
|
|
35788
35814
|
setCheckout(newCheckout);
|
|
35789
35815
|
if (step === "payment") {
|
|
35790
|
-
|
|
35791
|
-
|
|
35816
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35817
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35818
|
+
console.log("[Payment Debug] Discount removed - New total:", newTotal, "Current total:", currentTotal);
|
|
35819
|
+
if (newTotal !== currentTotal) {
|
|
35820
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35821
|
+
yield generatePaymentSecret();
|
|
35822
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35823
|
+
}
|
|
35824
|
+
else {
|
|
35825
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35826
|
+
}
|
|
35792
35827
|
}
|
|
35793
35828
|
});
|
|
35829
|
+
const calculateTotalWithDiscounts = (checkout) => {
|
|
35830
|
+
var _a, _b;
|
|
35831
|
+
if (!checkout)
|
|
35832
|
+
return 0;
|
|
35833
|
+
const subtotal = checkout.lineItems.reduce((acc, item) => {
|
|
35834
|
+
var _a, _b;
|
|
35835
|
+
const productItem = ((_a = item.productData) === null || _a === void 0 ? void 0 : _a.selectedVariant) || item.productData;
|
|
35836
|
+
return acc + ((_b = productItem === null || productItem === void 0 ? void 0 : productItem.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
|
|
35837
|
+
}, 0);
|
|
35838
|
+
const shippingPrice = (_a = checkout.shipping) !== null && _a !== void 0 ? _a : 0;
|
|
35839
|
+
const total = subtotal + ((_b = checkout.tax) !== null && _b !== void 0 ? _b : 0) + shippingPrice;
|
|
35840
|
+
const isShippingFree = subtotal > shippingPrice &&
|
|
35841
|
+
checkout.appliedDiscounts.some((discount) => discount.discount.type === "FREE_SHIPPING");
|
|
35842
|
+
const filteredDiscounts = checkout.appliedDiscounts.filter((discount) => discount.discount.type !== "FREE_SHIPPING");
|
|
35843
|
+
const finalTotal = total -
|
|
35844
|
+
filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0) -
|
|
35845
|
+
(isShippingFree ? shippingPrice : 0);
|
|
35846
|
+
console.log("[Payment Debug] Total calculation:", {
|
|
35847
|
+
subtotal,
|
|
35848
|
+
shippingPrice,
|
|
35849
|
+
tax: checkout.tax,
|
|
35850
|
+
isShippingFree,
|
|
35851
|
+
discountAmount: filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0),
|
|
35852
|
+
finalTotal,
|
|
35853
|
+
});
|
|
35854
|
+
return finalTotal;
|
|
35855
|
+
};
|
|
35794
35856
|
React.useEffect(() => {
|
|
35857
|
+
console.log("[Payment Debug] Setting up discount revalidation interval");
|
|
35795
35858
|
const interval = setTimeout(() => {
|
|
35796
35859
|
if (step !== "payment") {
|
|
35860
|
+
console.log("[Payment Debug] Interval triggered revalidation");
|
|
35797
35861
|
revalidateDiscounts();
|
|
35798
35862
|
}
|
|
35799
35863
|
}, 1000 * 5);
|
|
35800
|
-
return () =>
|
|
35864
|
+
return () => {
|
|
35865
|
+
console.log("[Payment Debug] Clearing revalidation interval");
|
|
35866
|
+
clearInterval(interval);
|
|
35867
|
+
};
|
|
35801
35868
|
}, []);
|
|
35802
35869
|
return (React.createElement(IframeWrapper, { iframeRef: iframeRef },
|
|
35803
35870
|
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 " },
|
package/dist/index.mjs
CHANGED
|
@@ -35730,33 +35730,58 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35730
35730
|
};
|
|
35731
35731
|
function generatePaymentSecret() {
|
|
35732
35732
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35733
|
+
console.log("[Payment Debug] Generating new payment secret");
|
|
35733
35734
|
const { paymentSecret, publicKey, checkoutSession } = yield storeClient.generateCheckoutPaymentSecret(clientSecret, checkoutId);
|
|
35734
35735
|
setPaymentSecret(paymentSecret);
|
|
35735
35736
|
setPublicKey(publicKey);
|
|
35736
35737
|
setCheckout(checkoutSession);
|
|
35738
|
+
console.log("[Payment Debug] New payment secret generated");
|
|
35737
35739
|
});
|
|
35738
35740
|
}
|
|
35739
35741
|
useEffect(() => {
|
|
35740
35742
|
if (step === "payment" &&
|
|
35741
35743
|
!paymentSecret &&
|
|
35742
35744
|
!paymentSecretPromiseRef.current) {
|
|
35745
|
+
console.log("[Payment Debug] Initial payment secret generation triggered by useEffect");
|
|
35743
35746
|
paymentSecretPromiseRef.current = generatePaymentSecret().finally(() => {
|
|
35744
35747
|
paymentSecretPromiseRef.current = null;
|
|
35745
35748
|
});
|
|
35746
35749
|
}
|
|
35747
35750
|
}, [paymentSecret, step]);
|
|
35748
35751
|
const applyDiscountCode = (code) => __awaiter(this, void 0, void 0, function* () {
|
|
35752
|
+
console.log("[Payment Debug] Applying discount code:", code);
|
|
35749
35753
|
const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
|
|
35750
35754
|
setCheckout(newCheckout);
|
|
35751
35755
|
if (step === "payment") {
|
|
35752
|
-
|
|
35753
|
-
|
|
35756
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35757
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35758
|
+
console.log("[Payment Debug] Discount applied - New total:", newTotal, "Current total:", currentTotal);
|
|
35759
|
+
if (newTotal !== currentTotal) {
|
|
35760
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35761
|
+
yield generatePaymentSecret();
|
|
35762
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35763
|
+
}
|
|
35764
|
+
else {
|
|
35765
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35766
|
+
}
|
|
35754
35767
|
}
|
|
35755
35768
|
});
|
|
35756
35769
|
const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
|
|
35770
|
+
console.log("[Payment Debug] Revalidating discounts");
|
|
35757
35771
|
if (step === "payment") {
|
|
35758
|
-
yield
|
|
35759
|
-
|
|
35772
|
+
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
35773
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35774
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35775
|
+
console.log("[Payment Debug] Discounts revalidated - New total:", newTotal, "Current total:", currentTotal);
|
|
35776
|
+
if (newTotal !== currentTotal) {
|
|
35777
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35778
|
+
yield generatePaymentSecret();
|
|
35779
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35780
|
+
}
|
|
35781
|
+
else {
|
|
35782
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35783
|
+
}
|
|
35784
|
+
setCheckout(newCheckout);
|
|
35760
35785
|
}
|
|
35761
35786
|
else {
|
|
35762
35787
|
const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
|
|
@@ -35764,20 +35789,62 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
|
|
|
35764
35789
|
}
|
|
35765
35790
|
});
|
|
35766
35791
|
const removeDiscount = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
35792
|
+
console.log("[Payment Debug] Removing discount:", id);
|
|
35767
35793
|
const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
|
|
35768
35794
|
setCheckout(newCheckout);
|
|
35769
35795
|
if (step === "payment") {
|
|
35770
|
-
|
|
35771
|
-
|
|
35796
|
+
const newTotal = calculateTotalWithDiscounts(newCheckout);
|
|
35797
|
+
const currentTotal = calculateTotalWithDiscounts(checkout);
|
|
35798
|
+
console.log("[Payment Debug] Discount removed - New total:", newTotal, "Current total:", currentTotal);
|
|
35799
|
+
if (newTotal !== currentTotal) {
|
|
35800
|
+
console.log("[Payment Debug] Total changed, regenerating payment secret");
|
|
35801
|
+
yield generatePaymentSecret();
|
|
35802
|
+
setPaymentComponentKey((prev) => prev + 1);
|
|
35803
|
+
}
|
|
35804
|
+
else {
|
|
35805
|
+
console.log("[Payment Debug] Total unchanged, skipping payment secret regeneration");
|
|
35806
|
+
}
|
|
35772
35807
|
}
|
|
35773
35808
|
});
|
|
35809
|
+
const calculateTotalWithDiscounts = (checkout) => {
|
|
35810
|
+
var _a, _b;
|
|
35811
|
+
if (!checkout)
|
|
35812
|
+
return 0;
|
|
35813
|
+
const subtotal = checkout.lineItems.reduce((acc, item) => {
|
|
35814
|
+
var _a, _b;
|
|
35815
|
+
const productItem = ((_a = item.productData) === null || _a === void 0 ? void 0 : _a.selectedVariant) || item.productData;
|
|
35816
|
+
return acc + ((_b = productItem === null || productItem === void 0 ? void 0 : productItem.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
|
|
35817
|
+
}, 0);
|
|
35818
|
+
const shippingPrice = (_a = checkout.shipping) !== null && _a !== void 0 ? _a : 0;
|
|
35819
|
+
const total = subtotal + ((_b = checkout.tax) !== null && _b !== void 0 ? _b : 0) + shippingPrice;
|
|
35820
|
+
const isShippingFree = subtotal > shippingPrice &&
|
|
35821
|
+
checkout.appliedDiscounts.some((discount) => discount.discount.type === "FREE_SHIPPING");
|
|
35822
|
+
const filteredDiscounts = checkout.appliedDiscounts.filter((discount) => discount.discount.type !== "FREE_SHIPPING");
|
|
35823
|
+
const finalTotal = total -
|
|
35824
|
+
filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0) -
|
|
35825
|
+
(isShippingFree ? shippingPrice : 0);
|
|
35826
|
+
console.log("[Payment Debug] Total calculation:", {
|
|
35827
|
+
subtotal,
|
|
35828
|
+
shippingPrice,
|
|
35829
|
+
tax: checkout.tax,
|
|
35830
|
+
isShippingFree,
|
|
35831
|
+
discountAmount: filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0),
|
|
35832
|
+
finalTotal,
|
|
35833
|
+
});
|
|
35834
|
+
return finalTotal;
|
|
35835
|
+
};
|
|
35774
35836
|
useEffect(() => {
|
|
35837
|
+
console.log("[Payment Debug] Setting up discount revalidation interval");
|
|
35775
35838
|
const interval = setTimeout(() => {
|
|
35776
35839
|
if (step !== "payment") {
|
|
35840
|
+
console.log("[Payment Debug] Interval triggered revalidation");
|
|
35777
35841
|
revalidateDiscounts();
|
|
35778
35842
|
}
|
|
35779
35843
|
}, 1000 * 5);
|
|
35780
|
-
return () =>
|
|
35844
|
+
return () => {
|
|
35845
|
+
console.log("[Payment Debug] Clearing revalidation interval");
|
|
35846
|
+
clearInterval(interval);
|
|
35847
|
+
};
|
|
35781
35848
|
}, []);
|
|
35782
35849
|
return (React__default.createElement(IframeWrapper, { iframeRef: iframeRef },
|
|
35783
35850
|
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 " },
|