@blocklet/payment-react-headless 1.26.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 (250) hide show
  1. package/.eslintrc.js +18 -0
  2. package/build.config.ts +30 -0
  3. package/es/checkout/context/CheckoutProvider.d.ts +6 -0
  4. package/es/checkout/context/CheckoutProvider.js +209 -0
  5. package/es/checkout/context/CustomerFormContext.d.ts +4 -0
  6. package/es/checkout/context/CustomerFormContext.js +9 -0
  7. package/es/checkout/context/ExchangeRateContext.d.ts +11 -0
  8. package/es/checkout/context/ExchangeRateContext.js +9 -0
  9. package/es/checkout/context/PaymentMethodContext.d.ts +26 -0
  10. package/es/checkout/context/PaymentMethodContext.js +9 -0
  11. package/es/checkout/context/SessionContext.d.ts +45 -0
  12. package/es/checkout/context/SessionContext.js +9 -0
  13. package/es/checkout/context/SubmitContext.d.ts +4 -0
  14. package/es/checkout/context/SubmitContext.js +9 -0
  15. package/es/checkout/context/index.d.ts +6 -0
  16. package/es/checkout/context/index.js +6 -0
  17. package/es/checkout/core/billingInterval.d.ts +15 -0
  18. package/es/checkout/core/billingInterval.js +36 -0
  19. package/es/checkout/core/crossSell.d.ts +4 -0
  20. package/es/checkout/core/crossSell.js +30 -0
  21. package/es/checkout/core/customerForm.d.ts +5 -0
  22. package/es/checkout/core/customerForm.js +105 -0
  23. package/es/checkout/core/exchangeRate.d.ts +11 -0
  24. package/es/checkout/core/exchangeRate.js +25 -0
  25. package/es/checkout/core/index.d.ts +10 -0
  26. package/es/checkout/core/index.js +55 -0
  27. package/es/checkout/core/lineItems.d.ts +7 -0
  28. package/es/checkout/core/lineItems.js +59 -0
  29. package/es/checkout/core/paymentMethod.d.ts +23 -0
  30. package/es/checkout/core/paymentMethod.js +85 -0
  31. package/es/checkout/core/pricing.d.ts +32 -0
  32. package/es/checkout/core/pricing.js +221 -0
  33. package/es/checkout/core/promotion.d.ts +10 -0
  34. package/es/checkout/core/promotion.js +39 -0
  35. package/es/checkout/core/session.d.ts +26 -0
  36. package/es/checkout/core/session.js +50 -0
  37. package/es/checkout/core/submit.d.ts +40 -0
  38. package/es/checkout/core/submit.js +66 -0
  39. package/es/checkout/hooks/index.d.ts +34 -0
  40. package/es/checkout/hooks/index.js +19 -0
  41. package/es/checkout/hooks/useBillingInterval.d.ts +14 -0
  42. package/es/checkout/hooks/useBillingInterval.js +50 -0
  43. package/es/checkout/hooks/useCheckout.d.ts +2 -0
  44. package/es/checkout/hooks/useCheckout.js +212 -0
  45. package/es/checkout/hooks/useCheckoutSession.d.ts +58 -0
  46. package/es/checkout/hooks/useCheckoutSession.js +107 -0
  47. package/es/checkout/hooks/useCheckoutStatus.d.ts +10 -0
  48. package/es/checkout/hooks/useCheckoutStatus.js +16 -0
  49. package/es/checkout/hooks/useCrossSell.d.ts +8 -0
  50. package/es/checkout/hooks/useCrossSell.js +57 -0
  51. package/es/checkout/hooks/useCustomerForm.d.ts +14 -0
  52. package/es/checkout/hooks/useCustomerForm.js +116 -0
  53. package/es/checkout/hooks/useCustomerFormFeature.d.ts +2 -0
  54. package/es/checkout/hooks/useCustomerFormFeature.js +4 -0
  55. package/es/checkout/hooks/useExchangeRate.d.ts +11 -0
  56. package/es/checkout/hooks/useExchangeRate.js +15 -0
  57. package/es/checkout/hooks/useLineItems.d.ts +22 -0
  58. package/es/checkout/hooks/useLineItems.js +139 -0
  59. package/es/checkout/hooks/usePaymentMethod.d.ts +26 -0
  60. package/es/checkout/hooks/usePaymentMethod.js +101 -0
  61. package/es/checkout/hooks/usePaymentMethodFeature.d.ts +2 -0
  62. package/es/checkout/hooks/usePaymentMethodFeature.js +4 -0
  63. package/es/checkout/hooks/usePricing.d.ts +57 -0
  64. package/es/checkout/hooks/usePricing.js +174 -0
  65. package/es/checkout/hooks/usePricingFeature.d.ts +28 -0
  66. package/es/checkout/hooks/usePricingFeature.js +36 -0
  67. package/es/checkout/hooks/useProduct.d.ts +32 -0
  68. package/es/checkout/hooks/useProduct.js +5 -0
  69. package/es/checkout/hooks/usePromotion.d.ts +12 -0
  70. package/es/checkout/hooks/usePromotion.js +48 -0
  71. package/es/checkout/hooks/useSlippage.d.ts +8 -0
  72. package/es/checkout/hooks/useSlippage.js +29 -0
  73. package/es/checkout/hooks/useSubmit.d.ts +38 -0
  74. package/es/checkout/hooks/useSubmit.js +493 -0
  75. package/es/checkout/hooks/useSubmitFeature.d.ts +2 -0
  76. package/es/checkout/hooks/useSubmitFeature.js +4 -0
  77. package/es/checkout/hooks/useUpsell.d.ts +5 -0
  78. package/es/checkout/hooks/useUpsell.js +25 -0
  79. package/es/checkout/index.d.ts +37 -0
  80. package/es/checkout/index.js +28 -0
  81. package/es/checkout/types.d.ts +262 -0
  82. package/es/checkout/types.js +0 -0
  83. package/es/index.d.ts +1 -0
  84. package/es/index.js +28 -0
  85. package/es/shared/api.d.ts +41 -0
  86. package/es/shared/api.js +81 -0
  87. package/es/shared/format.d.ts +38 -0
  88. package/es/shared/format.js +229 -0
  89. package/es/shared/polling.d.ts +15 -0
  90. package/es/shared/polling.js +20 -0
  91. package/es/shared/types.d.ts +10 -0
  92. package/es/shared/types.js +0 -0
  93. package/es/shared/validation.d.ts +38 -0
  94. package/es/shared/validation.js +190 -0
  95. package/es/types/checkout-augmented.d.ts +42 -0
  96. package/es/types/checkout-augmented.js +17 -0
  97. package/es/types/external.d.ts +18 -0
  98. package/examples/01-basic-checkout.tsx +159 -0
  99. package/examples/01-credit-recharge.tsx +19 -0
  100. package/examples/02-subscription.tsx +40 -0
  101. package/examples/03-upsell.tsx +60 -0
  102. package/examples/04-cross-sell.tsx +54 -0
  103. package/examples/05-full-checkout.tsx +126 -0
  104. package/jest.config.js +15 -0
  105. package/lib/checkout/context/CheckoutProvider.d.ts +6 -0
  106. package/lib/checkout/context/CheckoutProvider.js +181 -0
  107. package/lib/checkout/context/CustomerFormContext.d.ts +4 -0
  108. package/lib/checkout/context/CustomerFormContext.js +16 -0
  109. package/lib/checkout/context/ExchangeRateContext.d.ts +11 -0
  110. package/lib/checkout/context/ExchangeRateContext.js +16 -0
  111. package/lib/checkout/context/PaymentMethodContext.d.ts +26 -0
  112. package/lib/checkout/context/PaymentMethodContext.js +16 -0
  113. package/lib/checkout/context/SessionContext.d.ts +45 -0
  114. package/lib/checkout/context/SessionContext.js +16 -0
  115. package/lib/checkout/context/SubmitContext.d.ts +4 -0
  116. package/lib/checkout/context/SubmitContext.js +16 -0
  117. package/lib/checkout/context/index.d.ts +6 -0
  118. package/lib/checkout/context/index.js +77 -0
  119. package/lib/checkout/core/billingInterval.d.ts +15 -0
  120. package/lib/checkout/core/billingInterval.js +42 -0
  121. package/lib/checkout/core/crossSell.d.ts +4 -0
  122. package/lib/checkout/core/crossSell.js +43 -0
  123. package/lib/checkout/core/customerForm.d.ts +5 -0
  124. package/lib/checkout/core/customerForm.js +106 -0
  125. package/lib/checkout/core/exchangeRate.d.ts +11 -0
  126. package/lib/checkout/core/exchangeRate.js +45 -0
  127. package/lib/checkout/core/index.d.ts +10 -0
  128. package/lib/checkout/core/index.js +297 -0
  129. package/lib/checkout/core/lineItems.d.ts +7 -0
  130. package/lib/checkout/core/lineItems.js +76 -0
  131. package/lib/checkout/core/paymentMethod.d.ts +23 -0
  132. package/lib/checkout/core/paymentMethod.js +114 -0
  133. package/lib/checkout/core/pricing.d.ts +32 -0
  134. package/lib/checkout/core/pricing.js +216 -0
  135. package/lib/checkout/core/promotion.d.ts +10 -0
  136. package/lib/checkout/core/promotion.js +62 -0
  137. package/lib/checkout/core/session.d.ts +26 -0
  138. package/lib/checkout/core/session.js +58 -0
  139. package/lib/checkout/core/submit.d.ts +40 -0
  140. package/lib/checkout/core/submit.js +84 -0
  141. package/lib/checkout/hooks/index.d.ts +34 -0
  142. package/lib/checkout/hooks/index.js +138 -0
  143. package/lib/checkout/hooks/useBillingInterval.d.ts +14 -0
  144. package/lib/checkout/hooks/useBillingInterval.js +63 -0
  145. package/lib/checkout/hooks/useCheckout.d.ts +2 -0
  146. package/lib/checkout/hooks/useCheckout.js +190 -0
  147. package/lib/checkout/hooks/useCheckoutSession.d.ts +58 -0
  148. package/lib/checkout/hooks/useCheckoutSession.js +119 -0
  149. package/lib/checkout/hooks/useCheckoutStatus.d.ts +10 -0
  150. package/lib/checkout/hooks/useCheckoutStatus.js +28 -0
  151. package/lib/checkout/hooks/useCrossSell.d.ts +8 -0
  152. package/lib/checkout/hooks/useCrossSell.js +75 -0
  153. package/lib/checkout/hooks/useCustomerForm.d.ts +14 -0
  154. package/lib/checkout/hooks/useCustomerForm.js +135 -0
  155. package/lib/checkout/hooks/useCustomerFormFeature.d.ts +2 -0
  156. package/lib/checkout/hooks/useCustomerFormFeature.js +10 -0
  157. package/lib/checkout/hooks/useExchangeRate.d.ts +11 -0
  158. package/lib/checkout/hooks/useExchangeRate.js +29 -0
  159. package/lib/checkout/hooks/useLineItems.d.ts +22 -0
  160. package/lib/checkout/hooks/useLineItems.js +142 -0
  161. package/lib/checkout/hooks/usePaymentMethod.d.ts +26 -0
  162. package/lib/checkout/hooks/usePaymentMethod.js +101 -0
  163. package/lib/checkout/hooks/usePaymentMethodFeature.d.ts +2 -0
  164. package/lib/checkout/hooks/usePaymentMethodFeature.js +10 -0
  165. package/lib/checkout/hooks/usePricing.d.ts +57 -0
  166. package/lib/checkout/hooks/usePricing.js +168 -0
  167. package/lib/checkout/hooks/usePricingFeature.d.ts +28 -0
  168. package/lib/checkout/hooks/usePricingFeature.js +48 -0
  169. package/lib/checkout/hooks/useProduct.d.ts +32 -0
  170. package/lib/checkout/hooks/useProduct.js +21 -0
  171. package/lib/checkout/hooks/usePromotion.d.ts +12 -0
  172. package/lib/checkout/hooks/usePromotion.js +57 -0
  173. package/lib/checkout/hooks/useSlippage.d.ts +8 -0
  174. package/lib/checkout/hooks/useSlippage.js +39 -0
  175. package/lib/checkout/hooks/useSubmit.d.ts +38 -0
  176. package/lib/checkout/hooks/useSubmit.js +504 -0
  177. package/lib/checkout/hooks/useSubmitFeature.d.ts +2 -0
  178. package/lib/checkout/hooks/useSubmitFeature.js +10 -0
  179. package/lib/checkout/hooks/useUpsell.d.ts +5 -0
  180. package/lib/checkout/hooks/useUpsell.js +40 -0
  181. package/lib/checkout/index.d.ts +37 -0
  182. package/lib/checkout/index.js +182 -0
  183. package/lib/checkout/types.d.ts +262 -0
  184. package/lib/checkout/types.js +1 -0
  185. package/lib/index.d.ts +1 -0
  186. package/lib/index.js +162 -0
  187. package/lib/shared/api.d.ts +41 -0
  188. package/lib/shared/api.js +88 -0
  189. package/lib/shared/format.d.ts +38 -0
  190. package/lib/shared/format.js +262 -0
  191. package/lib/shared/polling.d.ts +15 -0
  192. package/lib/shared/polling.js +32 -0
  193. package/lib/shared/types.d.ts +10 -0
  194. package/lib/shared/types.js +1 -0
  195. package/lib/shared/validation.d.ts +38 -0
  196. package/lib/shared/validation.js +212 -0
  197. package/lib/types/checkout-augmented.d.ts +42 -0
  198. package/lib/types/checkout-augmented.js +24 -0
  199. package/lib/types/external.d.ts +18 -0
  200. package/package.json +64 -0
  201. package/src/checkout/context/CheckoutProvider.tsx +269 -0
  202. package/src/checkout/context/CustomerFormContext.ts +14 -0
  203. package/src/checkout/context/ExchangeRateContext.ts +21 -0
  204. package/src/checkout/context/PaymentMethodContext.ts +36 -0
  205. package/src/checkout/context/SessionContext.ts +49 -0
  206. package/src/checkout/context/SubmitContext.ts +14 -0
  207. package/src/checkout/context/index.ts +6 -0
  208. package/src/checkout/core/billingInterval.ts +62 -0
  209. package/src/checkout/core/crossSell.ts +52 -0
  210. package/src/checkout/core/customerForm.ts +122 -0
  211. package/src/checkout/core/exchangeRate.ts +38 -0
  212. package/src/checkout/core/index.ts +60 -0
  213. package/src/checkout/core/lineItems.ts +106 -0
  214. package/src/checkout/core/paymentMethod.ts +113 -0
  215. package/src/checkout/core/pricing.ts +347 -0
  216. package/src/checkout/core/promotion.ts +59 -0
  217. package/src/checkout/core/session.ts +62 -0
  218. package/src/checkout/core/submit.ts +109 -0
  219. package/src/checkout/hooks/index.ts +41 -0
  220. package/src/checkout/hooks/useBillingInterval.ts +71 -0
  221. package/src/checkout/hooks/useCheckout.ts +267 -0
  222. package/src/checkout/hooks/useCheckoutSession.ts +217 -0
  223. package/src/checkout/hooks/useCheckoutStatus.ts +31 -0
  224. package/src/checkout/hooks/useCrossSell.ts +80 -0
  225. package/src/checkout/hooks/useCustomerForm.ts +156 -0
  226. package/src/checkout/hooks/useCustomerFormFeature.ts +7 -0
  227. package/src/checkout/hooks/useExchangeRate.ts +28 -0
  228. package/src/checkout/hooks/useLineItems.ts +191 -0
  229. package/src/checkout/hooks/usePaymentMethod.ts +165 -0
  230. package/src/checkout/hooks/usePaymentMethodFeature.ts +8 -0
  231. package/src/checkout/hooks/usePricing.ts +274 -0
  232. package/src/checkout/hooks/usePricingFeature.ts +73 -0
  233. package/src/checkout/hooks/useProduct.ts +32 -0
  234. package/src/checkout/hooks/usePromotion.ts +67 -0
  235. package/src/checkout/hooks/useSlippage.ts +39 -0
  236. package/src/checkout/hooks/useSubmit.ts +684 -0
  237. package/src/checkout/hooks/useSubmitFeature.ts +7 -0
  238. package/src/checkout/hooks/useUpsell.ts +35 -0
  239. package/src/checkout/index.ts +65 -0
  240. package/src/checkout/types.ts +292 -0
  241. package/src/index.ts +64 -0
  242. package/src/shared/api.ts +118 -0
  243. package/src/shared/format.ts +318 -0
  244. package/src/shared/polling.ts +49 -0
  245. package/src/shared/types.ts +13 -0
  246. package/src/shared/validation.ts +254 -0
  247. package/src/types/checkout-augmented.ts +77 -0
  248. package/src/types/external.d.ts +18 -0
  249. package/tools/jest.js +1 -0
  250. package/tsconfig.json +18 -0
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePricing = usePricing;
7
+ var _react = require("react");
8
+ var _ahooks = require("ahooks");
9
+ var _checkoutAugmented = require("../../types/checkout-augmented");
10
+ var _format = require("../../shared/format");
11
+ var _exchangeRate = require("../core/exchangeRate");
12
+ var _pricing = require("../core/pricing");
13
+ var _promotion = require("../core/promotion");
14
+ var _submit = require("../core/submit");
15
+ function usePricing(sessionData, sessionId, currency, isStripe, refreshSession, paymentMethodType) {
16
+ const session = sessionData?.checkoutSession;
17
+ const items = session?.line_items || [];
18
+ const [exchangeRate, setExchangeRate] = (0, _react.useState)(null);
19
+ const [rateProvider, setRateProvider] = (0, _react.useState)(null);
20
+ const [rateProviderDisplay, setRateProviderDisplay] = (0, _react.useState)(null);
21
+ const [rateFetchedAt, setRateFetchedAt] = (0, _react.useState)(null);
22
+ const [rateStatus, setRateStatus] = (0, _react.useState)("loading");
23
+ const [promotionCode, setPromotionCode] = (0, _react.useState)(null);
24
+ (0, _react.useEffect)(() => {
25
+ if (!session || promotionCode) return;
26
+ const code = (0, _promotion.extractPromotionCodeFromSession)(session);
27
+ if (code) setPromotionCode(code);
28
+ }, [session?.id]);
29
+ const pollingRef = (0, _react.useRef)(null);
30
+ const failCountRef = (0, _react.useRef)(0);
31
+ const mountedRef = (0, _react.useRef)(true);
32
+ const hasDynamicPricing = (0, _react.useMemo)(() => (0, _exchangeRate.checkHasDynamicPricing)(items), [items]);
33
+ const fetchRate = (0, _ahooks.useMemoizedFn)(async () => {
34
+ if (!sessionId || !hasDynamicPricing || isStripe) {
35
+ setRateStatus(hasDynamicPricing ? "unavailable" : "available");
36
+ if (isStripe) {
37
+ setExchangeRate(null);
38
+ setRateProvider(null);
39
+ setRateProviderDisplay(null);
40
+ setRateFetchedAt(null);
41
+ }
42
+ return;
43
+ }
44
+ try {
45
+ setRateStatus("loading");
46
+ const result = await (0, _exchangeRate.fetchExchangeRate)(sessionId, currency?.id);
47
+ if (!mountedRef.current) return;
48
+ if (result.rate) {
49
+ setExchangeRate(result.rate);
50
+ setRateProvider(result.provider);
51
+ setRateProviderDisplay(result.providerDisplay);
52
+ setRateFetchedAt(result.fetchedAt);
53
+ setRateStatus("available");
54
+ failCountRef.current = 0;
55
+ } else {
56
+ setRateStatus("unavailable");
57
+ }
58
+ } catch {
59
+ if (!mountedRef.current) return;
60
+ failCountRef.current += 1;
61
+ setRateStatus("unavailable");
62
+ }
63
+ });
64
+ (0, _react.useEffect)(() => {
65
+ mountedRef.current = true;
66
+ if (!hasDynamicPricing || isStripe || !sessionId || session?.status === "complete") {
67
+ if (isStripe) {
68
+ setExchangeRate(null);
69
+ setRateProvider(null);
70
+ setRateProviderDisplay(null);
71
+ setRateFetchedAt(null);
72
+ setRateStatus("unavailable");
73
+ } else if (session?.status === "complete") {
74
+ setRateStatus("available");
75
+ }
76
+ return void 0;
77
+ }
78
+ fetchRate();
79
+ const scheduleNext = () => {
80
+ const backoff = (0, _exchangeRate.getBackoffInterval)(failCountRef.current);
81
+ pollingRef.current = setTimeout(() => {
82
+ fetchRate().then(scheduleNext);
83
+ }, backoff);
84
+ };
85
+ scheduleNext();
86
+ const handleVisibility = () => {
87
+ if (document.hidden) {
88
+ if (pollingRef.current) clearTimeout(pollingRef.current);
89
+ } else if (session?.status !== "complete") {
90
+ fetchRate().then(scheduleNext);
91
+ }
92
+ };
93
+ document.addEventListener("visibilitychange", handleVisibility);
94
+ return () => {
95
+ mountedRef.current = false;
96
+ if (pollingRef.current) clearTimeout(pollingRef.current);
97
+ document.removeEventListener("visibilitychange", handleVisibility);
98
+ };
99
+ }, [hasDynamicPricing, isStripe, sessionId, currency?.id, session?.status]);
100
+ const amounts = (0, _react.useMemo)(() => (0, _pricing.calculateAmounts)(items, currency, session, exchangeRate, hasDynamicPricing, paymentMethodType), [items, currency, exchangeRate, hasDynamicPricing, session, paymentMethodType]);
101
+ const quoteMeta = (0, _react.useMemo)(() => (0, _pricing.calculateQuoteMeta)(items, hasDynamicPricing, sessionData), [items, hasDynamicPricing, sessionData]);
102
+ const setSlippage = (0, _ahooks.useMemoizedFn)(async config => {
103
+ try {
104
+ const data = await (0, _submit.updateSlippage)(sessionId, config);
105
+ if (data?.checkoutSession || data?.quotes) {
106
+ await refreshSession(true);
107
+ }
108
+ } catch (err) {
109
+ console.error("Failed to update slippage:", (0, _checkoutAugmented.getErrorMessage)(err));
110
+ }
111
+ });
112
+ const currentSlippagePercent = quoteMeta.slippagePercent ?? 0.5;
113
+ (0, _react.useEffect)(() => {
114
+ if (!quoteMeta.expired || !quoteMeta.expiresAt || session?.status === "complete") return;
115
+ refreshSession(true);
116
+ }, [quoteMeta.expired]);
117
+ const applyPromotion = (0, _ahooks.useMemoizedFn)(async code => {
118
+ const result = await (0, _promotion.applyPromotionCode)(sessionId, code, currency?.id);
119
+ if (result.success) {
120
+ setPromotionCode(code);
121
+ await refreshSession(true);
122
+ }
123
+ return result;
124
+ });
125
+ const removePromotion = (0, _ahooks.useMemoizedFn)(async () => {
126
+ try {
127
+ await (0, _promotion.removePromotionCode)(sessionId);
128
+ setPromotionCode(null);
129
+ await refreshSession(true);
130
+ } catch {}
131
+ });
132
+ const promotionApplied = (session?.discounts || []).length > 0;
133
+ const promotionActive = (0, _promotion.isPromotionActive)(session);
134
+ const trial = (0, _pricing.calculateTrial)(session, currency?.id, items, currency, hasDynamicPricing, exchangeRate);
135
+ return {
136
+ ...amounts,
137
+ hasDynamicPricing,
138
+ rate: {
139
+ value: (0, _format.formatExchangeRate)(exchangeRate),
140
+ display: exchangeRate ? (0, _format.formatExchangeRateDisplay)(exchangeRate) : null,
141
+ provider: rateProvider,
142
+ providerDisplay: rateProviderDisplay,
143
+ fetchedAt: rateFetchedAt,
144
+ status: rateStatus,
145
+ refresh: fetchRate
146
+ },
147
+ quote: {
148
+ locked: quoteMeta.locked,
149
+ lockedAt: quoteMeta.lockedAt,
150
+ expiresAt: quoteMeta.expiresAt,
151
+ expired: quoteMeta.expired,
152
+ baseCurrency: quoteMeta.baseCurrency
153
+ },
154
+ slippage: {
155
+ percent: currentSlippagePercent,
156
+ set: setSlippage
157
+ },
158
+ promotion: {
159
+ applied: promotionApplied,
160
+ code: promotionCode,
161
+ active: promotionActive,
162
+ inactiveReason: !promotionActive ? "Promotions not allowed" : null,
163
+ apply: applyPromotion,
164
+ remove: removePromotion
165
+ },
166
+ trial
167
+ };
168
+ }
@@ -0,0 +1,28 @@
1
+ export interface UsePricingFeatureReturn {
2
+ subtotal: string;
3
+ paymentAmount: string;
4
+ discount: string | null;
5
+ tax: {
6
+ amount: string;
7
+ rate: number;
8
+ inclusive: boolean;
9
+ } | null;
10
+ staking: string | null;
11
+ total: string;
12
+ usdEquivalent: string | null;
13
+ hasDynamicPricing: boolean;
14
+ quote: {
15
+ locked: boolean;
16
+ lockedAt: number | null;
17
+ expiresAt: number | null;
18
+ expired: boolean;
19
+ baseCurrency: string | null;
20
+ };
21
+ trial: {
22
+ active: boolean;
23
+ days: number;
24
+ afterTrialPrice: string | null;
25
+ afterTrialInterval: string | null;
26
+ };
27
+ }
28
+ export declare function usePricingFeature(): UsePricingFeatureReturn;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePricingFeature = usePricingFeature;
7
+ var _react = require("react");
8
+ var _SessionContext = require("../context/SessionContext");
9
+ var _PaymentMethodContext = require("../context/PaymentMethodContext");
10
+ var _ExchangeRateContext = require("../context/ExchangeRateContext");
11
+ var _pricing = require("../core/pricing");
12
+ function usePricingFeature() {
13
+ const {
14
+ items,
15
+ session,
16
+ sessionData,
17
+ refresh
18
+ } = (0, _SessionContext.useSessionContext)();
19
+ const {
20
+ currency
21
+ } = (0, _PaymentMethodContext.usePaymentMethodContext)();
22
+ const {
23
+ rate: exchangeRate,
24
+ hasDynamicPricing
25
+ } = (0, _ExchangeRateContext.useExchangeRateContext)();
26
+ const {
27
+ current: currentMethod
28
+ } = (0, _PaymentMethodContext.usePaymentMethodContext)();
29
+ const amounts = (0, _react.useMemo)(() => (0, _pricing.calculateAmounts)(items, currency, session, exchangeRate, hasDynamicPricing, currentMethod?.type || null), [items, currency, exchangeRate, hasDynamicPricing, session, currentMethod?.type]);
30
+ const quoteMeta = (0, _react.useMemo)(() => (0, _pricing.calculateQuoteMeta)(items, hasDynamicPricing, sessionData), [items, hasDynamicPricing, sessionData]);
31
+ (0, _react.useEffect)(() => {
32
+ if (!quoteMeta.expired || !quoteMeta.expiresAt || session?.status === "complete") return;
33
+ refresh(true);
34
+ }, [quoteMeta.expired]);
35
+ const trial = (0, _pricing.calculateTrial)(session, currency?.id, items, currency, hasDynamicPricing, exchangeRate);
36
+ return {
37
+ ...amounts,
38
+ hasDynamicPricing,
39
+ quote: {
40
+ locked: quoteMeta.locked,
41
+ lockedAt: quoteMeta.lockedAt,
42
+ expiresAt: quoteMeta.expiresAt,
43
+ expired: quoteMeta.expired,
44
+ baseCurrency: quoteMeta.baseCurrency
45
+ },
46
+ trial
47
+ };
48
+ }
@@ -0,0 +1,32 @@
1
+ export interface UseProductReturn {
2
+ product: {
3
+ name: string;
4
+ description: string;
5
+ images: string[];
6
+ features: Array<{
7
+ name: string;
8
+ icon?: string;
9
+ }>;
10
+ billing: {
11
+ mode: 'payment' | 'subscription';
12
+ interval: 'month' | 'year' | 'week' | 'day' | null;
13
+ intervalCount: number;
14
+ displayInterval: string;
15
+ };
16
+ metadata: Record<string, string>;
17
+ } | null;
18
+ subscription: {
19
+ mode: 'payment' | 'subscription' | 'setup';
20
+ showStake: boolean;
21
+ confirmMessage: string;
22
+ } | null;
23
+ pageInfo: {
24
+ formPurposeDescription?: {
25
+ en: string;
26
+ zh: string;
27
+ };
28
+ showProductFeatures: boolean;
29
+ } | null;
30
+ vendorCount: number;
31
+ }
32
+ export declare function useProduct(): UseProductReturn;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useProduct = useProduct;
7
+ var _SessionContext = require("../context/SessionContext");
8
+ function useProduct() {
9
+ const {
10
+ product,
11
+ subscription,
12
+ pageInfo,
13
+ vendorCount
14
+ } = (0, _SessionContext.useSessionContext)();
15
+ return {
16
+ product,
17
+ subscription,
18
+ pageInfo,
19
+ vendorCount
20
+ };
21
+ }
@@ -0,0 +1,12 @@
1
+ export interface UsePromotionReturn {
2
+ applied: boolean;
3
+ code: string | null;
4
+ active: boolean;
5
+ inactiveReason: string | null;
6
+ apply: (code: string) => Promise<{
7
+ success: boolean;
8
+ error?: string;
9
+ }>;
10
+ remove: () => Promise<void>;
11
+ }
12
+ export declare function usePromotion(): UsePromotionReturn;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePromotion = usePromotion;
7
+ var _react = require("react");
8
+ var _ahooks = require("ahooks");
9
+ var _SessionContext = require("../context/SessionContext");
10
+ var _PaymentMethodContext = require("../context/PaymentMethodContext");
11
+ var _promotion = require("../core/promotion");
12
+ function usePromotion() {
13
+ const {
14
+ session,
15
+ effectiveSessionId,
16
+ refresh
17
+ } = (0, _SessionContext.useSessionContext)();
18
+ const {
19
+ currency
20
+ } = (0, _PaymentMethodContext.usePaymentMethodContext)();
21
+ const [promotionCode, setPromotionCode] = (0, _react.useState)(null);
22
+ (0, _react.useEffect)(() => {
23
+ if (!session || promotionCode) return;
24
+ const code = (0, _promotion.extractPromotionCodeFromSession)(session);
25
+ if (code) setPromotionCode(code);
26
+ }, [session?.id]);
27
+ const apply = (0, _ahooks.useMemoizedFn)(async code => {
28
+ if (session?.status === "complete") return {
29
+ success: false,
30
+ error: "Session already completed"
31
+ };
32
+ const result = await (0, _promotion.applyPromotionCode)(effectiveSessionId, code, currency?.id);
33
+ if (result.success) {
34
+ setPromotionCode(code);
35
+ await refresh(true);
36
+ }
37
+ return result;
38
+ });
39
+ const remove = (0, _ahooks.useMemoizedFn)(async () => {
40
+ if (session?.status === "complete") return;
41
+ try {
42
+ await (0, _promotion.removePromotionCode)(effectiveSessionId);
43
+ setPromotionCode(null);
44
+ await refresh(true);
45
+ } catch {}
46
+ });
47
+ const promotionApplied = (session?.discounts || []).length > 0;
48
+ const promotionActive = (0, _promotion.isPromotionActive)(session);
49
+ return {
50
+ applied: promotionApplied,
51
+ code: promotionCode,
52
+ active: promotionActive,
53
+ inactiveReason: !promotionActive ? "Promotions not allowed" : null,
54
+ apply,
55
+ remove
56
+ };
57
+ }
@@ -0,0 +1,8 @@
1
+ export interface UseSlippageReturn {
2
+ percent: number;
3
+ set: (config: {
4
+ mode: string;
5
+ percent: number;
6
+ }) => Promise<void>;
7
+ }
8
+ export declare function useSlippage(): UseSlippageReturn;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useSlippage = useSlippage;
7
+ var _react = require("react");
8
+ var _ahooks = require("ahooks");
9
+ var _checkoutAugmented = require("../../types/checkout-augmented");
10
+ var _SessionContext = require("../context/SessionContext");
11
+ var _ExchangeRateContext = require("../context/ExchangeRateContext");
12
+ var _pricing = require("../core/pricing");
13
+ var _submit = require("../core/submit");
14
+ function useSlippage() {
15
+ const {
16
+ items,
17
+ sessionData,
18
+ effectiveSessionId,
19
+ refresh
20
+ } = (0, _SessionContext.useSessionContext)();
21
+ const {
22
+ hasDynamicPricing
23
+ } = (0, _ExchangeRateContext.useExchangeRateContext)();
24
+ const quoteMeta = (0, _react.useMemo)(() => (0, _pricing.calculateQuoteMeta)(items, hasDynamicPricing, sessionData), [items, hasDynamicPricing, sessionData]);
25
+ const set = (0, _ahooks.useMemoizedFn)(async config => {
26
+ try {
27
+ const data = await (0, _submit.updateSlippage)(effectiveSessionId, config);
28
+ if (data?.checkoutSession || data?.quotes) {
29
+ await refresh(true);
30
+ }
31
+ } catch (err) {
32
+ console.error("Failed to update slippage:", (0, _checkoutAugmented.getErrorMessage)(err));
33
+ }
34
+ });
35
+ return {
36
+ percent: quoteMeta.slippagePercent ?? 0.5,
37
+ set
38
+ };
39
+ }
@@ -0,0 +1,38 @@
1
+ import type { SessionData } from './useCheckoutSession';
2
+ import type { SubmitStatus, SubmitContext, CheckoutResult, CheckoutFormData } from '../types';
3
+ export interface VendorStatus {
4
+ success: boolean;
5
+ status: 'delivered' | 'pending' | 'failed';
6
+ progress: number;
7
+ message: string;
8
+ appUrl?: string;
9
+ title?: string;
10
+ name?: string;
11
+ vendorType: string;
12
+ }
13
+ export interface VendorOrderStatus {
14
+ payment_status: string;
15
+ session_status: string;
16
+ vendors: VendorStatus[];
17
+ error: string | null;
18
+ isAllCompleted: boolean;
19
+ hasFailed: boolean;
20
+ }
21
+ export interface UseSubmitReturn {
22
+ status: SubmitStatus;
23
+ context: SubmitContext;
24
+ execute: () => Promise<void>;
25
+ confirm: () => Promise<void>;
26
+ cancel: () => void;
27
+ result: CheckoutResult | null;
28
+ retry: () => Promise<void>;
29
+ reset: () => void;
30
+ stripeConfirm: () => Promise<void>;
31
+ stripeCancel: () => Promise<void>;
32
+ vendorStatus: VendorOrderStatus | null;
33
+ /** Whether the checkout config is locked (user clicked "Connect and Pay", pending login/submit) */
34
+ locked: boolean;
35
+ /** Lock the checkout config — prevents quantity/currency/method changes */
36
+ lock: () => void;
37
+ }
38
+ export declare function useSubmit(sessionData: SessionData | null, sessionId: string, currencyId: string | null, isStripe: boolean, isCredit: boolean, isDonation: boolean, formValues: CheckoutFormData, validateForm: () => Promise<boolean>, refreshSession: (force?: boolean) => Promise<void>, updateSessionData?: (data: SessionData) => void): UseSubmitReturn;