@nextblock-cms/ecom 0.8.6 → 0.8.7
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/index.cjs.js +1 -1
- package/index.es.js +136 -533
- package/lib/CurrencyProvider.cjs.js +1 -0
- package/lib/CurrencyProvider.es.js +99 -0
- package/lib/cart-store.cjs.js +1 -0
- package/{cart-store.es.js → lib/cart-store.es.js} +13 -13
- package/lib/components/AccountNavigationMenu.cjs.js +1 -0
- package/lib/components/AccountNavigationMenu.es.js +44 -0
- package/lib/components/AddToCartButton.cjs.js +1 -0
- package/{AddToCartButton-ffE0DhHu.js → lib/components/AddToCartButton.es.js} +14 -13
- package/lib/components/Cart.cjs.js +1 -0
- package/lib/components/Cart.es.js +148 -0
- package/lib/components/CartDrawer.cjs.js +1 -0
- package/{components → lib/components}/CartDrawer.es.js +15 -14
- package/{components → lib/components}/CartIcon.cjs.js +1 -1
- package/{components → lib/components}/CartIcon.es.js +1 -0
- package/lib/components/Checkout.cjs.js +1 -0
- package/{components → lib/components}/Checkout.es.js +330 -363
- package/lib/components/CouponForm.cjs.js +1 -0
- package/lib/components/CouponForm.es.js +136 -0
- package/lib/components/CurrencySwitcher.cjs.js +1 -0
- package/{components → lib/components}/CurrencySwitcher.es.js +5 -4
- package/lib/components/CustomerProfileForm.cjs.js +1 -0
- package/lib/components/CustomerProfileForm.es.js +355 -0
- package/lib/components/FeaturedProduct.cjs.js +1 -0
- package/{components → lib/components}/FeaturedProduct.es.js +9 -8
- package/lib/components/InvoiceDocument.cjs.js +1 -0
- package/lib/components/InvoiceDocument.es.js +196 -0
- package/lib/components/InvoiceViewerShell.cjs.js +29 -0
- package/lib/components/InvoiceViewerShell.es.js +100 -0
- package/lib/components/ProductCard.cjs.js +1 -0
- package/lib/components/ProductCard.es.js +119 -0
- package/lib/components/ProductDetailsLayout.cjs.js +1 -0
- package/lib/components/ProductDetailsLayout.es.js +361 -0
- package/lib/components/ProductGallery.cjs.js +1 -0
- package/lib/components/ProductGallery.es.js +41 -0
- package/lib/components/ProductGrid.cjs.js +1 -0
- package/lib/components/ProductGrid.es.js +19 -0
- package/lib/components/ShippingEstimator.cjs.js +1 -0
- package/lib/components/ShippingEstimator.es.js +125 -0
- package/lib/components/SubscriptionSelector.cjs.js +1 -0
- package/lib/components/SubscriptionSelector.es.js +140 -0
- package/lib/countries.cjs.js +1 -0
- package/lib/countries.es.js +77 -0
- package/lib/coupon-server.cjs.js +1 -0
- package/{coupon-server-DUKVtyYs.js → lib/coupon-server.es.js} +68 -82
- package/lib/coupons.cjs.js +1 -0
- package/lib/coupons.es.js +17 -0
- package/lib/currency-store.cjs.js +1 -0
- package/lib/currency-store.es.js +27 -0
- package/lib/currency-sync.cjs.js +1 -0
- package/lib/currency-sync.es.js +180 -0
- package/lib/customer-addresses.cjs.js +1 -0
- package/lib/customer-addresses.es.js +116 -0
- package/lib/customer-orders.cjs.js +1 -0
- package/lib/customer-orders.es.js +45 -0
- package/lib/customer.cjs.js +1 -0
- package/{customer-C3xccjUg.js → lib/customer.es.js} +15 -14
- package/lib/export-helpers.cjs.js +1 -0
- package/lib/export-helpers.es.js +56 -0
- package/lib/factory.cjs.js +1 -0
- package/lib/factory.es.js +8 -0
- package/lib/freemius-coupons.cjs.js +1 -0
- package/lib/freemius-coupons.es.js +272 -0
- package/lib/freemius-order-sync.cjs.js +1 -0
- package/lib/freemius-order-sync.es.js +284 -0
- package/lib/inventory-settings.cjs.js +1 -0
- package/lib/inventory-settings.es.js +86 -0
- package/lib/invoice-server.cjs.js +33 -0
- package/lib/invoice-server.es.js +141 -0
- package/lib/invoice-ui.cjs.js +1 -0
- package/lib/invoice-ui.es.js +71 -0
- package/lib/invoice.cjs.js +1 -0
- package/lib/invoice.es.js +102 -0
- package/lib/order-inventory.cjs.js +61 -0
- package/lib/order-inventory.es.js +128 -0
- package/lib/order-tax-details.cjs.js +1 -0
- package/lib/order-tax-details.es.js +164 -0
- package/lib/pages/cms/coupons/CouponEditorForm.cjs.js +1 -0
- package/lib/pages/cms/coupons/CouponEditorForm.es.js +189 -0
- package/lib/pages/cms/coupons/CouponsPage.cjs.js +5 -0
- package/lib/pages/cms/coupons/CouponsPage.es.js +171 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.cjs.js +1 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.es.js +35 -0
- package/lib/pages/cms/coupons/EditCouponPage.cjs.js +1 -0
- package/lib/pages/cms/coupons/EditCouponPage.es.js +48 -0
- package/lib/pages/cms/coupons/ProductScopePicker.cjs.js +1 -0
- package/lib/pages/cms/coupons/ProductScopePicker.es.js +120 -0
- package/lib/pages/cms/coupons/actions.cjs.js +1 -0
- package/lib/pages/cms/coupons/actions.es.js +128 -0
- package/lib/pages/cms/coupons/product-options.cjs.js +1 -0
- package/lib/pages/cms/coupons/product-options.es.js +27 -0
- package/lib/pages/cms/orders/ExportReportsDialog.cjs.js +1 -0
- package/lib/pages/cms/orders/ExportReportsDialog.es.js +130 -0
- package/lib/pages/cms/orders/OrderDetailPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderDetailPage.es.js +137 -0
- package/lib/pages/cms/orders/OrderPrintButton.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderPrintButton.es.js +22 -0
- package/lib/pages/cms/orders/OrderStatusForm.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderStatusForm.es.js +71 -0
- package/lib/pages/cms/orders/OrdersPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrdersPage.es.js +104 -0
- package/lib/pages/cms/orders/actions.cjs.js +7 -0
- package/lib/pages/cms/orders/actions.es.js +71 -0
- package/lib/pages/cms/orders/export-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/export-actions.es.js +27 -0
- package/lib/pages/cms/orders/server-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/server-actions.es.js +55 -0
- package/lib/pages/cms/payments/PaymentsClient.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsClient.es.js +188 -0
- package/lib/pages/cms/payments/PaymentsPage.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsPage.es.js +29 -0
- package/lib/pages/cms/payments/actions.cjs.js +1 -0
- package/lib/pages/cms/payments/actions.es.js +23 -0
- package/lib/pages/cms/payments/queries.cjs.js +1 -0
- package/lib/pages/cms/payments/queries.es.js +30 -0
- package/lib/pages/cms/products/ProductsPage.cjs.js +1 -0
- package/lib/pages/cms/products/ProductsPage.es.js +46 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.es.js +156 -0
- package/lib/pages/cms/products/actions.cjs.js +44 -0
- package/lib/pages/cms/products/actions.es.js +175 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.es.js +48 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.es.js +291 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.es.js +36 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.cjs.js +1 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.es.js +263 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.cjs.js +1 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.es.js +114 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +107 -0
- package/lib/pages/cms/products/components/DeleteProductButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/DeleteProductButton.es.js +48 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.cjs.js +1 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.es.js +86 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.es.js +160 -0
- package/lib/pages/cms/products/components/ProductForm.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductForm.es.js +702 -0
- package/lib/pages/cms/products/components/ProductMediaManager.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductMediaManager.es.js +104 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.es.js +228 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.es.js +137 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.es.js +36 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.es.js +40 -0
- package/lib/pages/cms/products/components/VariationsEditor.cjs.js +1 -0
- package/lib/pages/cms/products/components/VariationsEditor.es.js +408 -0
- package/lib/pages/cms/products/inventory/InventoryPage.cjs.js +1 -0
- package/lib/pages/cms/products/inventory/InventoryPage.es.js +16 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.cjs.js +2 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.es.js +214 -0
- package/lib/pages/cms/products/inventory/actions.cjs.js +3 -0
- package/lib/pages/cms/products/inventory/actions.es.js +95 -0
- package/lib/pages/cms/products/new/NewProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/new/NewProductPage.es.js +76 -0
- package/lib/pages/cms/products/product-price-sync.cjs.js +1 -0
- package/lib/pages/cms/products/product-price-sync.es.js +98 -0
- package/lib/pages/cms/products/server-actions.cjs.js +1 -0
- package/lib/pages/cms/products/server-actions.es.js +193 -0
- package/lib/pages/cms/shipping/ShippingPage.cjs.js +5 -0
- package/lib/pages/cms/shipping/ShippingPage.es.js +211 -0
- package/lib/pages/cms/shipping/components/RateForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/RateForm.es.js +441 -0
- package/lib/pages/cms/shipping/components/ZoneForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/ZoneForm.es.js +228 -0
- package/lib/pages/cms/shipping/server-actions.cjs.js +1 -0
- package/lib/pages/cms/shipping/server-actions.es.js +159 -0
- package/lib/pages/cms/taxes/TaxesPage.cjs.js +1 -0
- package/lib/pages/cms/taxes/TaxesPage.es.js +172 -0
- package/lib/pages/cms/taxes/actions.cjs.js +1 -0
- package/lib/pages/cms/taxes/actions.es.js +57 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.cjs.js +1 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.es.js +100 -0
- package/lib/product-actions.cjs.js +237 -0
- package/lib/product-actions.es.js +524 -0
- package/lib/product-context.cjs.js +1 -0
- package/lib/product-context.es.js +16 -0
- package/lib/product-schema.cjs.js +1 -0
- package/lib/product-schema.es.js +134 -0
- package/lib/providers/freemius.cjs.js +5 -0
- package/lib/providers/freemius.es.js +538 -0
- package/lib/providers/stripe.cjs.js +1 -0
- package/lib/providers/stripe.es.js +473 -0
- package/lib/server-actions/coupon-actions.cjs.js +1 -0
- package/lib/server-actions/coupon-actions.es.js +23 -0
- package/lib/server-actions/customer-actions.cjs.js +1 -0
- package/lib/server-actions/customer-actions.es.js +30 -0
- package/lib/server-actions/product-actions.cjs.js +1 -0
- package/{server-actions → lib/server-actions}/product-actions.es.js +5 -4
- package/lib/server-actions/shipping-actions.cjs.js +1 -0
- package/{shipping-actions-BnHjlo2q.js → lib/server-actions/shipping-actions.es.js} +9 -7
- package/lib/server-actions/tax-actions.cjs.js +1 -0
- package/lib/server-actions/tax-actions.es.js +40 -0
- package/lib/shared-inventory.cjs.js +15 -0
- package/lib/shared-inventory.es.js +171 -0
- package/lib/shipping/resolver.cjs.js +7 -0
- package/lib/shipping/resolver.es.js +62 -0
- package/lib/shipping-rate-currency.cjs.js +1 -0
- package/lib/shipping-rate-currency.es.js +72 -0
- package/lib/states.cjs.js +1 -0
- package/{states-DlHJezQ1.js → lib/states.es.js} +27 -100
- package/lib/stripe/checkout.cjs.js +1 -0
- package/lib/stripe/checkout.es.js +182 -0
- package/lib/stripe/client.cjs.js +1 -0
- package/lib/stripe/client.es.js +7 -0
- package/lib/stripe/order-sync.cjs.js +1 -0
- package/lib/stripe/order-sync.es.js +130 -0
- package/lib/stripe/webhooks.cjs.js +1 -0
- package/lib/stripe/webhooks.es.js +30 -0
- package/lib/tax-calculation.cjs.js +1 -0
- package/lib/tax-calculation.es.js +101 -0
- package/lib/trials.cjs.js +1 -0
- package/{trials-RDcRAZKx.js → lib/trials.es.js} +9 -9
- package/{use-cart.es.js → lib/use-cart.es.js} +1 -0
- package/lib/zod-config.cjs.js +1 -0
- package/lib/zod-config.es.js +6 -0
- package/package.json +4 -4
- package/server.cjs.js +1 -110
- package/server.es.js +178 -6052
- package/AddToCartButton-DsSEH9yp.cjs +0 -1
- package/CouponForm-BI_5SBHQ.js +0 -152
- package/CouponForm-D_uu2bLP.cjs +0 -1
- package/CurrencyProvider-Cg-Q6ucj.cjs +0 -1
- package/CurrencyProvider-OzlRN96V.js +0 -120
- package/CurrencyProvider.cjs.js +0 -1
- package/CurrencyProvider.es.js +0 -10
- package/ProductDetailsLayout-BxJH_BxR.cjs +0 -1
- package/ProductDetailsLayout-Dg8dRTYp.js +0 -530
- package/ProductGrid-BdL7iglO.cjs +0 -1
- package/ProductGrid-D3AWM8sr.js +0 -130
- package/actions-AHmBrS5e.cjs +0 -48
- package/actions-j5fuSZX-.js +0 -820
- package/cart-store.cjs.js +0 -1
- package/components/Cart.cjs.js +0 -1
- package/components/Cart.es.js +0 -259
- package/components/CartDrawer.cjs.js +0 -1
- package/components/Checkout.cjs.js +0 -1
- package/components/CurrencySwitcher.cjs.js +0 -1
- package/components/FeaturedProduct.cjs.js +0 -1
- package/components/ProductDetailsLayout.cjs.js +0 -1
- package/components/ProductDetailsLayout.es.js +0 -19
- package/components/ProductGrid.cjs.js +0 -1
- package/components/ProductGrid.es.js +0 -6
- package/coupon-server-C9ySm3I5.cjs +0 -1
- package/customer-C-UPvrYN.cjs +0 -1
- package/invoice-ui-CAnJPu6n.cjs +0 -29
- package/invoice-ui-DtpbTJE1.js +0 -2886
- package/product-actions-CONBF8i6.cjs +0 -251
- package/product-actions-DjlIWCMH.js +0 -691
- package/resolver-Fz_FKsfX.cjs +0 -7
- package/resolver-cfrCaHpE.js +0 -128
- package/server-actions/product-actions.cjs.js +0 -1
- package/shipping-actions-D6zt20gg.cjs +0 -1
- package/states-CWYRxV4B.cjs +0 -1
- package/tax-calculation-Dab89aHd.js +0 -181
- package/tax-calculation-MkDKOEkL.cjs +0 -1
- package/trials-DvZaOtNy.cjs +0 -1
- /package/{components → lib/components}/SimpleTiptapRenderer.cjs.js +0 -0
- /package/{components → lib/components}/SimpleTiptapRenderer.es.js +0 -0
- /package/{currency-constants.cjs.js → lib/currency-constants.cjs.js} +0 -0
- /package/{currency-constants.es.js → lib/currency-constants.es.js} +0 -0
- /package/{currency-rest-client-uolJxUkL.cjs → lib/currency-rest-client.cjs.js} +0 -0
- /package/{currency-rest-client-CwoqdgAP.js → lib/currency-rest-client.es.js} +0 -0
- /package/{currency.cjs.js → lib/currency.cjs.js} +0 -0
- /package/{currency.es.js → lib/currency.es.js} +0 -0
- /package/{types.cjs.js → lib/types.cjs.js} +0 -0
- /package/{types.es.js → lib/types.es.js} +0 -0
- /package/{use-cart.cjs.js → lib/use-cart.cjs.js} +0 -0
- /package/{variation-utils.cjs.js → lib/variation-utils.cjs.js} +0 -0
- /package/{variation-utils.es.js → lib/variation-utils.es.js} +0 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { minorUnitAmountToMajor as w } from "@nextblock-cms/utils";
|
|
2
|
+
import { resolveFreemiusCheckoutCredentials as S, readFreemiusEnvValue as h } from "./providers/freemius.es.js";
|
|
3
|
+
function f(e) {
|
|
4
|
+
if (!e)
|
|
5
|
+
return null;
|
|
6
|
+
const o = new Date(e);
|
|
7
|
+
return Number.isNaN(o.getTime()) ? e : o.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "");
|
|
8
|
+
}
|
|
9
|
+
function C(e) {
|
|
10
|
+
return e === "percent" ? "percentage" : "dollar";
|
|
11
|
+
}
|
|
12
|
+
function E(e) {
|
|
13
|
+
return S(e).apiKey || h("FREEMIUS_API_KEY");
|
|
14
|
+
}
|
|
15
|
+
async function l(e) {
|
|
16
|
+
const o = E(e.productId);
|
|
17
|
+
if (!o)
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Missing Freemius API bearer token for product ${e.productId}. Set FREEMIUS_API_KEY or FREEMIUS_CHECKOUT_PRODUCTS_JSON[${e.productId}].apiKey.`
|
|
20
|
+
);
|
|
21
|
+
const s = e.couponId ? `/${e.couponId}` : "", t = await fetch(
|
|
22
|
+
`https://api.freemius.com/v1/products/${e.productId}/coupons${s}.json`,
|
|
23
|
+
{
|
|
24
|
+
method: e.method,
|
|
25
|
+
headers: {
|
|
26
|
+
Authorization: `Bearer ${o}`,
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
Accept: "application/json"
|
|
29
|
+
},
|
|
30
|
+
body: e.method === "DELETE" ? void 0 : JSON.stringify(e.body || {})
|
|
31
|
+
}
|
|
32
|
+
), n = await t.text(), r = n ? JSON.parse(n) : null;
|
|
33
|
+
if (!t.ok)
|
|
34
|
+
throw new Error(
|
|
35
|
+
r?.message || r?.error?.message || `Freemius coupon request failed with ${t.status}`
|
|
36
|
+
);
|
|
37
|
+
return r;
|
|
38
|
+
}
|
|
39
|
+
function y(e, o) {
|
|
40
|
+
return {
|
|
41
|
+
code: e.code,
|
|
42
|
+
discount: e.discount_type === "fixed" ? w(e.discount_amount, "USD") : e.discount_amount,
|
|
43
|
+
discount_type: C(e.discount_type),
|
|
44
|
+
plans: o.planIds.length > 0 ? o.planIds.join(",") : null,
|
|
45
|
+
licenses: null,
|
|
46
|
+
billing_cycles: null,
|
|
47
|
+
start_date: f(e.starts_at),
|
|
48
|
+
end_date: f(e.ends_at),
|
|
49
|
+
redemptions_limit: e.redemption_limit ?? null,
|
|
50
|
+
has_renewals_discount: !1,
|
|
51
|
+
has_addons_discount: !1,
|
|
52
|
+
is_one_per_user: !1,
|
|
53
|
+
is_active: e.is_active,
|
|
54
|
+
user_type: "all"
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async function P(e, o) {
|
|
58
|
+
if (o.provider_scope === "stripe")
|
|
59
|
+
return [];
|
|
60
|
+
const { data: s, error: t } = await e.from("coupon_products").select("product_id").eq("coupon_id", o.id);
|
|
61
|
+
if (t)
|
|
62
|
+
throw new Error(t.message);
|
|
63
|
+
const n = (s || []).map((c) => c.product_id);
|
|
64
|
+
let r = e.from("products").select("id, freemius_product_id, freemius_plan_id").eq("payment_provider", "freemius").not("freemius_product_id", "is", null);
|
|
65
|
+
n.length > 0 && (r = r.in("id", n));
|
|
66
|
+
const { data: u, error: i } = await r;
|
|
67
|
+
if (i)
|
|
68
|
+
throw new Error(i.message);
|
|
69
|
+
const d = /* @__PURE__ */ new Map();
|
|
70
|
+
for (const c of u || []) {
|
|
71
|
+
const a = String(c.freemius_product_id || "").trim();
|
|
72
|
+
if (!a)
|
|
73
|
+
continue;
|
|
74
|
+
const p = d.get(a) ?? {
|
|
75
|
+
freemiusProductId: a,
|
|
76
|
+
productId: c.id,
|
|
77
|
+
planIds: []
|
|
78
|
+
};
|
|
79
|
+
n.length > 0 && c.freemius_plan_id && p.planIds.push(String(c.freemius_plan_id)), d.set(a, p);
|
|
80
|
+
}
|
|
81
|
+
return [...d.values()].map((c) => ({
|
|
82
|
+
...c,
|
|
83
|
+
planIds: [...new Set(c.planIds)]
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
async function q(e, o) {
|
|
87
|
+
const { data: s } = await e.from("coupon_freemius_mappings").select("sync_status, sync_error").eq("coupon_id", o), t = s || [];
|
|
88
|
+
if (t.length === 0) {
|
|
89
|
+
await e.from("coupons").update({
|
|
90
|
+
freemius_sync_status: "not_required",
|
|
91
|
+
freemius_sync_error: null
|
|
92
|
+
}).eq("id", o);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const n = t.find((r) => r.sync_status === "failed");
|
|
96
|
+
await e.from("coupons").update({
|
|
97
|
+
freemius_sync_status: n ? "failed" : "synced",
|
|
98
|
+
freemius_sync_error: n?.sync_error ?? null
|
|
99
|
+
}).eq("id", o);
|
|
100
|
+
}
|
|
101
|
+
async function O(e) {
|
|
102
|
+
const { data: o, error: s } = await e.client.from("coupons").select(
|
|
103
|
+
"id, code, name, internal_note, provider_scope, discount_type, discount_amount, is_active, starts_at, ends_at, redemption_limit"
|
|
104
|
+
).eq("id", e.couponId).single();
|
|
105
|
+
if (s || !o)
|
|
106
|
+
return {
|
|
107
|
+
success: !1,
|
|
108
|
+
error: s?.message || "Coupon not found"
|
|
109
|
+
};
|
|
110
|
+
const t = o, n = await P(e.client, t);
|
|
111
|
+
await e.client.from("coupons").update({
|
|
112
|
+
freemius_sync_status: n.length > 0 ? "pending" : "not_required",
|
|
113
|
+
freemius_sync_error: null
|
|
114
|
+
}).eq("id", t.id);
|
|
115
|
+
for (const r of n) {
|
|
116
|
+
const { data: u } = await e.client.from("coupon_freemius_mappings").select("id, freemius_coupon_id").eq("coupon_id", t.id).eq("freemius_product_id", r.freemiusProductId).maybeSingle(), i = y(t, r);
|
|
117
|
+
try {
|
|
118
|
+
const d = await l({
|
|
119
|
+
productId: r.freemiusProductId,
|
|
120
|
+
method: u?.freemius_coupon_id ? "PUT" : "POST",
|
|
121
|
+
couponId: u?.freemius_coupon_id ?? null,
|
|
122
|
+
body: i
|
|
123
|
+
}), c = d?.coupon ?? d;
|
|
124
|
+
await e.client.from("coupon_freemius_mappings").upsert(
|
|
125
|
+
{
|
|
126
|
+
id: u?.id,
|
|
127
|
+
coupon_id: t.id,
|
|
128
|
+
product_id: r.productId,
|
|
129
|
+
freemius_product_id: r.freemiusProductId,
|
|
130
|
+
freemius_coupon_id: c?.id ? String(c.id) : u?.freemius_coupon_id ?? null,
|
|
131
|
+
freemius_coupon_code: t.code,
|
|
132
|
+
sync_status: "synced",
|
|
133
|
+
sync_error: null,
|
|
134
|
+
remote_payload: d,
|
|
135
|
+
last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
136
|
+
},
|
|
137
|
+
{ onConflict: "coupon_id,freemius_product_id" }
|
|
138
|
+
);
|
|
139
|
+
} catch (d) {
|
|
140
|
+
await e.client.from("coupon_freemius_mappings").upsert(
|
|
141
|
+
{
|
|
142
|
+
id: u?.id,
|
|
143
|
+
coupon_id: t.id,
|
|
144
|
+
product_id: r.productId,
|
|
145
|
+
freemius_product_id: r.freemiusProductId,
|
|
146
|
+
freemius_coupon_id: u?.freemius_coupon_id ?? null,
|
|
147
|
+
freemius_coupon_code: t.code,
|
|
148
|
+
sync_status: "failed",
|
|
149
|
+
sync_error: d.message || "Freemius sync failed",
|
|
150
|
+
last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
151
|
+
},
|
|
152
|
+
{ onConflict: "coupon_id,freemius_product_id" }
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return await q(e.client, t.id), {
|
|
157
|
+
success: !0,
|
|
158
|
+
targetCount: n.length
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function v(e) {
|
|
162
|
+
const o = (e.sku || "").toUpperCase().replace(/[^A-Z0-9]/g, "");
|
|
163
|
+
return o ? `SALE${o}`.slice(0, 32) : `SALE${e.productId.replace(/[^A-Za-z0-9]/g, "").toUpperCase()}`.slice(0, 32);
|
|
164
|
+
}
|
|
165
|
+
function F(e, o) {
|
|
166
|
+
if (typeof e != "number" || e <= 0 || typeof o != "number" || o < 0 || o >= e)
|
|
167
|
+
return null;
|
|
168
|
+
const s = Math.round((1 - o / e) * 100);
|
|
169
|
+
return s <= 0 || s > 100 ? null : s;
|
|
170
|
+
}
|
|
171
|
+
async function b(e) {
|
|
172
|
+
const { data: o, error: s } = await e.client.from("products").select(
|
|
173
|
+
"id, sku, price, sale_price, sale_start_at, sale_end_at, payment_provider, freemius_product_id, freemius_plan_id"
|
|
174
|
+
).eq("id", e.productId).maybeSingle();
|
|
175
|
+
if (s || !o)
|
|
176
|
+
return { success: !1, error: s?.message || "Product not found" };
|
|
177
|
+
const t = o, n = String(t.freemius_product_id || "").trim();
|
|
178
|
+
if (t.payment_provider !== "freemius" || !n)
|
|
179
|
+
return { success: !0, skipped: !0, reason: "not_a_freemius_product" };
|
|
180
|
+
const r = F(t.price, t.sale_price), u = r !== null, { data: i } = await e.client.from("product_freemius_sale_coupons").select("id, freemius_coupon_id, freemius_coupon_code, discount_percent, is_active").eq("product_id", t.id).maybeSingle();
|
|
181
|
+
if (!u && !i?.freemius_coupon_id)
|
|
182
|
+
return i?.id && await e.client.from("product_freemius_sale_coupons").update({ is_active: !1, updated_at: (/* @__PURE__ */ new Date()).toISOString() }).eq("id", i.id), { success: !0, active: !1 };
|
|
183
|
+
const d = i?.freemius_coupon_code || v({ sku: t.sku, productId: t.id }), c = t.freemius_plan_id ? [String(t.freemius_plan_id)] : [], a = r ?? i?.discount_percent ?? 1, p = {
|
|
184
|
+
id: t.id,
|
|
185
|
+
code: d,
|
|
186
|
+
name: `Scheduled sale ${t.sku || t.id}`,
|
|
187
|
+
discount_type: "percent",
|
|
188
|
+
discount_amount: a,
|
|
189
|
+
is_active: u,
|
|
190
|
+
starts_at: t.sale_start_at,
|
|
191
|
+
ends_at: t.sale_end_at,
|
|
192
|
+
redemption_limit: null
|
|
193
|
+
}, g = {
|
|
194
|
+
productId: t.id,
|
|
195
|
+
planIds: c
|
|
196
|
+
}, I = y(p, g);
|
|
197
|
+
try {
|
|
198
|
+
const _ = await l({
|
|
199
|
+
productId: n,
|
|
200
|
+
method: i?.freemius_coupon_id ? "PUT" : "POST",
|
|
201
|
+
couponId: i?.freemius_coupon_id ?? null,
|
|
202
|
+
body: I
|
|
203
|
+
}), m = _?.coupon ?? _;
|
|
204
|
+
return await e.client.from("product_freemius_sale_coupons").upsert(
|
|
205
|
+
{
|
|
206
|
+
id: i?.id,
|
|
207
|
+
product_id: t.id,
|
|
208
|
+
freemius_product_id: n,
|
|
209
|
+
freemius_plan_id: t.freemius_plan_id ? String(t.freemius_plan_id) : null,
|
|
210
|
+
freemius_coupon_id: m?.id ? String(m.id) : i?.freemius_coupon_id ?? null,
|
|
211
|
+
freemius_coupon_code: d,
|
|
212
|
+
discount_percent: r,
|
|
213
|
+
starts_at: t.sale_start_at,
|
|
214
|
+
ends_at: t.sale_end_at,
|
|
215
|
+
is_active: u,
|
|
216
|
+
sync_status: "synced",
|
|
217
|
+
sync_error: null,
|
|
218
|
+
remote_payload: _,
|
|
219
|
+
last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
220
|
+
},
|
|
221
|
+
{ onConflict: "product_id" }
|
|
222
|
+
), { success: !0, active: u, code: d };
|
|
223
|
+
} catch (_) {
|
|
224
|
+
return await e.client.from("product_freemius_sale_coupons").upsert(
|
|
225
|
+
{
|
|
226
|
+
id: i?.id,
|
|
227
|
+
product_id: t.id,
|
|
228
|
+
freemius_product_id: n,
|
|
229
|
+
freemius_plan_id: t.freemius_plan_id ? String(t.freemius_plan_id) : null,
|
|
230
|
+
freemius_coupon_id: i?.freemius_coupon_id ?? null,
|
|
231
|
+
freemius_coupon_code: d,
|
|
232
|
+
discount_percent: r,
|
|
233
|
+
starts_at: t.sale_start_at,
|
|
234
|
+
ends_at: t.sale_end_at,
|
|
235
|
+
is_active: u,
|
|
236
|
+
sync_status: "failed",
|
|
237
|
+
sync_error: _.message || "Freemius sale coupon sync failed",
|
|
238
|
+
last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
239
|
+
},
|
|
240
|
+
{ onConflict: "product_id" }
|
|
241
|
+
), {
|
|
242
|
+
success: !1,
|
|
243
|
+
error: _.message || "Freemius sale coupon sync failed"
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async function A(e) {
|
|
248
|
+
const { data: o } = await e.client.from("coupon_freemius_mappings").select("id, freemius_product_id, freemius_coupon_id").eq("coupon_id", e.couponId).not("freemius_coupon_id", "is", null);
|
|
249
|
+
for (const s of o || [])
|
|
250
|
+
try {
|
|
251
|
+
await l({
|
|
252
|
+
productId: s.freemius_product_id,
|
|
253
|
+
couponId: s.freemius_coupon_id,
|
|
254
|
+
method: "DELETE"
|
|
255
|
+
}), await e.client.from("coupon_freemius_mappings").update({
|
|
256
|
+
sync_status: "deleted",
|
|
257
|
+
sync_error: null,
|
|
258
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
259
|
+
}).eq("id", s.id);
|
|
260
|
+
} catch (t) {
|
|
261
|
+
await e.client.from("coupon_freemius_mappings").update({
|
|
262
|
+
sync_status: "failed",
|
|
263
|
+
sync_error: t.message || "Freemius delete failed",
|
|
264
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
265
|
+
}).eq("id", s.id);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
export {
|
|
269
|
+
A as deleteCouponFromFreemius,
|
|
270
|
+
O as syncCouponToFreemius,
|
|
271
|
+
b as syncProductSaleCouponToFreemius
|
|
272
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("crypto"),C=require("@freemius/sdk"),O=require("@supabase/supabase-js"),R=require("./invoice-server.cjs.js"),j=require("./order-inventory.cjs.js"),d=require("./providers/freemius.cjs.js");function s(e){return e==null||e===""?null:String(e)}function a(e){const r=s(e);if(!r)return null;const t=r.includes("T")?r:r.replace(" ","T")+"Z",i=new Date(t);return Number.isNaN(i.getTime())?r:i.toISOString()}function K(e){if(e==null||e==="")return null;const r=Number(e);return Number.isFinite(r)?r:null}function m(...e){return e.some(r=>{const t=K(r);return t!==null&&t>0})}function l(e){return e==="pending"||e==="trial"}function y(e){return["license.cancelled","license.deleted","license.expired","subscription.cancelled","subscription.renewal.failed.last","trial.cancelled","trial.expired"].includes(e||"")}function x(e,r){return e==="license.extended"&&(r?.is_renewal===!0||r?.is_renewal===1||r?.is_renewal==="1"||r?.is_renewal==="true")}function A(e){const r=e.objects?.payment??e.data?.payment??e.data;return e.type==="payment.created"&&m(r?.gross,r?.total_gross,r?.amount,r?.initial_amount)}function b(e){const r=e?.purchase??null,t=e?.trial??null;return{freemius_product_id:s(r?.plugin_id)??s(r?.product_id)??s(r?.productId)??s(t?.plugin_id)??s(t?.product_id)??s(t?.productId),freemius_plan_id:s(r?.plan_id)??s(r?.planId),freemius_license_id:s(r?.license_id)??s(r?.licenseId)??s(t?.license_id)??s(t?.licenseId),freemius_subscription_id:s(r?.subscription_id)??s(r?.subscriptionId)??s(t?.subscription_id)??s(t?.subscriptionId),freemius_trial_id:s(t?.id),freemius_user_id:s(r?.user_id)??s(r?.userId)??s(t?.user_id)??s(t?.userId)??s(e?.user?.id),freemius_trial_ends_at:a(t?.trial_ends_at)??a(t?.trialEndsAt)??a(r?.trial_ends)??a(r?.trialEnds)}}function S(e,r="pending"){const t=e?.purchase??null,i=e?.trial??null;return r==="paid"?"paid":i&&(i.canceled_at||i.canceledAt)&&l(r)?"cancelled":i?"trial":t?(t.canceled_at||t.canceledAt)&&l(r)?"cancelled":m(t.initial_amount,t.initialAmount,t.gross,t.total_gross,t.totalGross)?"paid":t.trial_ends||t.trialEnds||t.subscription_id||t.subscriptionId?"trial":r:r}function f(e){const{event:r,currentStatus:t,purchaseData:i}=e;return t==="paid"&&y(r.type)?"paid":y(r.type)?l(t)?"cancelled":t:A(r)||x(r.type,r.data)||i&&m(i.initialAmount,i.initial_amount,i.gross,i.totalGross,i.total_gross)?"paid":i?.canceled===!0&&l(t)?"cancelled":(i?.subscriptionId||i?.subscription_id||r.type==="subscription.created"||r.type==="license.created")&&t==="pending"?"trial":t}function M(e,r){const t={freemius_last_event_type:r,freemius_last_synced_at:new Date().toISOString()};for(const[i,n]of Object.entries(e))n!=null&&n!==""&&(t[i]=n);return t}function g(){const e=process.env.NEXT_PUBLIC_SUPABASE_URL,r=process.env.SUPABASE_SERVICE_ROLE_KEY;if(!e||!r)throw new Error("Missing Supabase Service Role environment variables");return O.createClient(e,r,{auth:{autoRefreshToken:!1,persistSession:!1}})}async function N(e,r){const{data:t,error:i}=await e.from("orders").select("id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at").eq("id",r).single();if(i||!t)throw new Error(i?.message||"Order not found");return t}async function T(e,r){const{data:t,error:i}=await e.from("orders").select("id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at").eq("freemius_license_id",r).order("created_at",{ascending:!1}).limit(1).maybeSingle();if(i)throw new Error(i.message);return t??null}async function h(e){const r=M(e.metadata,e.eventType),t=e.order.status,i=t==="paid"&&e.nextStatus!=="paid"?"paid":e.nextStatus;i!==t&&(r.status=i);const{error:n}=await e.client.from("orders").update(r).eq("id",e.order.id);if(n)throw new Error(n.message);return i==="paid"&&t!=="paid"&&(await R.assignInvoiceMetadata({orderId:e.order.id,client:e.client}),await j.applyOrderInventoryDeduction(e.client,e.order.id)),i}async function q(e){const r=e.client??g(),t=await N(r,e.orderId);if(t.provider!=="freemius")return{success:!1,error:"Only Freemius orders can be synced from Freemius checkout"};const i=b(e.checkoutResponse);let n=null;if(i.freemius_license_id)try{n=await v(i.freemius_product_id??t.freemius_product_id,i.freemius_license_id)}catch(k){console.error("[Freemius Checkout Sync] Failed to retrieve purchase data:",k)}const u={type:"checkout.purchaseCompleted",data:{license_id:i.freemius_license_id},objects:{}},c={...i,...F(u,n)},o=S(e.checkoutResponse,t.status),_=n?f({event:u,currentStatus:t.status,purchaseData:n}):o==="paid"?t.status:o,w=await h({client:r,order:t,nextStatus:_,metadata:c,eventType:"checkout.purchaseCompleted"});return{success:!0,orderId:t.id,status:w,metadata:c}}function E(e){return s(e.data?.license_id)??s(e.objects?.license?.id)??s(e.objects?.subscription?.license_id)??s(e.objects?.payment?.license_id)??s(e.data?.payment?.license_id)}function F(e,r){const t=e.objects?.license,i=e.objects?.subscription,n=e.objects?.trial,u=e.objects?.payment??e.data?.payment;return{freemius_product_id:s(r?.productId)??s(r?.product_id)??s(t?.plugin_id)??s(i?.plugin_id)??s(u?.plugin_id),freemius_plan_id:s(r?.planId)??s(r?.plan_id)??s(t?.plan_id),freemius_license_id:s(r?.licenseId)??s(r?.license_id)??E(e),freemius_subscription_id:s(r?.subscriptionId)??s(r?.subscription_id)??s(i?.id)??s(e.data?.subscription_id),freemius_trial_id:s(n?.id)??s(e.data?.trial_id),freemius_user_id:s(r?.userId)??s(r?.user_id)??s(e.objects?.user?.id),freemius_trial_ends_at:a(n?.trial_ends_at)??a(n?.trial_ends)??a(t?.trial_ends)??a(r?.trialEndsAt)??a(r?.trial_ends_at)}}async function v(e,r){if(!e)return null;const t=d.resolveFreemiusCheckoutCredentials(e);return!t.apiKey||!t.secretKey||!t.publicKey?null:new C.Freemius({productId:Number(e),apiKey:t.apiKey,secretKey:t.secretKey,publicKey:t.publicKey}).purchase.retrievePurchaseData(r)}async function P(e){const r=e.client??g(),t=E(e.event);if(!t)return{success:!0,ignored:!0,reason:"missing_license_id",type:e.event.type};const i=await T(r,t);if(!i)return{success:!0,ignored:!0,reason:"unknown_license_id",licenseId:t,type:e.event.type};let n=null;try{n=await v(i.freemius_product_id??e.event.objects?.license?.plugin_id,t)}catch(_){console.error("[Freemius Sync] Failed to retrieve purchase data:",_)}const u=F(e.event,n),c=f({event:e.event,currentStatus:i.status,purchaseData:n}),o=await h({client:r,order:i,nextStatus:c,metadata:u,eventType:e.event.type??null});return{success:!0,orderId:i.id,status:o,type:e.event.type}}function I(){const e=[d.readFreemiusEnvValue("FREEMIUS_SECRET_KEY"),d.readFreemiusEnvValue("FREEMIUS_ECOMMERCE_SANDBOX_SECRET_KEY"),...Object.values(d.parseFreemiusCheckoutCredentialsMap()??{}).map(r=>r.secretKey??null)];return Array.from(new Set(e.filter(r=>!!r)))}function B(e,r){return r?I().some(t=>{const i=p.createHmac("sha256",t).update(e).digest("hex");try{return p.timingSafeEqual(Buffer.from(i,"hex"),Buffer.from(r,"hex"))}catch{return!1}}):!1}exports.extractFreemiusCheckoutMetadata=b;exports.getFreemiusWebhookSecretCandidates=I;exports.resolveFreemiusStatusFromCheckoutResponse=S;exports.resolveFreemiusStatusFromWebhookEvent=f;exports.syncFreemiusCheckoutOrder=q;exports.syncFreemiusOrderFromWebhookEvent=P;exports.verifyFreemiusWebhookSignature=B;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import f from "crypto";
|
|
2
|
+
import { Freemius as v } from "@freemius/sdk";
|
|
3
|
+
import { createClient as F } from "@supabase/supabase-js";
|
|
4
|
+
import { assignInvoiceMetadata as C } from "./invoice-server.es.js";
|
|
5
|
+
import { applyOrderInventoryDeduction as k } from "./order-inventory.es.js";
|
|
6
|
+
import { readFreemiusEnvValue as m, parseFreemiusCheckoutCredentialsMap as O, resolveFreemiusCheckoutCredentials as R } from "./providers/freemius.es.js";
|
|
7
|
+
function s(e) {
|
|
8
|
+
return e == null || e === "" ? null : String(e);
|
|
9
|
+
}
|
|
10
|
+
function a(e) {
|
|
11
|
+
const r = s(e);
|
|
12
|
+
if (!r)
|
|
13
|
+
return null;
|
|
14
|
+
const t = r.includes("T") ? r : r.replace(" ", "T") + "Z", i = new Date(t);
|
|
15
|
+
return Number.isNaN(i.getTime()) ? r : i.toISOString();
|
|
16
|
+
}
|
|
17
|
+
function K(e) {
|
|
18
|
+
if (e == null || e === "")
|
|
19
|
+
return null;
|
|
20
|
+
const r = Number(e);
|
|
21
|
+
return Number.isFinite(r) ? r : null;
|
|
22
|
+
}
|
|
23
|
+
function _(...e) {
|
|
24
|
+
return e.some((r) => {
|
|
25
|
+
const t = K(r);
|
|
26
|
+
return t !== null && t > 0;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function d(e) {
|
|
30
|
+
return e === "pending" || e === "trial";
|
|
31
|
+
}
|
|
32
|
+
function p(e) {
|
|
33
|
+
return [
|
|
34
|
+
"license.cancelled",
|
|
35
|
+
"license.deleted",
|
|
36
|
+
"license.expired",
|
|
37
|
+
"subscription.cancelled",
|
|
38
|
+
"subscription.renewal.failed.last",
|
|
39
|
+
"trial.cancelled",
|
|
40
|
+
"trial.expired"
|
|
41
|
+
].includes(e || "");
|
|
42
|
+
}
|
|
43
|
+
function j(e, r) {
|
|
44
|
+
return e === "license.extended" && (r?.is_renewal === !0 || r?.is_renewal === 1 || r?.is_renewal === "1" || r?.is_renewal === "true");
|
|
45
|
+
}
|
|
46
|
+
function x(e) {
|
|
47
|
+
const r = e.objects?.payment ?? e.data?.payment ?? e.data;
|
|
48
|
+
return e.type === "payment.created" && _(
|
|
49
|
+
r?.gross,
|
|
50
|
+
r?.total_gross,
|
|
51
|
+
r?.amount,
|
|
52
|
+
r?.initial_amount
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
function A(e) {
|
|
56
|
+
const r = e?.purchase ?? null, t = e?.trial ?? null;
|
|
57
|
+
return {
|
|
58
|
+
freemius_product_id: s(r?.plugin_id) ?? s(r?.product_id) ?? s(r?.productId) ?? s(t?.plugin_id) ?? s(t?.product_id) ?? s(t?.productId),
|
|
59
|
+
freemius_plan_id: s(r?.plan_id) ?? s(r?.planId),
|
|
60
|
+
freemius_license_id: s(r?.license_id) ?? s(r?.licenseId) ?? s(t?.license_id) ?? s(t?.licenseId),
|
|
61
|
+
freemius_subscription_id: s(r?.subscription_id) ?? s(r?.subscriptionId) ?? s(t?.subscription_id) ?? s(t?.subscriptionId),
|
|
62
|
+
freemius_trial_id: s(t?.id),
|
|
63
|
+
freemius_user_id: s(r?.user_id) ?? s(r?.userId) ?? s(t?.user_id) ?? s(t?.userId) ?? s(e?.user?.id),
|
|
64
|
+
freemius_trial_ends_at: a(t?.trial_ends_at) ?? a(t?.trialEndsAt) ?? a(r?.trial_ends) ?? a(r?.trialEnds)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function N(e, r = "pending") {
|
|
68
|
+
const t = e?.purchase ?? null, i = e?.trial ?? null;
|
|
69
|
+
return r === "paid" ? "paid" : i && (i.canceled_at || i.canceledAt) && d(r) ? "cancelled" : i ? "trial" : t ? (t.canceled_at || t.canceledAt) && d(r) ? "cancelled" : _(
|
|
70
|
+
t.initial_amount,
|
|
71
|
+
t.initialAmount,
|
|
72
|
+
t.gross,
|
|
73
|
+
t.total_gross,
|
|
74
|
+
t.totalGross
|
|
75
|
+
) ? "paid" : t.trial_ends || t.trialEnds || t.subscription_id || t.subscriptionId ? "trial" : r : r;
|
|
76
|
+
}
|
|
77
|
+
function y(e) {
|
|
78
|
+
const { event: r, currentStatus: t, purchaseData: i } = e;
|
|
79
|
+
return t === "paid" && p(r.type) ? "paid" : p(r.type) ? d(t) ? "cancelled" : t : x(r) || j(r.type, r.data) || i && _(
|
|
80
|
+
i.initialAmount,
|
|
81
|
+
i.initial_amount,
|
|
82
|
+
i.gross,
|
|
83
|
+
i.totalGross,
|
|
84
|
+
i.total_gross
|
|
85
|
+
) ? "paid" : i?.canceled === !0 && d(t) ? "cancelled" : (i?.subscriptionId || i?.subscription_id || r.type === "subscription.created" || r.type === "license.created") && t === "pending" ? "trial" : t;
|
|
86
|
+
}
|
|
87
|
+
function M(e, r) {
|
|
88
|
+
const t = {
|
|
89
|
+
freemius_last_event_type: r,
|
|
90
|
+
freemius_last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
91
|
+
};
|
|
92
|
+
for (const [i, n] of Object.entries(e))
|
|
93
|
+
n != null && n !== "" && (t[i] = n);
|
|
94
|
+
return t;
|
|
95
|
+
}
|
|
96
|
+
function b() {
|
|
97
|
+
const e = process.env.NEXT_PUBLIC_SUPABASE_URL, r = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
98
|
+
if (!e || !r)
|
|
99
|
+
throw new Error("Missing Supabase Service Role environment variables");
|
|
100
|
+
return F(e, r, {
|
|
101
|
+
auth: {
|
|
102
|
+
autoRefreshToken: !1,
|
|
103
|
+
persistSession: !1
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
async function T(e, r) {
|
|
108
|
+
const { data: t, error: i } = await e.from("orders").select(
|
|
109
|
+
"id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at"
|
|
110
|
+
).eq("id", r).single();
|
|
111
|
+
if (i || !t)
|
|
112
|
+
throw new Error(i?.message || "Order not found");
|
|
113
|
+
return t;
|
|
114
|
+
}
|
|
115
|
+
async function B(e, r) {
|
|
116
|
+
const { data: t, error: i } = await e.from("orders").select(
|
|
117
|
+
"id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at"
|
|
118
|
+
).eq("freemius_license_id", r).order("created_at", { ascending: !1 }).limit(1).maybeSingle();
|
|
119
|
+
if (i)
|
|
120
|
+
throw new Error(i.message);
|
|
121
|
+
return t ?? null;
|
|
122
|
+
}
|
|
123
|
+
async function g(e) {
|
|
124
|
+
const r = M(e.metadata, e.eventType), t = e.order.status, i = t === "paid" && e.nextStatus !== "paid" ? "paid" : e.nextStatus;
|
|
125
|
+
i !== t && (r.status = i);
|
|
126
|
+
const { error: n } = await e.client.from("orders").update(r).eq("id", e.order.id);
|
|
127
|
+
if (n)
|
|
128
|
+
throw new Error(n.message);
|
|
129
|
+
return i === "paid" && t !== "paid" && (await C({
|
|
130
|
+
orderId: e.order.id,
|
|
131
|
+
client: e.client
|
|
132
|
+
}), await k(e.client, e.order.id)), i;
|
|
133
|
+
}
|
|
134
|
+
async function V(e) {
|
|
135
|
+
const r = e.client ?? b(), t = await T(r, e.orderId);
|
|
136
|
+
if (t.provider !== "freemius")
|
|
137
|
+
return {
|
|
138
|
+
success: !1,
|
|
139
|
+
error: "Only Freemius orders can be synced from Freemius checkout"
|
|
140
|
+
};
|
|
141
|
+
const i = A(e.checkoutResponse);
|
|
142
|
+
let n = null;
|
|
143
|
+
if (i.freemius_license_id)
|
|
144
|
+
try {
|
|
145
|
+
n = await h(
|
|
146
|
+
i.freemius_product_id ?? t.freemius_product_id,
|
|
147
|
+
i.freemius_license_id
|
|
148
|
+
);
|
|
149
|
+
} catch (w) {
|
|
150
|
+
console.error("[Freemius Checkout Sync] Failed to retrieve purchase data:", w);
|
|
151
|
+
}
|
|
152
|
+
const c = {
|
|
153
|
+
type: "checkout.purchaseCompleted",
|
|
154
|
+
data: { license_id: i.freemius_license_id },
|
|
155
|
+
objects: {}
|
|
156
|
+
}, o = {
|
|
157
|
+
...i,
|
|
158
|
+
...E(c, n)
|
|
159
|
+
}, u = N(
|
|
160
|
+
e.checkoutResponse,
|
|
161
|
+
t.status
|
|
162
|
+
), l = n ? y({
|
|
163
|
+
event: c,
|
|
164
|
+
currentStatus: t.status,
|
|
165
|
+
purchaseData: n
|
|
166
|
+
}) : u === "paid" ? t.status : u, I = await g({
|
|
167
|
+
client: r,
|
|
168
|
+
order: t,
|
|
169
|
+
nextStatus: l,
|
|
170
|
+
metadata: o,
|
|
171
|
+
eventType: "checkout.purchaseCompleted"
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
success: !0,
|
|
175
|
+
orderId: t.id,
|
|
176
|
+
status: I,
|
|
177
|
+
metadata: o
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function S(e) {
|
|
181
|
+
return s(e.data?.license_id) ?? s(e.objects?.license?.id) ?? s(e.objects?.subscription?.license_id) ?? s(e.objects?.payment?.license_id) ?? s(e.data?.payment?.license_id);
|
|
182
|
+
}
|
|
183
|
+
function E(e, r) {
|
|
184
|
+
const t = e.objects?.license, i = e.objects?.subscription, n = e.objects?.trial, c = e.objects?.payment ?? e.data?.payment;
|
|
185
|
+
return {
|
|
186
|
+
freemius_product_id: s(r?.productId) ?? s(r?.product_id) ?? s(t?.plugin_id) ?? s(i?.plugin_id) ?? s(c?.plugin_id),
|
|
187
|
+
freemius_plan_id: s(r?.planId) ?? s(r?.plan_id) ?? s(t?.plan_id),
|
|
188
|
+
freemius_license_id: s(r?.licenseId) ?? s(r?.license_id) ?? S(e),
|
|
189
|
+
freemius_subscription_id: s(r?.subscriptionId) ?? s(r?.subscription_id) ?? s(i?.id) ?? s(e.data?.subscription_id),
|
|
190
|
+
freemius_trial_id: s(n?.id) ?? s(e.data?.trial_id),
|
|
191
|
+
freemius_user_id: s(r?.userId) ?? s(r?.user_id) ?? s(e.objects?.user?.id),
|
|
192
|
+
freemius_trial_ends_at: a(n?.trial_ends_at) ?? a(n?.trial_ends) ?? a(t?.trial_ends) ?? a(r?.trialEndsAt) ?? a(r?.trial_ends_at)
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
async function h(e, r) {
|
|
196
|
+
if (!e)
|
|
197
|
+
return null;
|
|
198
|
+
const t = R(e);
|
|
199
|
+
return !t.apiKey || !t.secretKey || !t.publicKey ? null : new v({
|
|
200
|
+
productId: Number(e),
|
|
201
|
+
apiKey: t.apiKey,
|
|
202
|
+
secretKey: t.secretKey,
|
|
203
|
+
publicKey: t.publicKey
|
|
204
|
+
}).purchase.retrievePurchaseData(r);
|
|
205
|
+
}
|
|
206
|
+
async function X(e) {
|
|
207
|
+
const r = e.client ?? b(), t = S(e.event);
|
|
208
|
+
if (!t)
|
|
209
|
+
return {
|
|
210
|
+
success: !0,
|
|
211
|
+
ignored: !0,
|
|
212
|
+
reason: "missing_license_id",
|
|
213
|
+
type: e.event.type
|
|
214
|
+
};
|
|
215
|
+
const i = await B(r, t);
|
|
216
|
+
if (!i)
|
|
217
|
+
return {
|
|
218
|
+
success: !0,
|
|
219
|
+
ignored: !0,
|
|
220
|
+
reason: "unknown_license_id",
|
|
221
|
+
licenseId: t,
|
|
222
|
+
type: e.event.type
|
|
223
|
+
};
|
|
224
|
+
let n = null;
|
|
225
|
+
try {
|
|
226
|
+
n = await h(
|
|
227
|
+
i.freemius_product_id ?? e.event.objects?.license?.plugin_id,
|
|
228
|
+
t
|
|
229
|
+
);
|
|
230
|
+
} catch (l) {
|
|
231
|
+
console.error("[Freemius Sync] Failed to retrieve purchase data:", l);
|
|
232
|
+
}
|
|
233
|
+
const c = E(e.event, n), o = y({
|
|
234
|
+
event: e.event,
|
|
235
|
+
currentStatus: i.status,
|
|
236
|
+
purchaseData: n
|
|
237
|
+
}), u = await g({
|
|
238
|
+
client: r,
|
|
239
|
+
order: i,
|
|
240
|
+
nextStatus: o,
|
|
241
|
+
metadata: c,
|
|
242
|
+
eventType: e.event.type ?? null
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
success: !0,
|
|
246
|
+
orderId: i.id,
|
|
247
|
+
status: u,
|
|
248
|
+
type: e.event.type
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function P() {
|
|
252
|
+
const e = [
|
|
253
|
+
m("FREEMIUS_SECRET_KEY"),
|
|
254
|
+
m("FREEMIUS_ECOMMERCE_SANDBOX_SECRET_KEY"),
|
|
255
|
+
...Object.values(O() ?? {}).map(
|
|
256
|
+
(r) => r.secretKey ?? null
|
|
257
|
+
)
|
|
258
|
+
];
|
|
259
|
+
return Array.from(
|
|
260
|
+
new Set(e.filter((r) => !!r))
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
function z(e, r) {
|
|
264
|
+
return r ? P().some((t) => {
|
|
265
|
+
const i = f.createHmac("sha256", t).update(e).digest("hex");
|
|
266
|
+
try {
|
|
267
|
+
return f.timingSafeEqual(
|
|
268
|
+
Buffer.from(i, "hex"),
|
|
269
|
+
Buffer.from(r, "hex")
|
|
270
|
+
);
|
|
271
|
+
} catch {
|
|
272
|
+
return !1;
|
|
273
|
+
}
|
|
274
|
+
}) : !1;
|
|
275
|
+
}
|
|
276
|
+
export {
|
|
277
|
+
A as extractFreemiusCheckoutMetadata,
|
|
278
|
+
P as getFreemiusWebhookSecretCandidates,
|
|
279
|
+
N as resolveFreemiusStatusFromCheckoutResponse,
|
|
280
|
+
y as resolveFreemiusStatusFromWebhookEvent,
|
|
281
|
+
V as syncFreemiusCheckoutOrder,
|
|
282
|
+
X as syncFreemiusOrderFromWebhookEvent,
|
|
283
|
+
z as verifyFreemiusWebhookSignature
|
|
284
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o="ecommerce_inventory_settings",r={trackQuantities:!0,enableTaxes:!1,taxCalculationMode:"manual"};function a(e,t=!0){if(typeof e=="boolean")return e;if(typeof e=="string"){const n=e.trim().toLowerCase();if(["true","1","yes","on"].includes(n))return!0;if(["false","0","no","off"].includes(n))return!1}return t}function c(e,t=r.taxCalculationMode){return e==="automatic"?"automatic":e==="manual"?"manual":t}function i(e){if(e&&typeof e=="object"&&!Array.isArray(e)){const t=e;return{trackQuantities:a(t.trackQuantities??t.track_quantities,r.trackQuantities),enableTaxes:a(t.enableTaxes??t.enable_taxes,r.enableTaxes),taxCalculationMode:c(t.taxCalculationMode??t.tax_calculation_mode,r.taxCalculationMode)}}return{trackQuantities:a(e,r.trackQuantities),enableTaxes:r.enableTaxes,taxCalculationMode:r.taxCalculationMode}}async function s(e){const{data:t,error:n}=await e.from("site_settings").select("value").eq("key",o).maybeSingle();return n||!t?r:i(t.value)}async function u(e,t){return e.from("site_settings").upsert({key:o,value:{track_quantities:t.trackQuantities,enable_taxes:t.enableTaxes,tax_calculation_mode:t.taxCalculationMode}})}function l(e){return{error:`${e} is no longer available.`,errorKey:"ecommerce.inventory_item_unavailable",errorParams:{item:e},errorStatus:409}}function m(e,t){return{error:`Only ${t} units remain for ${e}.`,errorKey:"ecommerce.inventory_insufficient",errorParams:{item:e,count:t},errorStatus:409}}exports.DEFAULT_ECOMMERCE_INVENTORY_SETTINGS=r;exports.ECOMMERCE_INVENTORY_SETTINGS_KEY=o;exports.createInventoryInsufficientError=m;exports.createInventoryUnavailableError=l;exports.getEcommerceInventorySettings=s;exports.normalizeEcommerceInventorySettings=i;exports.upsertEcommerceInventorySettings=u;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const o = "ecommerce_inventory_settings", a = {
|
|
2
|
+
trackQuantities: !0,
|
|
3
|
+
enableTaxes: !1,
|
|
4
|
+
taxCalculationMode: "manual"
|
|
5
|
+
};
|
|
6
|
+
function n(e, t = !0) {
|
|
7
|
+
if (typeof e == "boolean")
|
|
8
|
+
return e;
|
|
9
|
+
if (typeof e == "string") {
|
|
10
|
+
const r = e.trim().toLowerCase();
|
|
11
|
+
if (["true", "1", "yes", "on"].includes(r))
|
|
12
|
+
return !0;
|
|
13
|
+
if (["false", "0", "no", "off"].includes(r))
|
|
14
|
+
return !1;
|
|
15
|
+
}
|
|
16
|
+
return t;
|
|
17
|
+
}
|
|
18
|
+
function i(e, t = a.taxCalculationMode) {
|
|
19
|
+
return e === "automatic" ? "automatic" : e === "manual" ? "manual" : t;
|
|
20
|
+
}
|
|
21
|
+
function c(e) {
|
|
22
|
+
if (e && typeof e == "object" && !Array.isArray(e)) {
|
|
23
|
+
const t = e;
|
|
24
|
+
return {
|
|
25
|
+
trackQuantities: n(
|
|
26
|
+
t.trackQuantities ?? t.track_quantities,
|
|
27
|
+
a.trackQuantities
|
|
28
|
+
),
|
|
29
|
+
enableTaxes: n(
|
|
30
|
+
t.enableTaxes ?? t.enable_taxes,
|
|
31
|
+
a.enableTaxes
|
|
32
|
+
),
|
|
33
|
+
taxCalculationMode: i(
|
|
34
|
+
t.taxCalculationMode ?? t.tax_calculation_mode,
|
|
35
|
+
a.taxCalculationMode
|
|
36
|
+
)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
trackQuantities: n(
|
|
41
|
+
e,
|
|
42
|
+
a.trackQuantities
|
|
43
|
+
),
|
|
44
|
+
enableTaxes: a.enableTaxes,
|
|
45
|
+
taxCalculationMode: a.taxCalculationMode
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function u(e) {
|
|
49
|
+
const { data: t, error: r } = await e.from("site_settings").select("value").eq("key", o).maybeSingle();
|
|
50
|
+
return r || !t ? a : c(t.value);
|
|
51
|
+
}
|
|
52
|
+
async function s(e, t) {
|
|
53
|
+
return e.from("site_settings").upsert({
|
|
54
|
+
key: o,
|
|
55
|
+
value: {
|
|
56
|
+
track_quantities: t.trackQuantities,
|
|
57
|
+
enable_taxes: t.enableTaxes,
|
|
58
|
+
tax_calculation_mode: t.taxCalculationMode
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function l(e) {
|
|
63
|
+
return {
|
|
64
|
+
error: `${e} is no longer available.`,
|
|
65
|
+
errorKey: "ecommerce.inventory_item_unavailable",
|
|
66
|
+
errorParams: { item: e },
|
|
67
|
+
errorStatus: 409
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function f(e, t) {
|
|
71
|
+
return {
|
|
72
|
+
error: `Only ${t} units remain for ${e}.`,
|
|
73
|
+
errorKey: "ecommerce.inventory_insufficient",
|
|
74
|
+
errorParams: { item: e, count: t },
|
|
75
|
+
errorStatus: 409
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
a as DEFAULT_ECOMMERCE_INVENTORY_SETTINGS,
|
|
80
|
+
o as ECOMMERCE_INVENTORY_SETTINGS_KEY,
|
|
81
|
+
f as createInventoryInsufficientError,
|
|
82
|
+
l as createInventoryUnavailableError,
|
|
83
|
+
u as getEcommerceInventorySettings,
|
|
84
|
+
c as normalizeEcommerceInventorySettings,
|
|
85
|
+
s as upsertEcommerceInventorySettings
|
|
86
|
+
};
|