@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,174 @@
1
+ import { useState, useMemo, useEffect, useRef } from "react";
2
+ import { useMemoizedFn } from "ahooks";
3
+ import { getErrorMessage } from "../../types/checkout-augmented.js";
4
+ import { formatExchangeRate, formatExchangeRateDisplay } from "../../shared/format.js";
5
+ import { checkHasDynamicPricing, fetchExchangeRate, getBackoffInterval } from "../core/exchangeRate.js";
6
+ import { calculateAmounts, calculateQuoteMeta, calculateTrial } from "../core/pricing.js";
7
+ import {
8
+ extractPromotionCodeFromSession,
9
+ applyPromotionCode,
10
+ removePromotionCode,
11
+ isPromotionActive
12
+ } from "../core/promotion.js";
13
+ import { updateSlippage } from "../core/submit.js";
14
+ export function usePricing(sessionData, sessionId, currency, isStripe, refreshSession, paymentMethodType) {
15
+ const session = sessionData?.checkoutSession;
16
+ const items = session?.line_items || [];
17
+ const [exchangeRate, setExchangeRate] = useState(null);
18
+ const [rateProvider, setRateProvider] = useState(null);
19
+ const [rateProviderDisplay, setRateProviderDisplay] = useState(null);
20
+ const [rateFetchedAt, setRateFetchedAt] = useState(null);
21
+ const [rateStatus, setRateStatus] = useState("loading");
22
+ const [promotionCode, setPromotionCode] = useState(null);
23
+ useEffect(() => {
24
+ if (!session || promotionCode) return;
25
+ const code = extractPromotionCodeFromSession(session);
26
+ if (code) setPromotionCode(code);
27
+ }, [session?.id]);
28
+ const pollingRef = useRef(null);
29
+ const failCountRef = useRef(0);
30
+ const mountedRef = useRef(true);
31
+ const hasDynamicPricing = useMemo(() => checkHasDynamicPricing(items), [items]);
32
+ const fetchRate = useMemoizedFn(async () => {
33
+ if (!sessionId || !hasDynamicPricing || isStripe) {
34
+ setRateStatus(hasDynamicPricing ? "unavailable" : "available");
35
+ if (isStripe) {
36
+ setExchangeRate(null);
37
+ setRateProvider(null);
38
+ setRateProviderDisplay(null);
39
+ setRateFetchedAt(null);
40
+ }
41
+ return;
42
+ }
43
+ try {
44
+ setRateStatus("loading");
45
+ const result = await fetchExchangeRate(sessionId, currency?.id);
46
+ if (!mountedRef.current) return;
47
+ if (result.rate) {
48
+ setExchangeRate(result.rate);
49
+ setRateProvider(result.provider);
50
+ setRateProviderDisplay(result.providerDisplay);
51
+ setRateFetchedAt(result.fetchedAt);
52
+ setRateStatus("available");
53
+ failCountRef.current = 0;
54
+ } else {
55
+ setRateStatus("unavailable");
56
+ }
57
+ } catch {
58
+ if (!mountedRef.current) return;
59
+ failCountRef.current += 1;
60
+ setRateStatus("unavailable");
61
+ }
62
+ });
63
+ useEffect(() => {
64
+ mountedRef.current = true;
65
+ if (!hasDynamicPricing || isStripe || !sessionId || session?.status === "complete") {
66
+ if (isStripe) {
67
+ setExchangeRate(null);
68
+ setRateProvider(null);
69
+ setRateProviderDisplay(null);
70
+ setRateFetchedAt(null);
71
+ setRateStatus("unavailable");
72
+ } else if (session?.status === "complete") {
73
+ setRateStatus("available");
74
+ }
75
+ return void 0;
76
+ }
77
+ fetchRate();
78
+ const scheduleNext = () => {
79
+ const backoff = getBackoffInterval(failCountRef.current);
80
+ pollingRef.current = setTimeout(() => {
81
+ fetchRate().then(scheduleNext);
82
+ }, backoff);
83
+ };
84
+ scheduleNext();
85
+ const handleVisibility = () => {
86
+ if (document.hidden) {
87
+ if (pollingRef.current) clearTimeout(pollingRef.current);
88
+ } else if (session?.status !== "complete") {
89
+ fetchRate().then(scheduleNext);
90
+ }
91
+ };
92
+ document.addEventListener("visibilitychange", handleVisibility);
93
+ return () => {
94
+ mountedRef.current = false;
95
+ if (pollingRef.current) clearTimeout(pollingRef.current);
96
+ document.removeEventListener("visibilitychange", handleVisibility);
97
+ };
98
+ }, [hasDynamicPricing, isStripe, sessionId, currency?.id, session?.status]);
99
+ const amounts = useMemo(
100
+ () => calculateAmounts(items, currency, session, exchangeRate, hasDynamicPricing, paymentMethodType),
101
+ [items, currency, exchangeRate, hasDynamicPricing, session, paymentMethodType]
102
+ );
103
+ const quoteMeta = useMemo(
104
+ () => calculateQuoteMeta(items, hasDynamicPricing, sessionData),
105
+ [items, hasDynamicPricing, sessionData]
106
+ );
107
+ const setSlippage = useMemoizedFn(async (config) => {
108
+ try {
109
+ const data = await updateSlippage(sessionId, config);
110
+ if (data?.checkoutSession || data?.quotes) {
111
+ await refreshSession(true);
112
+ }
113
+ } catch (err) {
114
+ console.error("Failed to update slippage:", getErrorMessage(err));
115
+ }
116
+ });
117
+ const currentSlippagePercent = quoteMeta.slippagePercent ?? 0.5;
118
+ useEffect(() => {
119
+ if (!quoteMeta.expired || !quoteMeta.expiresAt || session?.status === "complete") return;
120
+ refreshSession(true);
121
+ }, [quoteMeta.expired]);
122
+ const applyPromotion = useMemoizedFn(async (code) => {
123
+ const result = await applyPromotionCode(sessionId, code, currency?.id);
124
+ if (result.success) {
125
+ setPromotionCode(code);
126
+ await refreshSession(true);
127
+ }
128
+ return result;
129
+ });
130
+ const removePromotion = useMemoizedFn(async () => {
131
+ try {
132
+ await removePromotionCode(sessionId);
133
+ setPromotionCode(null);
134
+ await refreshSession(true);
135
+ } catch {
136
+ }
137
+ });
138
+ const promotionApplied = (session?.discounts || []).length > 0;
139
+ const promotionActive = isPromotionActive(session);
140
+ const trial = calculateTrial(session, currency?.id, items, currency, hasDynamicPricing, exchangeRate);
141
+ return {
142
+ ...amounts,
143
+ hasDynamicPricing,
144
+ rate: {
145
+ value: formatExchangeRate(exchangeRate),
146
+ display: exchangeRate ? formatExchangeRateDisplay(exchangeRate) : null,
147
+ provider: rateProvider,
148
+ providerDisplay: rateProviderDisplay,
149
+ fetchedAt: rateFetchedAt,
150
+ status: rateStatus,
151
+ refresh: fetchRate
152
+ },
153
+ quote: {
154
+ locked: quoteMeta.locked,
155
+ lockedAt: quoteMeta.lockedAt,
156
+ expiresAt: quoteMeta.expiresAt,
157
+ expired: quoteMeta.expired,
158
+ baseCurrency: quoteMeta.baseCurrency
159
+ },
160
+ slippage: {
161
+ percent: currentSlippagePercent,
162
+ set: setSlippage
163
+ },
164
+ promotion: {
165
+ applied: promotionApplied,
166
+ code: promotionCode,
167
+ active: promotionActive,
168
+ inactiveReason: !promotionActive ? "Promotions not allowed" : null,
169
+ apply: applyPromotion,
170
+ remove: removePromotion
171
+ },
172
+ trial
173
+ };
174
+ }
@@ -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,36 @@
1
+ import { useMemo, useEffect } from "react";
2
+ import { useSessionContext } from "../context/SessionContext.js";
3
+ import { usePaymentMethodContext } from "../context/PaymentMethodContext.js";
4
+ import { useExchangeRateContext } from "../context/ExchangeRateContext.js";
5
+ import { calculateAmounts, calculateQuoteMeta, calculateTrial } from "../core/pricing.js";
6
+ export function usePricingFeature() {
7
+ const { items, session, sessionData, refresh } = useSessionContext();
8
+ const { currency } = usePaymentMethodContext();
9
+ const { rate: exchangeRate, hasDynamicPricing } = useExchangeRateContext();
10
+ const { current: currentMethod } = usePaymentMethodContext();
11
+ const amounts = useMemo(
12
+ () => calculateAmounts(items, currency, session, exchangeRate, hasDynamicPricing, currentMethod?.type || null),
13
+ [items, currency, exchangeRate, hasDynamicPricing, session, currentMethod?.type]
14
+ );
15
+ const quoteMeta = useMemo(
16
+ () => calculateQuoteMeta(items, hasDynamicPricing, sessionData),
17
+ [items, hasDynamicPricing, sessionData]
18
+ );
19
+ useEffect(() => {
20
+ if (!quoteMeta.expired || !quoteMeta.expiresAt || session?.status === "complete") return;
21
+ refresh(true);
22
+ }, [quoteMeta.expired]);
23
+ const trial = calculateTrial(session, currency?.id, items, currency, hasDynamicPricing, exchangeRate);
24
+ return {
25
+ ...amounts,
26
+ hasDynamicPricing,
27
+ quote: {
28
+ locked: quoteMeta.locked,
29
+ lockedAt: quoteMeta.lockedAt,
30
+ expiresAt: quoteMeta.expiresAt,
31
+ expired: quoteMeta.expired,
32
+ baseCurrency: quoteMeta.baseCurrency
33
+ },
34
+ trial
35
+ };
36
+ }
@@ -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,5 @@
1
+ import { useSessionContext } from "../context/SessionContext.js";
2
+ export function useProduct() {
3
+ const { product, subscription, pageInfo, vendorCount } = useSessionContext();
4
+ return { product, subscription, pageInfo, vendorCount };
5
+ }
@@ -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,48 @@
1
+ import { useState, useEffect } from "react";
2
+ import { useMemoizedFn } from "ahooks";
3
+ import { useSessionContext } from "../context/SessionContext.js";
4
+ import { usePaymentMethodContext } from "../context/PaymentMethodContext.js";
5
+ import {
6
+ extractPromotionCodeFromSession,
7
+ applyPromotionCode,
8
+ removePromotionCode,
9
+ isPromotionActive
10
+ } from "../core/promotion.js";
11
+ export function usePromotion() {
12
+ const { session, effectiveSessionId, refresh } = useSessionContext();
13
+ const { currency } = usePaymentMethodContext();
14
+ const [promotionCode, setPromotionCode] = useState(null);
15
+ useEffect(() => {
16
+ if (!session || promotionCode) return;
17
+ const code = extractPromotionCodeFromSession(session);
18
+ if (code) setPromotionCode(code);
19
+ }, [session?.id]);
20
+ const apply = useMemoizedFn(async (code) => {
21
+ if (session?.status === "complete") return { success: false, error: "Session already completed" };
22
+ const result = await applyPromotionCode(effectiveSessionId, code, currency?.id);
23
+ if (result.success) {
24
+ setPromotionCode(code);
25
+ await refresh(true);
26
+ }
27
+ return result;
28
+ });
29
+ const remove = useMemoizedFn(async () => {
30
+ if (session?.status === "complete") return;
31
+ try {
32
+ await removePromotionCode(effectiveSessionId);
33
+ setPromotionCode(null);
34
+ await refresh(true);
35
+ } catch {
36
+ }
37
+ });
38
+ const promotionApplied = (session?.discounts || []).length > 0;
39
+ const promotionActive = isPromotionActive(session);
40
+ return {
41
+ applied: promotionApplied,
42
+ code: promotionCode,
43
+ active: promotionActive,
44
+ inactiveReason: !promotionActive ? "Promotions not allowed" : null,
45
+ apply,
46
+ remove
47
+ };
48
+ }
@@ -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,29 @@
1
+ import { useMemo } from "react";
2
+ import { useMemoizedFn } from "ahooks";
3
+ import { getErrorMessage } from "../../types/checkout-augmented.js";
4
+ import { useSessionContext } from "../context/SessionContext.js";
5
+ import { useExchangeRateContext } from "../context/ExchangeRateContext.js";
6
+ import { calculateQuoteMeta } from "../core/pricing.js";
7
+ import { updateSlippage } from "../core/submit.js";
8
+ export function useSlippage() {
9
+ const { items, sessionData, effectiveSessionId, refresh } = useSessionContext();
10
+ const { hasDynamicPricing } = useExchangeRateContext();
11
+ const quoteMeta = useMemo(
12
+ () => calculateQuoteMeta(items, hasDynamicPricing, sessionData),
13
+ [items, hasDynamicPricing, sessionData]
14
+ );
15
+ const set = useMemoizedFn(async (config) => {
16
+ try {
17
+ const data = await updateSlippage(effectiveSessionId, config);
18
+ if (data?.checkoutSession || data?.quotes) {
19
+ await refresh(true);
20
+ }
21
+ } catch (err) {
22
+ console.error("Failed to update slippage:", getErrorMessage(err));
23
+ }
24
+ });
25
+ return {
26
+ percent: quoteMeta.slippagePercent ?? 0.5,
27
+ set
28
+ };
29
+ }
@@ -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;