@betterstore/react 0.3.79 → 0.3.80

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.3.80
4
+
5
+ ### Patch Changes
6
+
7
+ - bug fixes
8
+
3
9
  ## 0.3.79
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -35769,14 +35769,24 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
35769
35769
  const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
35770
35770
  setCheckout(newCheckout);
35771
35771
  if (step === "payment") {
35772
- yield generatePaymentSecret();
35773
- setPaymentComponentKey((prev) => prev + 1);
35772
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35773
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35774
+ if (newTotal !== currentTotal) {
35775
+ yield generatePaymentSecret();
35776
+ setPaymentComponentKey((prev) => prev + 1);
35777
+ }
35774
35778
  }
35775
35779
  });
35776
35780
  const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
35777
35781
  if (step === "payment") {
35778
- yield generatePaymentSecret();
35779
- setPaymentComponentKey((prev) => prev + 1);
35782
+ const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
35783
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35784
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35785
+ if (newTotal !== currentTotal) {
35786
+ yield generatePaymentSecret();
35787
+ setPaymentComponentKey((prev) => prev + 1);
35788
+ }
35789
+ setCheckout(newCheckout);
35780
35790
  }
35781
35791
  else {
35782
35792
  const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
@@ -35787,10 +35797,32 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
35787
35797
  const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
35788
35798
  setCheckout(newCheckout);
35789
35799
  if (step === "payment") {
35790
- yield generatePaymentSecret();
35791
- setPaymentComponentKey((prev) => prev + 1);
35800
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35801
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35802
+ if (newTotal !== currentTotal) {
35803
+ yield generatePaymentSecret();
35804
+ setPaymentComponentKey((prev) => prev + 1);
35805
+ }
35792
35806
  }
35793
35807
  });
35808
+ const calculateTotalWithDiscounts = (checkout) => {
35809
+ var _a, _b;
35810
+ if (!checkout)
35811
+ return 0;
35812
+ const subtotal = checkout.lineItems.reduce((acc, item) => {
35813
+ var _a, _b;
35814
+ const productItem = ((_a = item.productData) === null || _a === void 0 ? void 0 : _a.selectedVariant) || item.productData;
35815
+ return acc + ((_b = productItem === null || productItem === void 0 ? void 0 : productItem.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
35816
+ }, 0);
35817
+ const shippingPrice = (_a = checkout.shipping) !== null && _a !== void 0 ? _a : 0;
35818
+ const total = subtotal + ((_b = checkout.tax) !== null && _b !== void 0 ? _b : 0) + shippingPrice;
35819
+ const isShippingFree = subtotal > shippingPrice &&
35820
+ checkout.appliedDiscounts.some((discount) => discount.discount.type === "FREE_SHIPPING");
35821
+ const filteredDiscounts = checkout.appliedDiscounts.filter((discount) => discount.discount.type !== "FREE_SHIPPING");
35822
+ return (total -
35823
+ filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0) -
35824
+ (isShippingFree ? shippingPrice : 0));
35825
+ };
35794
35826
  React.useEffect(() => {
35795
35827
  const interval = setTimeout(() => {
35796
35828
  if (step !== "payment") {
package/dist/index.mjs CHANGED
@@ -35749,14 +35749,24 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
35749
35749
  const newCheckout = yield storeClient.applyDiscountCode(clientSecret, checkoutId, code);
35750
35750
  setCheckout(newCheckout);
35751
35751
  if (step === "payment") {
35752
- yield generatePaymentSecret();
35753
- setPaymentComponentKey((prev) => prev + 1);
35752
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35753
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35754
+ if (newTotal !== currentTotal) {
35755
+ yield generatePaymentSecret();
35756
+ setPaymentComponentKey((prev) => prev + 1);
35757
+ }
35754
35758
  }
35755
35759
  });
35756
35760
  const revalidateDiscounts = () => __awaiter(this, void 0, void 0, function* () {
35757
35761
  if (step === "payment") {
35758
- yield generatePaymentSecret();
35759
- setPaymentComponentKey((prev) => prev + 1);
35762
+ const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
35763
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35764
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35765
+ if (newTotal !== currentTotal) {
35766
+ yield generatePaymentSecret();
35767
+ setPaymentComponentKey((prev) => prev + 1);
35768
+ }
35769
+ setCheckout(newCheckout);
35760
35770
  }
35761
35771
  else {
35762
35772
  const newCheckout = yield storeClient.revalidateDiscounts(clientSecret, checkoutId);
@@ -35767,10 +35777,32 @@ function CheckoutEmbedComponent({ checkoutId, config }) {
35767
35777
  const newCheckout = yield storeClient.removeDiscount(clientSecret, checkoutId, id);
35768
35778
  setCheckout(newCheckout);
35769
35779
  if (step === "payment") {
35770
- yield generatePaymentSecret();
35771
- setPaymentComponentKey((prev) => prev + 1);
35780
+ const newTotal = calculateTotalWithDiscounts(newCheckout);
35781
+ const currentTotal = calculateTotalWithDiscounts(checkout);
35782
+ if (newTotal !== currentTotal) {
35783
+ yield generatePaymentSecret();
35784
+ setPaymentComponentKey((prev) => prev + 1);
35785
+ }
35772
35786
  }
35773
35787
  });
35788
+ const calculateTotalWithDiscounts = (checkout) => {
35789
+ var _a, _b;
35790
+ if (!checkout)
35791
+ return 0;
35792
+ const subtotal = checkout.lineItems.reduce((acc, item) => {
35793
+ var _a, _b;
35794
+ const productItem = ((_a = item.productData) === null || _a === void 0 ? void 0 : _a.selectedVariant) || item.productData;
35795
+ return acc + ((_b = productItem === null || productItem === void 0 ? void 0 : productItem.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
35796
+ }, 0);
35797
+ const shippingPrice = (_a = checkout.shipping) !== null && _a !== void 0 ? _a : 0;
35798
+ const total = subtotal + ((_b = checkout.tax) !== null && _b !== void 0 ? _b : 0) + shippingPrice;
35799
+ const isShippingFree = subtotal > shippingPrice &&
35800
+ checkout.appliedDiscounts.some((discount) => discount.discount.type === "FREE_SHIPPING");
35801
+ const filteredDiscounts = checkout.appliedDiscounts.filter((discount) => discount.discount.type !== "FREE_SHIPPING");
35802
+ return (total -
35803
+ filteredDiscounts.reduce((acc, { amount }) => acc + amount, 0) -
35804
+ (isShippingFree ? shippingPrice : 0));
35805
+ };
35774
35806
  useEffect(() => {
35775
35807
  const interval = setTimeout(() => {
35776
35808
  if (step !== "payment") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/react",
3
- "version": "0.3.79",
3
+ "version": "0.3.80",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {