@nextblock-cms/ecom 0.20.3 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/_virtual/_rolldown/runtime.cjs.js +1 -0
  2. package/index.cjs.js +1 -1
  3. package/index.es.js +38 -146
  4. package/lib/CurrencyProvider.cjs.js +1 -1
  5. package/lib/CurrencyProvider.d.ts +1 -1
  6. package/lib/CurrencyProvider.es.js +85 -95
  7. package/lib/PaymentReadinessProvider.cjs.js +1 -1
  8. package/lib/PaymentReadinessProvider.d.ts +1 -1
  9. package/lib/PaymentReadinessProvider.es.js +24 -27
  10. package/lib/cart-store.cjs.js +1 -1
  11. package/lib/cart-store.es.js +109 -121
  12. package/lib/components/AccountNavigationMenu.cjs.js +1 -1
  13. package/lib/components/AccountNavigationMenu.d.ts +1 -1
  14. package/lib/components/AccountNavigationMenu.es.js +36 -50
  15. package/lib/components/AddToCartButton.cjs.js +1 -1
  16. package/lib/components/AddToCartButton.d.ts +1 -1
  17. package/lib/components/AddToCartButton.es.js +94 -89
  18. package/lib/components/Cart.cjs.js +1 -1
  19. package/lib/components/Cart.d.ts +1 -1
  20. package/lib/components/Cart.es.js +261 -180
  21. package/lib/components/CartDrawer.cjs.js +1 -1
  22. package/lib/components/CartDrawer.d.ts +1 -1
  23. package/lib/components/CartDrawer.es.js +218 -148
  24. package/lib/components/CartIcon.cjs.js +1 -1
  25. package/lib/components/CartIcon.d.ts +1 -1
  26. package/lib/components/CartIcon.es.js +33 -35
  27. package/lib/components/Checkout.cjs.js +1 -1
  28. package/lib/components/Checkout.d.ts +1 -1
  29. package/lib/components/Checkout.es.js +1092 -1108
  30. package/lib/components/CouponForm.cjs.js +1 -1
  31. package/lib/components/CouponForm.d.ts +1 -1
  32. package/lib/components/CouponForm.es.js +136 -148
  33. package/lib/components/CurrencySwitcher.cjs.js +1 -1
  34. package/lib/components/CurrencySwitcher.d.ts +1 -1
  35. package/lib/components/CurrencySwitcher.es.js +22 -18
  36. package/lib/components/CustomerProfileForm.cjs.js +1 -1
  37. package/lib/components/CustomerProfileForm.es.js +454 -313
  38. package/lib/components/FeaturedProduct.cjs.js +1 -1
  39. package/lib/components/FeaturedProduct.d.ts +1 -1
  40. package/lib/components/FeaturedProduct.es.js +98 -83
  41. package/lib/components/FreemiusLicensePanel.cjs.js +1 -1
  42. package/lib/components/FreemiusLicensePanel.d.ts +1 -1
  43. package/lib/components/FreemiusLicensePanel.es.js +70 -65
  44. package/lib/components/InvoiceDocument.cjs.js +1 -1
  45. package/lib/components/InvoiceDocument.es.js +256 -192
  46. package/lib/components/InvoiceViewerShell.cjs.js +2 -2
  47. package/lib/components/InvoiceViewerShell.d.ts +1 -1
  48. package/lib/components/InvoiceViewerShell.es.js +75 -106
  49. package/lib/components/ProductCard.cjs.js +1 -1
  50. package/lib/components/ProductCard.d.ts +1 -1
  51. package/lib/components/ProductCard.es.js +186 -171
  52. package/lib/components/ProductDetailsLayout.cjs.js +1 -1
  53. package/lib/components/ProductDetailsLayout.es.js +405 -404
  54. package/lib/components/ProductGallery.cjs.js +1 -1
  55. package/lib/components/ProductGallery.d.ts +1 -1
  56. package/lib/components/ProductGallery.es.js +51 -54
  57. package/lib/components/ProductGrid.cjs.js +1 -1
  58. package/lib/components/ProductGrid.d.ts +1 -1
  59. package/lib/components/ProductGrid.es.js +23 -25
  60. package/lib/components/ShippingEstimator.cjs.js +1 -1
  61. package/lib/components/ShippingEstimator.d.ts +1 -1
  62. package/lib/components/ShippingEstimator.es.js +158 -153
  63. package/lib/components/ShortDescription.cjs.js +1 -1
  64. package/lib/components/ShortDescription.es.js +17 -17
  65. package/lib/components/SimpleTiptapRenderer.cjs.js +1 -1
  66. package/lib/components/SimpleTiptapRenderer.es.js +238 -211
  67. package/lib/components/SubscriptionSelector.cjs.js +1 -1
  68. package/lib/components/SubscriptionSelector.es.js +159 -154
  69. package/lib/countries.cjs.js +1 -1
  70. package/lib/countries.es.js +232 -75
  71. package/lib/coupon-server.cjs.js +1 -1
  72. package/lib/coupon-server.es.js +180 -224
  73. package/lib/coupons.cjs.js +1 -1
  74. package/lib/coupons.es.js +12 -14
  75. package/lib/currency-constants.cjs.js +1 -1
  76. package/lib/currency-constants.es.js +4 -4
  77. package/lib/currency-rest-client.cjs.js +1 -1
  78. package/lib/currency-rest-client.es.js +15 -22
  79. package/lib/currency-store.cjs.js +1 -1
  80. package/lib/currency-store.es.js +20 -27
  81. package/lib/currency-sync.cjs.js +1 -1
  82. package/lib/currency-sync.es.js +124 -170
  83. package/lib/currency.cjs.js +1 -1
  84. package/lib/currency.es.js +242 -310
  85. package/lib/customer-addresses.cjs.js +1 -1
  86. package/lib/customer-addresses.es.js +80 -109
  87. package/lib/customer-orders.cjs.js +1 -1
  88. package/lib/customer-orders.es.js +31 -43
  89. package/lib/customer.cjs.js +1 -1
  90. package/lib/customer.es.js +43 -52
  91. package/lib/export-helpers.cjs.js +1 -1
  92. package/lib/export-helpers.es.js +53 -52
  93. package/lib/factory.cjs.js +1 -1
  94. package/lib/factory.es.js +10 -7
  95. package/lib/freemius-coupons.cjs.js +1 -1
  96. package/lib/freemius-coupons.es.js +245 -262
  97. package/lib/freemius-license-claim.cjs.js +1 -1
  98. package/lib/freemius-license-claim.es.js +87 -71
  99. package/lib/freemius-license.cjs.js +1 -1
  100. package/lib/freemius-license.es.js +32 -39
  101. package/lib/freemius-order-sync.cjs.js +1 -1
  102. package/lib/freemius-order-sync.es.js +226 -290
  103. package/lib/freemius-trial-provision.cjs.js +1 -1
  104. package/lib/freemius-trial-provision.es.js +136 -130
  105. package/lib/inventory-settings.cjs.js +1 -1
  106. package/lib/inventory-settings.es.js +69 -77
  107. package/lib/invoice-server.cjs.js +3 -3
  108. package/lib/invoice-server.es.js +94 -141
  109. package/lib/invoice-ui.cjs.js +1 -1
  110. package/lib/invoice-ui.es.js +50 -66
  111. package/lib/invoice.cjs.js +1 -1
  112. package/lib/invoice.es.js +85 -94
  113. package/lib/order-inventory.cjs.js +25 -25
  114. package/lib/order-inventory.es.js +67 -79
  115. package/lib/order-tax-details.cjs.js +1 -1
  116. package/lib/order-tax-details.es.js +142 -153
  117. package/lib/pages/cms/coupons/CouponEditorForm.cjs.js +1 -1
  118. package/lib/pages/cms/coupons/CouponEditorForm.d.ts +1 -1
  119. package/lib/pages/cms/coupons/CouponEditorForm.es.js +227 -185
  120. package/lib/pages/cms/coupons/CouponsPage.cjs.js +2 -2
  121. package/lib/pages/cms/coupons/CouponsPage.d.ts +1 -1
  122. package/lib/pages/cms/coupons/CouponsPage.es.js +235 -166
  123. package/lib/pages/cms/coupons/CreateCouponDialog.cjs.js +1 -1
  124. package/lib/pages/cms/coupons/CreateCouponDialog.d.ts +1 -1
  125. package/lib/pages/cms/coupons/CreateCouponDialog.es.js +35 -34
  126. package/lib/pages/cms/coupons/EditCouponPage.cjs.js +1 -1
  127. package/lib/pages/cms/coupons/EditCouponPage.d.ts +1 -1
  128. package/lib/pages/cms/coupons/EditCouponPage.es.js +47 -47
  129. package/lib/pages/cms/coupons/ProductScopePicker.cjs.js +1 -1
  130. package/lib/pages/cms/coupons/ProductScopePicker.d.ts +1 -1
  131. package/lib/pages/cms/coupons/ProductScopePicker.es.js +128 -117
  132. package/lib/pages/cms/coupons/actions.cjs.js +1 -1
  133. package/lib/pages/cms/coupons/actions.es.js +112 -115
  134. package/lib/pages/cms/coupons/index.cjs.js +1 -0
  135. package/lib/pages/cms/coupons/index.es.js +3 -0
  136. package/lib/pages/cms/coupons/product-options.cjs.js +1 -1
  137. package/lib/pages/cms/coupons/product-options.es.js +21 -23
  138. package/lib/pages/cms/orders/ExportReportsDialog.cjs.js +1 -1
  139. package/lib/pages/cms/orders/ExportReportsDialog.es.js +135 -129
  140. package/lib/pages/cms/orders/MarkPaidButton.d.ts +1 -1
  141. package/lib/pages/cms/orders/OrderDetailPage.cjs.js +1 -1
  142. package/lib/pages/cms/orders/OrderDetailPage.d.ts +1 -1
  143. package/lib/pages/cms/orders/OrderDetailPage.es.js +186 -134
  144. package/lib/pages/cms/orders/OrderPrintButton.cjs.js +1 -1
  145. package/lib/pages/cms/orders/OrderPrintButton.d.ts +1 -1
  146. package/lib/pages/cms/orders/OrderPrintButton.es.js +15 -21
  147. package/lib/pages/cms/orders/OrderStatusForm.cjs.js +1 -1
  148. package/lib/pages/cms/orders/OrderStatusForm.d.ts +1 -1
  149. package/lib/pages/cms/orders/OrderStatusForm.es.js +85 -69
  150. package/lib/pages/cms/orders/OrdersPage.cjs.js +1 -1
  151. package/lib/pages/cms/orders/OrdersPage.d.ts +1 -1
  152. package/lib/pages/cms/orders/OrdersPage.es.js +170 -102
  153. package/lib/pages/cms/orders/actions.cjs.js +3 -3
  154. package/lib/pages/cms/orders/actions.es.js +53 -69
  155. package/lib/pages/cms/orders/export-actions.cjs.js +1 -1
  156. package/lib/pages/cms/orders/export-actions.es.js +23 -26
  157. package/lib/pages/cms/orders/index.cjs.js +1 -0
  158. package/lib/pages/cms/orders/index.es.js +6 -0
  159. package/lib/pages/cms/orders/server-actions.cjs.js +1 -1
  160. package/lib/pages/cms/orders/server-actions.es.js +60 -51
  161. package/lib/pages/cms/payments/PaymentsClient.cjs.js +1 -1
  162. package/lib/pages/cms/payments/PaymentsClient.d.ts +1 -1
  163. package/lib/pages/cms/payments/PaymentsClient.es.js +316 -346
  164. package/lib/pages/cms/payments/PaymentsPage.cjs.js +1 -1
  165. package/lib/pages/cms/payments/PaymentsPage.d.ts +1 -1
  166. package/lib/pages/cms/payments/PaymentsPage.es.js +29 -32
  167. package/lib/pages/cms/payments/actions.cjs.js +1 -1
  168. package/lib/pages/cms/payments/actions.es.js +38 -48
  169. package/lib/pages/cms/payments/index.cjs.js +1 -0
  170. package/lib/pages/cms/payments/index.es.js +3 -0
  171. package/lib/pages/cms/payments/queries.cjs.js +1 -1
  172. package/lib/pages/cms/payments/queries.es.js +38 -48
  173. package/lib/pages/cms/products/ProductsPage.cjs.js +1 -1
  174. package/lib/pages/cms/products/ProductsPage.d.ts +1 -1
  175. package/lib/pages/cms/products/ProductsPage.es.js +63 -66
  176. package/lib/pages/cms/products/[id]/edit/EditProductPage.d.ts +1 -1
  177. package/lib/pages/cms/products/_id_/edit/EditProductPage.cjs.js +1 -1
  178. package/lib/pages/cms/products/_id_/edit/EditProductPage.es.js +138 -155
  179. package/lib/pages/cms/products/actions.cjs.js +4 -4
  180. package/lib/pages/cms/products/actions.es.js +121 -162
  181. package/lib/pages/cms/products/attributes/AttributeManagementPage.cjs.js +1 -1
  182. package/lib/pages/cms/products/attributes/AttributeManagementPage.d.ts +1 -1
  183. package/lib/pages/cms/products/attributes/AttributeManagementPage.es.js +55 -47
  184. package/lib/pages/cms/products/attributes/components/AttributeManager.cjs.js +1 -1
  185. package/lib/pages/cms/products/attributes/components/AttributeManager.d.ts +1 -1
  186. package/lib/pages/cms/products/attributes/components/AttributeManager.es.js +285 -290
  187. package/lib/pages/cms/products/categories/CategoryManagementPage.cjs.js +1 -1
  188. package/lib/pages/cms/products/categories/CategoryManagementPage.d.ts +1 -1
  189. package/lib/pages/cms/products/categories/CategoryManagementPage.es.js +43 -35
  190. package/lib/pages/cms/products/categories/components/CategoryManager.cjs.js +1 -1
  191. package/lib/pages/cms/products/categories/components/CategoryManager.es.js +315 -262
  192. package/lib/pages/cms/products/components/CopyProductFromLanguage.cjs.js +1 -1
  193. package/lib/pages/cms/products/components/CopyProductFromLanguage.es.js +115 -113
  194. package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -1
  195. package/lib/pages/cms/products/components/CurrencyPriceFields.d.ts +1 -1
  196. package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +136 -145
  197. package/lib/pages/cms/products/components/DeleteProductButton.cjs.js +1 -1
  198. package/lib/pages/cms/products/components/DeleteProductButton.d.ts +1 -1
  199. package/lib/pages/cms/products/components/DeleteProductButton.es.js +35 -47
  200. package/lib/pages/cms/products/components/FreemiusPricingDashboard.cjs.js +1 -1
  201. package/lib/pages/cms/products/components/FreemiusPricingDashboard.d.ts +1 -1
  202. package/lib/pages/cms/products/components/FreemiusPricingDashboard.es.js +106 -84
  203. package/lib/pages/cms/products/components/ProductCategorySelector.cjs.js +1 -1
  204. package/lib/pages/cms/products/components/ProductCategorySelector.es.js +156 -159
  205. package/lib/pages/cms/products/components/ProductForm.cjs.js +1 -1
  206. package/lib/pages/cms/products/components/ProductForm.es.js +872 -693
  207. package/lib/pages/cms/products/components/ProductMediaManager.cjs.js +1 -1
  208. package/lib/pages/cms/products/components/ProductMediaManager.es.js +106 -103
  209. package/lib/pages/cms/products/components/ProductsBulkTable.cjs.js +1 -1
  210. package/lib/pages/cms/products/components/ProductsBulkTable.d.ts +1 -1
  211. package/lib/pages/cms/products/components/ProductsBulkTable.es.js +303 -294
  212. package/lib/pages/cms/products/components/SaleScheduleFields.cjs.js +1 -1
  213. package/lib/pages/cms/products/components/SaleScheduleFields.d.ts +1 -1
  214. package/lib/pages/cms/products/components/SaleScheduleFields.es.js +111 -134
  215. package/lib/pages/cms/products/components/SyncFreemiusButton.cjs.js +1 -1
  216. package/lib/pages/cms/products/components/SyncFreemiusButton.d.ts +1 -1
  217. package/lib/pages/cms/products/components/SyncFreemiusButton.es.js +29 -35
  218. package/lib/pages/cms/products/components/SyncFreemiusPricingButton.cjs.js +2 -2
  219. package/lib/pages/cms/products/components/SyncFreemiusPricingButton.d.ts +1 -1
  220. package/lib/pages/cms/products/components/SyncFreemiusPricingButton.es.js +36 -48
  221. package/lib/pages/cms/products/components/SyncProductForm.d.ts +1 -1
  222. package/lib/pages/cms/products/components/VariationsEditor.cjs.js +1 -1
  223. package/lib/pages/cms/products/components/VariationsEditor.d.ts +1 -1
  224. package/lib/pages/cms/products/components/VariationsEditor.es.js +396 -405
  225. package/lib/pages/cms/products/index.cjs.js +1 -0
  226. package/lib/pages/cms/products/index.es.js +8 -0
  227. package/lib/pages/cms/products/inventory/InventoryPage.cjs.js +1 -1
  228. package/lib/pages/cms/products/inventory/InventoryPage.d.ts +1 -1
  229. package/lib/pages/cms/products/inventory/InventoryPage.es.js +21 -15
  230. package/lib/pages/cms/products/inventory/InventoryTableClient.cjs.js +2 -2
  231. package/lib/pages/cms/products/inventory/InventoryTableClient.d.ts +1 -1
  232. package/lib/pages/cms/products/inventory/InventoryTableClient.es.js +230 -209
  233. package/lib/pages/cms/products/inventory/actions.cjs.js +3 -3
  234. package/lib/pages/cms/products/inventory/actions.es.js +83 -91
  235. package/lib/pages/cms/products/new/NewProductPage.cjs.js +1 -1
  236. package/lib/pages/cms/products/new/NewProductPage.d.ts +1 -1
  237. package/lib/pages/cms/products/new/NewProductPage.es.js +58 -74
  238. package/lib/pages/cms/products/product-price-sync.cjs.js +1 -1
  239. package/lib/pages/cms/products/product-price-sync.es.js +74 -89
  240. package/lib/pages/cms/products/server-actions.cjs.js +1 -1
  241. package/lib/pages/cms/products/server-actions.es.js +250 -233
  242. package/lib/pages/cms/shipping/ShippingPage.cjs.js +2 -2
  243. package/lib/pages/cms/shipping/ShippingPage.es.js +287 -209
  244. package/lib/pages/cms/shipping/components/RateForm.cjs.js +1 -1
  245. package/lib/pages/cms/shipping/components/RateForm.d.ts +1 -1
  246. package/lib/pages/cms/shipping/components/RateForm.es.js +439 -436
  247. package/lib/pages/cms/shipping/components/ZoneForm.cjs.js +1 -1
  248. package/lib/pages/cms/shipping/components/ZoneForm.es.js +245 -224
  249. package/lib/pages/cms/shipping/index.cjs.js +1 -0
  250. package/lib/pages/cms/shipping/index.es.js +3 -0
  251. package/lib/pages/cms/shipping/server-actions.cjs.js +1 -1
  252. package/lib/pages/cms/shipping/server-actions.es.js +131 -149
  253. package/lib/pages/cms/taxes/TaxesPage.cjs.js +1 -1
  254. package/lib/pages/cms/taxes/TaxesPage.d.ts +1 -1
  255. package/lib/pages/cms/taxes/TaxesPage.es.js +211 -169
  256. package/lib/pages/cms/taxes/actions.cjs.js +1 -1
  257. package/lib/pages/cms/taxes/actions.es.js +41 -53
  258. package/lib/pages/cms/taxes/components/TaxRateForm.cjs.js +1 -1
  259. package/lib/pages/cms/taxes/components/TaxRateForm.d.ts +1 -1
  260. package/lib/pages/cms/taxes/components/TaxRateForm.es.js +112 -99
  261. package/lib/pages/cms/taxes/index.cjs.js +1 -0
  262. package/lib/pages/cms/taxes/index.es.js +3 -0
  263. package/lib/payment-config.cjs.js +1 -1
  264. package/lib/payment-config.es.js +110 -126
  265. package/lib/product-actions.cjs.js +4 -4
  266. package/lib/product-actions.es.js +289 -544
  267. package/lib/product-context.cjs.js +1 -1
  268. package/lib/product-context.es.js +13 -15
  269. package/lib/product-schema.cjs.js +1 -1
  270. package/lib/product-schema.es.js +99 -134
  271. package/lib/providers/freemius.cjs.js +1 -5
  272. package/lib/providers/freemius.es.js +446 -530
  273. package/lib/providers/stripe.cjs.js +1 -1
  274. package/lib/providers/stripe.es.js +417 -477
  275. package/lib/server-actions/coupon-actions.cjs.js +1 -1
  276. package/lib/server-actions/coupon-actions.es.js +22 -22
  277. package/lib/server-actions/customer-actions.cjs.js +1 -1
  278. package/lib/server-actions/customer-actions.es.js +26 -28
  279. package/lib/server-actions/product-actions.cjs.js +1 -1
  280. package/lib/server-actions/product-actions.es.js +9 -15
  281. package/lib/server-actions/shipping-actions.cjs.js +1 -1
  282. package/lib/server-actions/shipping-actions.es.js +27 -24
  283. package/lib/server-actions/tax-actions.cjs.js +1 -1
  284. package/lib/server-actions/tax-actions.es.js +33 -39
  285. package/lib/shared-inventory.cjs.js +2 -2
  286. package/lib/shared-inventory.es.js +111 -160
  287. package/lib/shipping/resolver.cjs.js +2 -2
  288. package/lib/shipping/resolver.es.js +44 -61
  289. package/lib/shipping-rate-currency.cjs.js +1 -1
  290. package/lib/shipping-rate-currency.es.js +52 -67
  291. package/lib/states.cjs.js +1 -1
  292. package/lib/states.es.js +333 -154
  293. package/lib/stripe/checkout.cjs.js +1 -1
  294. package/lib/stripe/checkout.es.js +125 -181
  295. package/lib/stripe/client.cjs.js +1 -1
  296. package/lib/stripe/client.es.js +14 -19
  297. package/lib/stripe/order-sync.cjs.js +1 -1
  298. package/lib/stripe/order-sync.es.js +106 -126
  299. package/lib/stripe/webhooks.cjs.js +1 -1
  300. package/lib/stripe/webhooks.es.js +37 -31
  301. package/lib/tax-calculation.cjs.js +1 -1
  302. package/lib/tax-calculation.es.js +84 -98
  303. package/lib/trials.cjs.js +1 -1
  304. package/lib/trials.es.js +24 -30
  305. package/lib/types.cjs.js +1 -1
  306. package/lib/types.es.js +21 -27
  307. package/lib/use-cart.cjs.js +1 -1
  308. package/lib/use-cart.es.js +38 -40
  309. package/lib/use-price-formatter.cjs.js +1 -1
  310. package/lib/use-price-formatter.es.js +10 -11
  311. package/lib/variation-utils.cjs.js +1 -1
  312. package/lib/variation-utils.es.js +199 -261
  313. package/lib/zod-config.cjs.js +1 -1
  314. package/lib/zod-config.es.js +5 -6
  315. package/package.json +16 -16
  316. package/server.cjs.js +1 -1
  317. package/server.es.js +56 -211
@@ -1,182 +1,126 @@
1
- import { getStripeClient as q } from "./client.es.js";
2
- import { createClient as L } from "@supabase/supabase-js";
3
- import { verifyPackageOnline as N } from "@nextblock-cms/db/server";
4
- import { resolveShippingOptions as k } from "../shipping/resolver.es.js";
5
- import { getDefaultCurrency as B, resolveEffectivePriceForCurrency as T } from "../currency.es.js";
6
- const M = async (n, d, v, P) => {
7
- const m = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL, h = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY;
8
- if (!m || !h)
9
- return console.error("Missing Supabase credentials for checkout (Service Key required)."), { error: "Internal Server Error", url: null };
10
- const t = L(m, h), f = process.env.NEXT_PUBLIC_URL || "http://localhost:4200";
11
- if (!n.length)
12
- return { error: "Cart is empty", url: null };
13
- const { data: A } = await t.from("currencies").select(
14
- "code, symbol, exchange_rate, is_default, is_active, auto_sync_product_prices, auto_update_exchange_rate, exchange_rate_source, exchange_rate_updated_at, rounding_mode, rounding_increment, rounding_charm_amount"
15
- ).eq("is_active", !0).order("code", { ascending: !0 }), u = A ?? [], w = B(u), o = u.find((e) => e.code === (P || "").toUpperCase()) ?? w;
16
- if (!await N("ecommerce"))
17
- return { error: "E-Commerce Package not active. Please purchase a license to accept payments.", url: null };
18
- const U = n.map((e) => e.product_id), { data: g, error: E } = await t.from("products").select("id, title, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at").in("id", U);
19
- if (E || !g)
20
- return console.error("Error fetching products for validation:", E), { error: "Failed to validate product prices", url: null };
21
- const I = new Map(g.map((e) => [e.id, e])), l = [];
22
- let s = 0;
23
- const y = [];
24
- for (const e of n) {
25
- const r = I.get(e.product_id);
26
- if (!r) {
27
- console.warn(`Product ${e.product_id} not found in DB, skipping.`);
28
- continue;
29
- }
30
- const C = T({
31
- prices: r.prices || {},
32
- salePrices: r.sale_prices || {},
33
- fallbackPrice: r.price,
34
- fallbackSalePrice: r.sale_price,
35
- saleStartAt: r.sale_start_at,
36
- saleEndAt: r.sale_end_at,
37
- scheduledPrice: r.scheduled_price,
38
- scheduledPrices: r.scheduled_prices || {},
39
- scheduledPriceAt: r.scheduled_price_at,
40
- currencyCode: o.code,
41
- currencies: u
42
- }), a = C.sale_price ?? C.price;
43
- a <= 0 && console.warn(`[Checkout Session Warning] Product ${r.title} has zero or negative price!`), l.push({
44
- price_data: {
45
- currency: o.code.toLowerCase(),
46
- product_data: {
47
- name: r.title,
48
- images: [],
49
- // Images temporarily removed due to schema mismatch (requires relation join)
50
- metadata: {
51
- productId: r.id
52
- }
53
- },
54
- unit_amount: a
55
- },
56
- quantity: e.quantity
57
- }), s += a * e.quantity, y.push({
58
- product_id: r.id,
59
- quantity: e.quantity,
60
- price_at_purchase: a
61
- });
62
- }
63
- if (l.length === 0)
64
- return { error: "No valid items in cart", url: null };
65
- const { data: c, error: p } = await t.from("orders").insert({
66
- total: s,
67
- user_id: d,
68
- currency: o.code,
69
- exchange_rate_at_purchase: o.exchange_rate
70
- }).select("id").single();
71
- if (p || !c)
72
- return console.error("Failed to create pending order:", p), console.error("Order Data attempted:", { status: "pending", total: s }), { error: `Failed to initiate order: ${p?.message || "Unknown error"}`, url: null };
73
- const R = y.map((e) => ({
74
- order_id: c.id,
75
- product_id: e.product_id,
76
- quantity: e.quantity,
77
- price_at_purchase: e.price_at_purchase
78
- })), { error: S } = await t.from("order_items").insert(R);
79
- S && console.error("Failed to insert order items:", S);
80
- let _ = [], i = v;
81
- if (!i && d) {
82
- const { data: e } = await t.from("user_addresses").select("country_code, state_code, postal_code").eq("user_id", d).eq("address_type", "shipping").limit(1).single();
83
- e && (i = {
84
- country: e.country_code,
85
- state: e.state_code,
86
- postal_code: e.postal_code
87
- });
88
- }
89
- i && (_ = (await k(
90
- s,
91
- i,
92
- null,
93
- o.code
94
- )).map((r) => ({
95
- shipping_rate_data: {
96
- type: "fixed_amount",
97
- fixed_amount: {
98
- amount: r.amount,
99
- currency: r.currency.toLowerCase()
100
- },
101
- display_name: r.name
102
- // delivery_estimate: { ... } // Could be added in future modules
103
- }
104
- })));
105
- try {
106
- const r = await (await q()).checkout.sessions.create({
107
- mode: "payment",
108
- success_url: `${f}/checkout/success?session_id={CHECKOUT_SESSION_ID}`,
109
- cancel_url: `${f}/`,
110
- line_items: l,
111
- billing_address_collection: "required",
112
- shipping_address_collection: {
113
- allowed_countries: [
114
- "US",
115
- "CA",
116
- "GB",
117
- "AU",
118
- "NZ",
119
- "IE",
120
- "FR",
121
- "DE",
122
- "IT",
123
- "ES",
124
- "MX",
125
- "BR",
126
- "AR",
127
- "CL",
128
- "CO",
129
- "PE",
130
- "ZA",
131
- "NG",
132
- "KE",
133
- "IN",
134
- "JP",
135
- "KR",
136
- "CN",
137
- "SG",
138
- "MY",
139
- "PH",
140
- "TH",
141
- "VN",
142
- "ID",
143
- "AE",
144
- "SA",
145
- "EG",
146
- "MA",
147
- "DZ",
148
- "TN",
149
- "PT",
150
- "NL",
151
- "BE",
152
- "CH",
153
- "AT",
154
- "SE",
155
- "NO",
156
- "DK",
157
- "FI",
158
- "PL",
159
- "CZ",
160
- "HU",
161
- "RO",
162
- "BG",
163
- "GR",
164
- "TR",
165
- "IL",
166
- "CY",
167
- "MT"
168
- ]
169
- },
170
- shipping_options: _.length > 0 ? _ : void 0,
171
- metadata: {
172
- orderId: c.id
173
- }
174
- });
175
- return await t.from("orders").update({ stripe_session_id: r.id }).eq("id", c.id), { url: r.url };
176
- } catch (e) {
177
- return console.error("Stripe session creation failed:", e), { error: e.message, url: null };
178
- }
179
- };
180
- export {
181
- M as createCheckoutSession
1
+ import { getDefaultCurrency as e, resolveEffectivePriceForCurrency as t } from "../currency.es.js";
2
+ import { resolveShippingOptions as n } from "../shipping/resolver.es.js";
3
+ import { getStripeClient as r } from "./client.es.js";
4
+ import { verifyPackageOnline as i } from "@nextblock-cms/db/server";
5
+ import { createClient as a } from "@supabase/supabase-js";
6
+ //#region libs/ecommerce/src/lib/stripe/checkout.ts
7
+ var o = async (o, s, c, l) => {
8
+ let u = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL, d = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY;
9
+ if (!u || !d) return console.error("Missing Supabase credentials for checkout (Service Key required)."), {
10
+ error: "Internal Server Error",
11
+ url: null
12
+ };
13
+ let f = a(u, d), p = process.env.NEXT_PUBLIC_URL || "http://localhost:4200";
14
+ if (!o.length) return {
15
+ error: "Cart is empty",
16
+ url: null
17
+ };
18
+ let { data: m } = await f.from("currencies").select("code, symbol, exchange_rate, is_default, is_active, auto_sync_product_prices, auto_update_exchange_rate, exchange_rate_source, exchange_rate_updated_at, rounding_mode, rounding_increment, rounding_charm_amount").eq("is_active", !0).order("code", { ascending: !0 }), h = m ?? [], g = e(h), _ = h.find((e) => e.code === (l || "").toUpperCase()) ?? g;
19
+ if (!await i("ecommerce")) return {
20
+ error: "E-Commerce Package not active. Please purchase a license to accept payments.",
21
+ url: null
22
+ };
23
+ let v = o.map((e) => e.product_id), { data: y, error: b } = await f.from("products").select("id, title, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at").in("id", v);
24
+ if (b || !y) return console.error("Error fetching products for validation:", b), {
25
+ error: "Failed to validate product prices",
26
+ url: null
27
+ };
28
+ let x = new Map(y.map((e) => [e.id, e])), S = [], C = 0, w = [];
29
+ for (let e of o) {
30
+ let n = x.get(e.product_id);
31
+ if (!n) {
32
+ console.warn(`Product ${e.product_id} not found in DB, skipping.`);
33
+ continue;
34
+ }
35
+ let r = t({
36
+ prices: n.prices || {},
37
+ salePrices: n.sale_prices || {},
38
+ fallbackPrice: n.price,
39
+ fallbackSalePrice: n.sale_price,
40
+ saleStartAt: n.sale_start_at,
41
+ saleEndAt: n.sale_end_at,
42
+ scheduledPrice: n.scheduled_price,
43
+ scheduledPrices: n.scheduled_prices || {},
44
+ scheduledPriceAt: n.scheduled_price_at,
45
+ currencyCode: _.code,
46
+ currencies: h
47
+ }), i = r.sale_price ?? r.price;
48
+ i <= 0 && console.warn(`[Checkout Session Warning] Product ${n.title} has zero or negative price!`), S.push({
49
+ price_data: {
50
+ currency: _.code.toLowerCase(),
51
+ product_data: {
52
+ name: n.title,
53
+ images: [],
54
+ metadata: { productId: n.id }
55
+ },
56
+ unit_amount: i
57
+ },
58
+ quantity: e.quantity
59
+ }), C += i * e.quantity, w.push({
60
+ product_id: n.id,
61
+ quantity: e.quantity,
62
+ price_at_purchase: i
63
+ });
64
+ }
65
+ if (S.length === 0) return {
66
+ error: "No valid items in cart",
67
+ url: null
68
+ };
69
+ let { data: T, error: E } = await f.from("orders").insert({
70
+ total: C,
71
+ user_id: s,
72
+ currency: _.code,
73
+ exchange_rate_at_purchase: _.exchange_rate
74
+ }).select("id").single();
75
+ if (E || !T) return console.error("Failed to create pending order:", E), console.error("Order Data attempted:", {
76
+ status: "pending",
77
+ total: C
78
+ }), {
79
+ error: `Failed to initiate order: ${E?.message || "Unknown error"}`,
80
+ url: null
81
+ };
82
+ let D = w.map((e) => ({
83
+ order_id: T.id,
84
+ product_id: e.product_id,
85
+ quantity: e.quantity,
86
+ price_at_purchase: e.price_at_purchase
87
+ })), { error: O } = await f.from("order_items").insert(D);
88
+ O && console.error("Failed to insert order items:", O);
89
+ let k = [], A = c;
90
+ if (!A && s) {
91
+ let { data: e } = await f.from("user_addresses").select("country_code, state_code, postal_code").eq("user_id", s).eq("address_type", "shipping").limit(1).single();
92
+ e && (A = {
93
+ country: e.country_code,
94
+ state: e.state_code,
95
+ postal_code: e.postal_code
96
+ });
97
+ }
98
+ A && (k = (await n(C, A, null, _.code)).map((e) => ({ shipping_rate_data: {
99
+ type: "fixed_amount",
100
+ fixed_amount: {
101
+ amount: e.amount,
102
+ currency: e.currency.toLowerCase()
103
+ },
104
+ display_name: e.name
105
+ } })));
106
+ try {
107
+ let e = await (await r()).checkout.sessions.create({
108
+ mode: "payment",
109
+ success_url: `${p}/checkout/success?session_id={CHECKOUT_SESSION_ID}`,
110
+ cancel_url: `${p}/`,
111
+ line_items: S,
112
+ billing_address_collection: "required",
113
+ shipping_address_collection: { allowed_countries: /* @__PURE__ */ "US.CA.GB.AU.NZ.IE.FR.DE.IT.ES.MX.BR.AR.CL.CO.PE.ZA.NG.KE.IN.JP.KR.CN.SG.MY.PH.TH.VN.ID.AE.SA.EG.MA.DZ.TN.PT.NL.BE.CH.AT.SE.NO.DK.FI.PL.CZ.HU.RO.BG.GR.TR.IL.CY.MT".split(".") },
114
+ shipping_options: k.length > 0 ? k : void 0,
115
+ metadata: { orderId: T.id }
116
+ });
117
+ return await f.from("orders").update({ stripe_session_id: e.id }).eq("id", T.id), { url: e.url };
118
+ } catch (e) {
119
+ return console.error("Stripe session creation failed:", e), {
120
+ error: e.message,
121
+ url: null
122
+ };
123
+ }
182
124
  };
125
+ //#endregion
126
+ export { o as createCheckoutSession };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("stripe"),o=require("../payment-config.cjs.js");let r=null,t=null;class n extends Error{constructor(){super("Stripe is not configured: no secret key found in CMS → Payments or STRIPE_SECRET_KEY."),this.name="StripeNotConfiguredError"}}async function c(){const e=await o.resolveStripeSecretKey();if(!e)throw new n;return r&&t===e||(r=new i(e,{typescript:!0}),t=e),r}exports.StripeNotConfiguredError=n;exports.getStripeClient=c;
1
+ const e=require("../../_virtual/_rolldown/runtime.cjs.js"),t=require("../payment-config.cjs.js");let n=require("stripe");n=e.__toESM(n,1);var r=null,i=null,a=class extends Error{constructor(){super(`Stripe is not configured: no secret key found in CMS → Payments or STRIPE_SECRET_KEY.`),this.name=`StripeNotConfiguredError`}};async function o(){let e=await t.resolveStripeSecretKey();if(!e)throw new a;return r&&i===e?r:(r=new n.default(e,{typescript:!0}),i=e,r)}exports.StripeNotConfiguredError=a,exports.getStripeClient=o;
@@ -1,20 +1,15 @@
1
- import n from "stripe";
2
- import { resolveStripeSecretKey as o } from "../payment-config.es.js";
3
- let r = null, t = null;
4
- class i extends Error {
5
- constructor() {
6
- super(
7
- "Stripe is not configured: no secret key found in CMS → Payments or STRIPE_SECRET_KEY."
8
- ), this.name = "StripeNotConfiguredError";
9
- }
10
- }
11
- async function p() {
12
- const e = await o();
13
- if (!e)
14
- throw new i();
15
- return r && t === e || (r = new n(e, { typescript: !0 }), t = e), r;
16
- }
17
- export {
18
- i as StripeNotConfiguredError,
19
- p as getStripeClient
1
+ import { resolveStripeSecretKey as e } from "../payment-config.es.js";
2
+ import t from "stripe";
3
+ //#region libs/ecommerce/src/lib/stripe/client.ts
4
+ var n = null, r = null, i = class extends Error {
5
+ constructor() {
6
+ super("Stripe is not configured: no secret key found in CMS → Payments or STRIPE_SECRET_KEY."), this.name = "StripeNotConfiguredError";
7
+ }
20
8
  };
9
+ async function a() {
10
+ let a = await e();
11
+ if (!a) throw new i();
12
+ return n && r === a ? n : (n = new t(a, { typescript: !0 }), r = a, n);
13
+ }
14
+ //#endregion
15
+ export { i as StripeNotConfiguredError, a as getStripeClient };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("@supabase/supabase-js"),d=require("../customer.cjs.js"),I=require("../customer-addresses.cjs.js"),w=require("../order-inventory.cjs.js"),E=require("../order-tax-details.cjs.js"),U=require("../invoice-server.cjs.js"),O=require("./client.cjs.js"),R=["total_details.breakdown"],P=["data.taxes.rate"];function q(){const r=process.env.NEXT_PUBLIC_SUPABASE_URL||process.env.SUPABASE_URL,t=process.env.SUPABASE_SERVICE_ROLE_KEY||process.env.SUPABASE_SECRET_KEY;if(!r||!t)throw new Error("Missing Supabase Service Role environment variables");return C.createClient(r,t,{auth:{autoRefreshToken:!1,persistSession:!1}})}function x(r,t){return r?d.normalizeCustomerAddress({recipient_name:t,line1:r.line1??null,line2:r.line2??null,city:r.city??null,state:r.state??null,postal_code:r.postal_code??null,country_code:r.country??null}):null}function z(r){const t=r??{};return d.normalizeOrderCustomerDetails({email:typeof t.email=="string"?t.email:null,name:typeof t.name=="string"?t.name:null,phone:typeof t.phone=="string"?t.phone:null,billing:t.billing??null,shipping:t.shipping??null})}async function k(r){const t=q(),u=await O.getStripeClient();let i=r;try{i=await u.checkout.sessions.retrieve(r.id,{expand:[...R]})}catch(c){console.error("[Stripe Sync] Failed to rehydrate session tax details:",c)}const p=i.metadata?.orderId;let m=t.from("orders").select("id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details").eq("stripe_session_id",r.id).maybeSingle();p&&(m=t.from("orders").select("id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details").eq("id",p).maybeSingle());const{data:e,error:_}=await m;if(_||!e)throw new Error(_?.message||"Order lookup failed");const s=z(e.customer_details),n=E.normalizeOrderTaxDetails(e.tax_details),v=x(i.customer_details?.address,i.customer_details?.name??s.name),y=i,A=x(y.shipping_details?.address,y.shipping_details?.name??s.name),o=d.normalizeOrderCustomerDetails({email:i.customer_details?.email??s.email,name:i.customer_details?.name??s.name,phone:i.customer_details?.phone??s.phone,billing:s.billing??v,shipping:s.shipping??A}),T=e.status==="paid",D=await u.checkout.sessions.listLineItems(i.id,{limit:100,expand:[...P]}),l=E.buildOrderTaxDetailsFromStripeSession({session:i,lineItems:D.data,subtotal:typeof e.subtotal=="number"?e.subtotal:n?.subtotal??0,shippingTotal:typeof e.shipping_total=="number"?e.shipping_total:n?.shipping_total??0,fallbackMode:n?.mode??"automatic",currency:(i.currency??e.currency??n?.currency??"USD").toUpperCase()}),a=l.tax_total>0||l.lines.length>0?l:n??l,S=a?.tax_total??(typeof e.tax_total=="number"?e.tax_total:0),g=(i.currency??e.currency??a?.currency??"USD").toUpperCase(),h={status:"paid",stripe_session_id:i.id,payment_intent_id:typeof i.payment_intent=="string"?i.payment_intent:null,provider:"stripe",customer_details:o,total:typeof i.amount_total=="number"?i.amount_total:e.total,currency:g,tax_total:S,tax_details:a},{error:b}=await t.from("orders").update(h).eq("id",e.id);if(b)throw new Error(b.message);if(e.user_id)try{await I.upsertDefaultUserAddresses({userId:e.user_id,billingAddress:o.billing,shippingAddress:o.shipping,client:t})}catch(c){console.error("[Stripe Sync] Failed to refresh saved customer addresses:",c)}const f=await U.assignInvoiceMetadata({orderId:e.id,client:t});return await w.applyOrderInventoryDeduction(t,e.id),{orderId:e.id,alreadyPaid:T,customerDetails:o,order:{id:e.id,invoice_number:f.invoiceNumber,paid_at:f.paidAt,total:h.total,currency:g,subtotal:typeof e.subtotal=="number"?e.subtotal:a?.subtotal??0,shipping_total:typeof e.shipping_total=="number"?e.shipping_total:a?.shipping_total??0,tax_total:S,tax_details:a}}}exports.syncStripeOrderFromSession=k;
1
+ const e=require("../customer.cjs.js"),t=require("../customer-addresses.cjs.js"),n=require("../order-tax-details.cjs.js"),r=require("./client.cjs.js"),i=require("../order-inventory.cjs.js"),a=require("../invoice-server.cjs.js");let o=require("@supabase/supabase-js");var s=[`total_details.breakdown`],c=[`data.taxes.rate`];function l(){let e=process.env.NEXT_PUBLIC_SUPABASE_URL||process.env.SUPABASE_URL,t=process.env.SUPABASE_SERVICE_ROLE_KEY||process.env.SUPABASE_SECRET_KEY;if(!e||!t)throw Error(`Missing Supabase Service Role environment variables`);return(0,o.createClient)(e,t,{auth:{autoRefreshToken:!1,persistSession:!1}})}function u(t,n){return t?e.normalizeCustomerAddress({recipient_name:n,line1:t.line1??null,line2:t.line2??null,city:t.city??null,state:t.state??null,postal_code:t.postal_code??null,country_code:t.country??null}):null}function d(t){let n=t??{};return e.normalizeOrderCustomerDetails({email:typeof n.email==`string`?n.email:null,name:typeof n.name==`string`?n.name:null,phone:typeof n.phone==`string`?n.phone:null,billing:n.billing??null,shipping:n.shipping??null})}async function f(o){let f=l(),p=await r.getStripeClient(),m=o;try{m=await p.checkout.sessions.retrieve(o.id,{expand:[...s]})}catch(e){console.error(`[Stripe Sync] Failed to rehydrate session tax details:`,e)}let h=m.metadata?.orderId,g=f.from(`orders`).select(`id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details`).eq(`stripe_session_id`,o.id).maybeSingle();h&&(g=f.from(`orders`).select(`id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details`).eq(`id`,h).maybeSingle());let{data:_,error:v}=await g;if(v||!_)throw Error(v?.message||`Order lookup failed`);let y=d(_.customer_details),b=n.normalizeOrderTaxDetails(_.tax_details),x=u(m.customer_details?.address,m.customer_details?.name??y.name),S=m.shipping_details,C=m.collected_information?.shipping_details??S??null,w=u(C?.address,C?.name??y.name),T=e.normalizeOrderCustomerDetails({email:m.customer_details?.email??y.email,name:m.customer_details?.name??y.name,phone:m.customer_details?.phone??y.phone,billing:y.billing??x,shipping:y.shipping??w}),E=_.status===`paid`,D=await p.checkout.sessions.listLineItems(m.id,{limit:100,expand:[...c]}),O=n.buildOrderTaxDetailsFromStripeSession({session:m,lineItems:D.data,subtotal:typeof _.subtotal==`number`?_.subtotal:b?.subtotal??0,shippingTotal:typeof _.shipping_total==`number`?_.shipping_total:b?.shipping_total??0,fallbackMode:b?.mode??`automatic`,currency:(m.currency??_.currency??b?.currency??`USD`).toUpperCase()}),k=O.tax_total>0||O.lines.length>0?O:b??O,A=k?.tax_total??(typeof _.tax_total==`number`?_.tax_total:0),j=(m.currency??_.currency??k?.currency??`USD`).toUpperCase(),M={status:`paid`,stripe_session_id:m.id,payment_intent_id:typeof m.payment_intent==`string`?m.payment_intent:null,provider:`stripe`,customer_details:T,total:typeof m.amount_total==`number`?m.amount_total:_.total,currency:j,tax_total:A,tax_details:k},{error:N}=await f.from(`orders`).update(M).eq(`id`,_.id);if(N)throw Error(N.message);if(_.user_id)try{await t.upsertDefaultUserAddresses({userId:_.user_id,billingAddress:T.billing,shippingAddress:T.shipping,client:f})}catch(e){console.error(`[Stripe Sync] Failed to refresh saved customer addresses:`,e)}let P=await a.assignInvoiceMetadata({orderId:_.id,client:f});return await i.applyOrderInventoryDeduction(f,_.id),{orderId:_.id,alreadyPaid:E,customerDetails:T,order:{id:_.id,invoice_number:P.invoiceNumber,paid_at:P.paidAt,total:M.total,currency:j,subtotal:typeof _.subtotal==`number`?_.subtotal:k?.subtotal??0,shipping_total:typeof _.shipping_total==`number`?_.shipping_total:k?.shipping_total??0,tax_total:A,tax_details:k}}}exports.syncStripeOrderFromSession=f;
@@ -1,130 +1,110 @@
1
- import { createClient as C } from "@supabase/supabase-js";
2
- import { normalizeOrderCustomerDetails as E, normalizeCustomerAddress as D } from "../customer.es.js";
3
- import { upsertDefaultUserAddresses as I } from "../customer-addresses.es.js";
4
- import { applyOrderInventoryDeduction as w } from "../order-inventory.es.js";
5
- import { normalizeOrderTaxDetails as U, buildOrderTaxDetailsFromStripeSession as R } from "../order-tax-details.es.js";
6
- import { assignInvoiceMetadata as P } from "../invoice-server.es.js";
7
- import { getStripeClient as O } from "./client.es.js";
8
- const z = ["total_details.breakdown"], k = ["data.taxes.rate"];
9
- function B() {
10
- const r = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL, t = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY;
11
- if (!r || !t)
12
- throw new Error("Missing Supabase Service Role environment variables");
13
- return C(r, t, {
14
- auth: {
15
- autoRefreshToken: !1,
16
- persistSession: !1
17
- }
18
- });
1
+ import { normalizeCustomerAddress as e, normalizeOrderCustomerDetails as t } from "../customer.es.js";
2
+ import { upsertDefaultUserAddresses as n } from "../customer-addresses.es.js";
3
+ import { buildOrderTaxDetailsFromStripeSession as r, normalizeOrderTaxDetails as i } from "../order-tax-details.es.js";
4
+ import { getStripeClient as a } from "./client.es.js";
5
+ import { applyOrderInventoryDeduction as o } from "../order-inventory.es.js";
6
+ import { assignInvoiceMetadata as s } from "../invoice-server.es.js";
7
+ import { createClient as c } from "@supabase/supabase-js";
8
+ //#region libs/ecommerce/src/lib/stripe/order-sync.ts
9
+ var l = ["total_details.breakdown"], u = ["data.taxes.rate"];
10
+ function d() {
11
+ let e = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL, t = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY;
12
+ if (!e || !t) throw Error("Missing Supabase Service Role environment variables");
13
+ return c(e, t, { auth: {
14
+ autoRefreshToken: !1,
15
+ persistSession: !1
16
+ } });
19
17
  }
20
- function b(r, t) {
21
- return r ? D({
22
- recipient_name: t,
23
- line1: r.line1 ?? null,
24
- line2: r.line2 ?? null,
25
- city: r.city ?? null,
26
- state: r.state ?? null,
27
- postal_code: r.postal_code ?? null,
28
- country_code: r.country ?? null
29
- }) : null;
18
+ function f(t, n) {
19
+ return t ? e({
20
+ recipient_name: n,
21
+ line1: t.line1 ?? null,
22
+ line2: t.line2 ?? null,
23
+ city: t.city ?? null,
24
+ state: t.state ?? null,
25
+ postal_code: t.postal_code ?? null,
26
+ country_code: t.country ?? null
27
+ }) : null;
30
28
  }
31
- function L(r) {
32
- const t = r ?? {};
33
- return E({
34
- email: typeof t.email == "string" ? t.email : null,
35
- name: typeof t.name == "string" ? t.name : null,
36
- phone: typeof t.phone == "string" ? t.phone : null,
37
- billing: t.billing ?? null,
38
- shipping: t.shipping ?? null
39
- });
29
+ function p(e) {
30
+ let n = e ?? {};
31
+ return t({
32
+ email: typeof n.email == "string" ? n.email : null,
33
+ name: typeof n.name == "string" ? n.name : null,
34
+ phone: typeof n.phone == "string" ? n.phone : null,
35
+ billing: n.billing ?? null,
36
+ shipping: n.shipping ?? null
37
+ });
40
38
  }
41
- async function Y(r) {
42
- const t = B(), p = await O();
43
- let i = r;
44
- try {
45
- i = await p.checkout.sessions.retrieve(r.id, {
46
- expand: [...z]
47
- });
48
- } catch (d) {
49
- console.error("[Stripe Sync] Failed to rehydrate session tax details:", d);
50
- }
51
- const c = i.metadata?.orderId;
52
- let u = t.from("orders").select(
53
- "id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details"
54
- ).eq("stripe_session_id", r.id).maybeSingle();
55
- c && (u = t.from("orders").select(
56
- "id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details"
57
- ).eq("id", c).maybeSingle());
58
- const { data: e, error: m } = await u;
59
- if (m || !e)
60
- throw new Error(m?.message || "Order lookup failed");
61
- const a = L(e.customer_details), o = U(e.tax_details), x = b(
62
- i.customer_details?.address,
63
- i.customer_details?.name ?? a.name
64
- ), _ = i, A = b(
65
- _.shipping_details?.address,
66
- _.shipping_details?.name ?? a.name
67
- ), n = E({
68
- email: i.customer_details?.email ?? a.email,
69
- name: i.customer_details?.name ?? a.name,
70
- phone: i.customer_details?.phone ?? a.phone,
71
- billing: a.billing ?? x,
72
- shipping: a.shipping ?? A
73
- }), T = e.status === "paid", v = await p.checkout.sessions.listLineItems(i.id, {
74
- limit: 100,
75
- expand: [...k]
76
- }), l = R({
77
- session: i,
78
- lineItems: v.data,
79
- subtotal: typeof e.subtotal == "number" ? e.subtotal : o?.subtotal ?? 0,
80
- shippingTotal: typeof e.shipping_total == "number" ? e.shipping_total : o?.shipping_total ?? 0,
81
- fallbackMode: o?.mode ?? "automatic",
82
- currency: (i.currency ?? e.currency ?? o?.currency ?? "USD").toUpperCase()
83
- }), s = l.tax_total > 0 || l.lines.length > 0 ? l : o ?? l, y = s?.tax_total ?? (typeof e.tax_total == "number" ? e.tax_total : 0), f = (i.currency ?? e.currency ?? s?.currency ?? "USD").toUpperCase(), g = {
84
- status: "paid",
85
- stripe_session_id: i.id,
86
- payment_intent_id: typeof i.payment_intent == "string" ? i.payment_intent : null,
87
- provider: "stripe",
88
- customer_details: n,
89
- total: typeof i.amount_total == "number" ? i.amount_total : e.total,
90
- currency: f,
91
- tax_total: y,
92
- tax_details: s
93
- }, { error: S } = await t.from("orders").update(g).eq("id", e.id);
94
- if (S)
95
- throw new Error(S.message);
96
- if (e.user_id)
97
- try {
98
- await I({
99
- userId: e.user_id,
100
- billingAddress: n.billing,
101
- shippingAddress: n.shipping,
102
- client: t
103
- });
104
- } catch (d) {
105
- console.error("[Stripe Sync] Failed to refresh saved customer addresses:", d);
106
- }
107
- const h = await P({
108
- orderId: e.id,
109
- client: t
110
- });
111
- return await w(t, e.id), {
112
- orderId: e.id,
113
- alreadyPaid: T,
114
- customerDetails: n,
115
- order: {
116
- id: e.id,
117
- invoice_number: h.invoiceNumber,
118
- paid_at: h.paidAt,
119
- total: g.total,
120
- currency: f,
121
- subtotal: typeof e.subtotal == "number" ? e.subtotal : s?.subtotal ?? 0,
122
- shipping_total: typeof e.shipping_total == "number" ? e.shipping_total : s?.shipping_total ?? 0,
123
- tax_total: y,
124
- tax_details: s
125
- }
126
- };
39
+ async function m(e) {
40
+ let c = d(), m = await a(), h = e;
41
+ try {
42
+ h = await m.checkout.sessions.retrieve(e.id, { expand: [...l] });
43
+ } catch (e) {
44
+ console.error("[Stripe Sync] Failed to rehydrate session tax details:", e);
45
+ }
46
+ let g = h.metadata?.orderId, _ = c.from("orders").select("id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details").eq("stripe_session_id", e.id).maybeSingle();
47
+ g && (_ = c.from("orders").select("id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details").eq("id", g).maybeSingle());
48
+ let { data: v, error: y } = await _;
49
+ if (y || !v) throw Error(y?.message || "Order lookup failed");
50
+ let b = p(v.customer_details), x = i(v.tax_details), S = f(h.customer_details?.address, h.customer_details?.name ?? b.name), C = h.shipping_details, w = h.collected_information?.shipping_details ?? C ?? null, T = f(w?.address, w?.name ?? b.name), E = t({
51
+ email: h.customer_details?.email ?? b.email,
52
+ name: h.customer_details?.name ?? b.name,
53
+ phone: h.customer_details?.phone ?? b.phone,
54
+ billing: b.billing ?? S,
55
+ shipping: b.shipping ?? T
56
+ }), D = v.status === "paid", O = await m.checkout.sessions.listLineItems(h.id, {
57
+ limit: 100,
58
+ expand: [...u]
59
+ }), k = r({
60
+ session: h,
61
+ lineItems: O.data,
62
+ subtotal: typeof v.subtotal == "number" ? v.subtotal : x?.subtotal ?? 0,
63
+ shippingTotal: typeof v.shipping_total == "number" ? v.shipping_total : x?.shipping_total ?? 0,
64
+ fallbackMode: x?.mode ?? "automatic",
65
+ currency: (h.currency ?? v.currency ?? x?.currency ?? "USD").toUpperCase()
66
+ }), A = k.tax_total > 0 || k.lines.length > 0 ? k : x ?? k, j = A?.tax_total ?? (typeof v.tax_total == "number" ? v.tax_total : 0), M = (h.currency ?? v.currency ?? A?.currency ?? "USD").toUpperCase(), N = {
67
+ status: "paid",
68
+ stripe_session_id: h.id,
69
+ payment_intent_id: typeof h.payment_intent == "string" ? h.payment_intent : null,
70
+ provider: "stripe",
71
+ customer_details: E,
72
+ total: typeof h.amount_total == "number" ? h.amount_total : v.total,
73
+ currency: M,
74
+ tax_total: j,
75
+ tax_details: A
76
+ }, { error: P } = await c.from("orders").update(N).eq("id", v.id);
77
+ if (P) throw Error(P.message);
78
+ if (v.user_id) try {
79
+ await n({
80
+ userId: v.user_id,
81
+ billingAddress: E.billing,
82
+ shippingAddress: E.shipping,
83
+ client: c
84
+ });
85
+ } catch (e) {
86
+ console.error("[Stripe Sync] Failed to refresh saved customer addresses:", e);
87
+ }
88
+ let F = await s({
89
+ orderId: v.id,
90
+ client: c
91
+ });
92
+ return await o(c, v.id), {
93
+ orderId: v.id,
94
+ alreadyPaid: D,
95
+ customerDetails: E,
96
+ order: {
97
+ id: v.id,
98
+ invoice_number: F.invoiceNumber,
99
+ paid_at: F.paidAt,
100
+ total: N.total,
101
+ currency: M,
102
+ subtotal: typeof v.subtotal == "number" ? v.subtotal : A?.subtotal ?? 0,
103
+ shipping_total: typeof v.shipping_total == "number" ? v.shipping_total : A?.shipping_total ?? 0,
104
+ tax_total: j,
105
+ tax_details: A
106
+ }
107
+ };
127
108
  }
128
- export {
129
- Y as syncStripeOrderFromSession
130
- };
109
+ //#endregion
110
+ export { m as syncStripeOrderFromSession };