@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,34 @@
1
+ export { useCheckout } from './useCheckout';
2
+ export { useCheckoutSession } from './useCheckoutSession';
3
+ export type { UseCheckoutSessionReturn, SessionData } from './useCheckoutSession';
4
+ export { usePaymentMethod } from './usePaymentMethod';
5
+ export type { UsePaymentMethodReturn } from './usePaymentMethod';
6
+ export { usePricing } from './usePricing';
7
+ export type { UsePricingReturn } from './usePricing';
8
+ export { useCustomerForm } from './useCustomerForm';
9
+ export type { UseCustomerFormReturn } from './useCustomerForm';
10
+ export { useSubmit } from './useSubmit';
11
+ export type { UseSubmitReturn, VendorStatus, VendorOrderStatus } from './useSubmit';
12
+ export { useProduct } from './useProduct';
13
+ export type { UseProductReturn } from './useProduct';
14
+ export { useLineItems } from './useLineItems';
15
+ export type { UseLineItemsReturn } from './useLineItems';
16
+ export { useBillingInterval } from './useBillingInterval';
17
+ export type { UseBillingIntervalReturn, BillingIntervalData } from './useBillingInterval';
18
+ export { useUpsell } from './useUpsell';
19
+ export type { UseUpsellReturn } from './useUpsell';
20
+ export { useCrossSell } from './useCrossSell';
21
+ export type { UseCrossSellReturn } from './useCrossSell';
22
+ export { usePromotion } from './usePromotion';
23
+ export type { UsePromotionReturn } from './usePromotion';
24
+ export { useExchangeRate } from './useExchangeRate';
25
+ export type { UseExchangeRateReturn } from './useExchangeRate';
26
+ export { useSlippage } from './useSlippage';
27
+ export type { UseSlippageReturn } from './useSlippage';
28
+ export { useCheckoutStatus } from './useCheckoutStatus';
29
+ export type { UseCheckoutStatusReturn } from './useCheckoutStatus';
30
+ export { usePricingFeature } from './usePricingFeature';
31
+ export type { UsePricingFeatureReturn } from './usePricingFeature';
32
+ export { usePaymentMethodFeature } from './usePaymentMethodFeature';
33
+ export { useCustomerFormFeature } from './useCustomerFormFeature';
34
+ export { useSubmitFeature } from './useSubmitFeature';
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "useBillingInterval", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _useBillingInterval.useBillingInterval;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "useCheckout", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _useCheckout.useCheckout;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "useCheckoutSession", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _useCheckoutSession.useCheckoutSession;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "useCheckoutStatus", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _useCheckoutStatus.useCheckoutStatus;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "useCrossSell", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _useCrossSell.useCrossSell;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "useCustomerForm", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _useCustomerForm.useCustomerForm;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "useCustomerFormFeature", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _useCustomerFormFeature.useCustomerFormFeature;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "useExchangeRate", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _useExchangeRate.useExchangeRate;
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "useLineItems", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _useLineItems.useLineItems;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "usePaymentMethod", {
61
+ enumerable: true,
62
+ get: function () {
63
+ return _usePaymentMethod.usePaymentMethod;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "usePaymentMethodFeature", {
67
+ enumerable: true,
68
+ get: function () {
69
+ return _usePaymentMethodFeature.usePaymentMethodFeature;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "usePricing", {
73
+ enumerable: true,
74
+ get: function () {
75
+ return _usePricing.usePricing;
76
+ }
77
+ });
78
+ Object.defineProperty(exports, "usePricingFeature", {
79
+ enumerable: true,
80
+ get: function () {
81
+ return _usePricingFeature.usePricingFeature;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "useProduct", {
85
+ enumerable: true,
86
+ get: function () {
87
+ return _useProduct.useProduct;
88
+ }
89
+ });
90
+ Object.defineProperty(exports, "usePromotion", {
91
+ enumerable: true,
92
+ get: function () {
93
+ return _usePromotion.usePromotion;
94
+ }
95
+ });
96
+ Object.defineProperty(exports, "useSlippage", {
97
+ enumerable: true,
98
+ get: function () {
99
+ return _useSlippage.useSlippage;
100
+ }
101
+ });
102
+ Object.defineProperty(exports, "useSubmit", {
103
+ enumerable: true,
104
+ get: function () {
105
+ return _useSubmit.useSubmit;
106
+ }
107
+ });
108
+ Object.defineProperty(exports, "useSubmitFeature", {
109
+ enumerable: true,
110
+ get: function () {
111
+ return _useSubmitFeature.useSubmitFeature;
112
+ }
113
+ });
114
+ Object.defineProperty(exports, "useUpsell", {
115
+ enumerable: true,
116
+ get: function () {
117
+ return _useUpsell.useUpsell;
118
+ }
119
+ });
120
+ var _useCheckout = require("./useCheckout");
121
+ var _useCheckoutSession = require("./useCheckoutSession");
122
+ var _usePaymentMethod = require("./usePaymentMethod");
123
+ var _usePricing = require("./usePricing");
124
+ var _useCustomerForm = require("./useCustomerForm");
125
+ var _useSubmit = require("./useSubmit");
126
+ var _useProduct = require("./useProduct");
127
+ var _useLineItems = require("./useLineItems");
128
+ var _useBillingInterval = require("./useBillingInterval");
129
+ var _useUpsell = require("./useUpsell");
130
+ var _useCrossSell = require("./useCrossSell");
131
+ var _usePromotion = require("./usePromotion");
132
+ var _useExchangeRate = require("./useExchangeRate");
133
+ var _useSlippage = require("./useSlippage");
134
+ var _useCheckoutStatus = require("./useCheckoutStatus");
135
+ var _usePricingFeature = require("./usePricingFeature");
136
+ var _usePaymentMethodFeature = require("./usePaymentMethodFeature");
137
+ var _useCustomerFormFeature = require("./useCustomerFormFeature");
138
+ var _useSubmitFeature = require("./useSubmitFeature");
@@ -0,0 +1,14 @@
1
+ import { type BillingIntervalType } from '../core/billingInterval';
2
+ export interface BillingIntervalData {
3
+ current: BillingIntervalType | null;
4
+ available: Array<{
5
+ interval: BillingIntervalType;
6
+ priceId: string;
7
+ amount: string;
8
+ savings: string | null;
9
+ }>;
10
+ switch: (interval: BillingIntervalType) => Promise<void>;
11
+ switching: boolean;
12
+ }
13
+ export type UseBillingIntervalReturn = BillingIntervalData | null;
14
+ export declare function useBillingInterval(): UseBillingIntervalReturn;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useBillingInterval = useBillingInterval;
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 _PaymentMethodContext = require("../context/PaymentMethodContext");
12
+ var _billingInterval = require("../core/billingInterval");
13
+ var _lineItems = require("../core/lineItems");
14
+ function useBillingInterval() {
15
+ const {
16
+ items,
17
+ session,
18
+ effectiveSessionId,
19
+ refresh
20
+ } = (0, _SessionContext.useSessionContext)();
21
+ const {
22
+ currency
23
+ } = (0, _PaymentMethodContext.usePaymentMethodContext)();
24
+ const currencyId = currency?.id || null;
25
+ const [switching, setSwitching] = (0, _react.useState)(false);
26
+ const upsell = (0, _ahooks.useMemoizedFn)(async (fromId, toId) => {
27
+ try {
28
+ await (0, _lineItems.performUpsell)(effectiveSessionId, fromId, toId, session, currencyId, refresh);
29
+ } catch (err) {
30
+ console.error("Failed to upsell:", (0, _checkoutAugmented.getErrorMessage)(err));
31
+ }
32
+ });
33
+ const downsell = (0, _ahooks.useMemoizedFn)(async priceId => {
34
+ try {
35
+ await (0, _lineItems.performDownsell)(effectiveSessionId, priceId, session, currencyId, refresh);
36
+ } catch (err) {
37
+ console.error("Failed to downsell:", (0, _checkoutAugmented.getErrorMessage)(err));
38
+ }
39
+ });
40
+ return (0, _react.useMemo)(() => {
41
+ const parsed = (0, _billingInterval.parseBillingInterval)(items);
42
+ if (!parsed) return null;
43
+ return {
44
+ current: parsed.current,
45
+ available: parsed.available,
46
+ switching,
47
+ switch: async interval => {
48
+ const target = parsed.available.find(a => a.interval === interval);
49
+ if (!target || switching) return;
50
+ setSwitching(true);
51
+ try {
52
+ if (!parsed.firstItem.upsell_price_id && target.priceId) {
53
+ await upsell(parsed.firstItem.price_id, target.priceId);
54
+ } else if (parsed.firstItem.upsell_price_id) {
55
+ await downsell(parsed.firstItem.upsell_price_id);
56
+ }
57
+ } finally {
58
+ setSwitching(false);
59
+ }
60
+ }
61
+ };
62
+ }, [items, effectiveSessionId, switching]);
63
+ }
@@ -0,0 +1,2 @@
1
+ import type { UseCheckoutReturn } from '../types';
2
+ export declare function useCheckout(sessionId: string): UseCheckoutReturn;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useCheckout = useCheckout;
7
+ var _react = require("react");
8
+ var _ahooks = require("ahooks");
9
+ var _checkoutAugmented = require("../../types/checkout-augmented");
10
+ var _useCheckoutSession = require("./useCheckoutSession");
11
+ var _usePaymentMethod = require("./usePaymentMethod");
12
+ var _usePricing = require("./usePricing");
13
+ var _useCustomerForm = require("./useCustomerForm");
14
+ var _useSubmit = require("./useSubmit");
15
+ var _lineItems = require("../core/lineItems");
16
+ var _crossSell = require("../core/crossSell");
17
+ var _billingInterval = require("../core/billingInterval");
18
+ function useCheckout(sessionId) {
19
+ const {
20
+ isLoading,
21
+ error,
22
+ errorCode,
23
+ refresh,
24
+ sessionData,
25
+ resolvedSessionId,
26
+ vendorCount,
27
+ product,
28
+ subscription,
29
+ pageInfo
30
+ } = (0, _useCheckoutSession.useCheckoutSession)(sessionId);
31
+ const session = sessionData?.checkoutSession;
32
+ const effectiveSessionId = resolvedSessionId || sessionId;
33
+ const paymentMethodHook = (0, _usePaymentMethod.usePaymentMethod)(sessionData, effectiveSessionId, refresh);
34
+ const pricingHook = (0, _usePricing.usePricing)(sessionData, effectiveSessionId, paymentMethodHook.currency, paymentMethodHook.isStripe, refresh, paymentMethodHook.current?.type || null);
35
+ const formHook = (0, _useCustomerForm.useCustomerForm)(sessionData, paymentMethodHook.currency?.id || null, paymentMethodHook.current?.id || null);
36
+ const isDonation = session?.submit_type === "donate";
37
+ const submitHook = (0, _useSubmit.useSubmit)(sessionData, effectiveSessionId, paymentMethodHook.currency?.id || null, paymentMethodHook.isStripe, paymentMethodHook.isCredit, isDonation, formHook.values, formHook.validate, refresh);
38
+ const items = session?.line_items || [];
39
+ const currencyId = paymentMethodHook.currency?.id || null;
40
+ const prevCurrencyRef = (0, _react.useRef)(null);
41
+ (0, _react.useEffect)(() => {
42
+ const currId = paymentMethodHook.currency?.id || null;
43
+ if (!currId || !session) return;
44
+ if (prevCurrencyRef.current === null || currId !== prevCurrencyRef.current) {
45
+ prevCurrencyRef.current = currId;
46
+ (0, _lineItems.recalculatePromotionIfNeeded)(session, effectiveSessionId, currId).then(() => refresh(true));
47
+ }
48
+ }, [paymentMethodHook.currency?.id, session?.id]);
49
+ const updateQuantity = (0, _ahooks.useMemoizedFn)(async (itemId, qty) => {
50
+ try {
51
+ await (0, _lineItems.adjustQuantity)(effectiveSessionId, itemId, qty, currencyId, session, refresh);
52
+ } catch (err) {
53
+ console.error("Failed to update quantity:", (0, _checkoutAugmented.getErrorMessage)(err));
54
+ }
55
+ });
56
+ const upsell = (0, _ahooks.useMemoizedFn)(async (fromId, toId) => {
57
+ try {
58
+ await (0, _lineItems.performUpsell)(effectiveSessionId, fromId, toId, session, currencyId, refresh);
59
+ } catch (err) {
60
+ console.error("Failed to upsell:", (0, _checkoutAugmented.getErrorMessage)(err));
61
+ }
62
+ });
63
+ const downsell = (0, _ahooks.useMemoizedFn)(async priceId => {
64
+ try {
65
+ await (0, _lineItems.performDownsell)(effectiveSessionId, priceId, session, currencyId, refresh);
66
+ } catch (err) {
67
+ console.error("Failed to downsell:", (0, _checkoutAugmented.getErrorMessage)(err));
68
+ }
69
+ });
70
+ const embeddedCrossSellItem = (0, _react.useMemo)(() => (0, _lineItems.getCrossSellItem)(items), [items]);
71
+ const [fetchedCrossSellItem, setFetchedCrossSellItem] = (0, _react.useState)(null);
72
+ (0, _react.useEffect)(() => {
73
+ if (!effectiveSessionId || !session) return void 0;
74
+ if (embeddedCrossSellItem) return void 0;
75
+ let cancelled = false;
76
+ (0, _crossSell.fetchCrossSellItem)(effectiveSessionId).then(item => {
77
+ if (!cancelled && item) {
78
+ setFetchedCrossSellItem(item);
79
+ }
80
+ });
81
+ return () => {
82
+ cancelled = true;
83
+ };
84
+ }, [effectiveSessionId, session?.id, embeddedCrossSellItem]);
85
+ const crossSellItem = embeddedCrossSellItem || fetchedCrossSellItem;
86
+ const crossSellRequired = (0, _react.useMemo)(() => items.some(item => item.cross_sell_required), [items]);
87
+ const addCrossSell = (0, _ahooks.useMemoizedFn)(async () => {
88
+ if (!crossSellItem) return;
89
+ try {
90
+ await (0, _crossSell.addCrossSellItem)(effectiveSessionId, crossSellItem.id, session, currencyId, refresh);
91
+ } catch (err) {
92
+ console.error("Failed to add cross-sell:", (0, _checkoutAugmented.getErrorMessage)(err));
93
+ }
94
+ });
95
+ const removeCrossSell = (0, _ahooks.useMemoizedFn)(async () => {
96
+ try {
97
+ await (0, _crossSell.removeCrossSellItem)(effectiveSessionId, session, currencyId, refresh);
98
+ } catch (err) {
99
+ console.error("Failed to remove cross-sell:", (0, _checkoutAugmented.getErrorMessage)(err));
100
+ }
101
+ });
102
+ const billingInterval = (0, _react.useMemo)(() => {
103
+ const parsed = (0, _billingInterval.parseBillingInterval)(items);
104
+ if (!parsed) return null;
105
+ return {
106
+ current: parsed.current,
107
+ available: parsed.available,
108
+ switch: async interval => {
109
+ const target = parsed.available.find(a => a.interval === interval);
110
+ if (!target) return;
111
+ if (!parsed.firstItem.upsell_price_id && target.priceId) {
112
+ await upsell(parsed.firstItem.price_id, target.priceId);
113
+ } else if (parsed.firstItem.upsell_price_id) {
114
+ await downsell(parsed.firstItem.upsell_price_id);
115
+ }
116
+ }
117
+ };
118
+ }, [items, effectiveSessionId]);
119
+ const setDonationAmount = (0, _ahooks.useMemoizedFn)(async (priceId, amount) => {
120
+ if (!isDonation) return;
121
+ try {
122
+ await (0, _lineItems.changeDonationAmount)(effectiveSessionId, priceId, amount, session, currencyId, refresh);
123
+ } catch (err) {
124
+ console.error("Failed to change amount:", (0, _checkoutAugmented.getErrorMessage)(err));
125
+ }
126
+ });
127
+ const canSubmit = submitHook.status === "idle" && !isLoading && !error && session?.status === "open";
128
+ const isCompleted = submitHook.status === "completed";
129
+ return {
130
+ isLoading,
131
+ error,
132
+ errorCode,
133
+ refresh: async () => {
134
+ await refresh();
135
+ },
136
+ product,
137
+ vendorCount,
138
+ lineItems: {
139
+ items: items.map(item => ({
140
+ ...item,
141
+ adjustable_quantity: item.adjustable_quantity
142
+ })),
143
+ updateQuantity,
144
+ upsell,
145
+ downsell,
146
+ billingInterval,
147
+ crossSellItem,
148
+ crossSellRequired,
149
+ addCrossSell,
150
+ removeCrossSell
151
+ },
152
+ paymentMethod: {
153
+ current: paymentMethodHook.current,
154
+ currency: paymentMethodHook.currency,
155
+ available: paymentMethodHook.available,
156
+ currencies: paymentMethodHook.currencies,
157
+ isStripe: paymentMethodHook.isStripe,
158
+ isCrypto: paymentMethodHook.isCrypto,
159
+ isCredit: paymentMethodHook.isCredit,
160
+ setType: paymentMethodHook.setType,
161
+ types: paymentMethodHook.types,
162
+ setCurrency: paymentMethodHook.setCurrency
163
+ },
164
+ stripe: paymentMethodHook.stripe,
165
+ pricing: pricingHook,
166
+ form: formHook,
167
+ submit: submitHook,
168
+ subscription,
169
+ pageInfo,
170
+ canSubmit,
171
+ isCompleted,
172
+ isDonation,
173
+ setDonationAmount,
174
+ customer: sessionData?.customer ? {
175
+ name: sessionData.customer.name || "",
176
+ email: sessionData.customer.email || "",
177
+ phone: sessionData.customer.phone || "",
178
+ address: {
179
+ country: sessionData.customer.address?.country || "",
180
+ state: sessionData.customer.address?.state || "",
181
+ city: sessionData.customer.address?.city || "",
182
+ line1: sessionData.customer.address?.line1 || "",
183
+ line2: sessionData.customer.address?.line2 || "",
184
+ postal_code: sessionData.customer.address?.postal_code || ""
185
+ }
186
+ } : null,
187
+ livemode: !!session?.livemode,
188
+ session: session || null
189
+ };
190
+ }
@@ -0,0 +1,58 @@
1
+ import type { TCheckoutSessionExpanded, TPaymentMethodExpanded, TPaymentIntent, TCustomer } from '@blocklet/payment-types';
2
+ export interface SessionData {
3
+ checkoutSession: TCheckoutSessionExpanded;
4
+ paymentMethods: TPaymentMethodExpanded[];
5
+ paymentIntent?: TPaymentIntent;
6
+ customer?: TCustomer;
7
+ quotes?: Record<string, {
8
+ quote_id: string;
9
+ expires_at: number;
10
+ quoted_amount: string;
11
+ exchange_rate?: string;
12
+ rate_provider_name?: string;
13
+ rate_provider_id?: string;
14
+ }>;
15
+ rateUnavailable?: boolean;
16
+ rateError?: string;
17
+ }
18
+ export interface UseCheckoutSessionReturn {
19
+ isLoading: boolean;
20
+ error: string | null;
21
+ /** Error code for structured error handling: 'SESSION_EXPIRED' | 'EMPTY_LINE_ITEMS' | null */
22
+ errorCode: 'SESSION_EXPIRED' | 'EMPTY_LINE_ITEMS' | null;
23
+ refresh: (forceRefresh?: boolean) => Promise<void>;
24
+ /** Directly update session data (e.g. after completion polling returns fresh data) */
25
+ setSessionData: (data: SessionData) => void;
26
+ sessionData: SessionData | null;
27
+ resolvedSessionId: string;
28
+ vendorCount: number;
29
+ product: {
30
+ name: string;
31
+ description: string;
32
+ images: string[];
33
+ features: Array<{
34
+ name: string;
35
+ icon?: string;
36
+ }>;
37
+ billing: {
38
+ mode: 'payment' | 'subscription';
39
+ interval: 'month' | 'year' | 'week' | 'day' | null;
40
+ intervalCount: number;
41
+ displayInterval: string;
42
+ };
43
+ metadata: Record<string, string>;
44
+ } | null;
45
+ subscription: {
46
+ mode: 'payment' | 'subscription' | 'setup';
47
+ showStake: boolean;
48
+ confirmMessage: string;
49
+ } | null;
50
+ pageInfo: {
51
+ formPurposeDescription?: {
52
+ en: string;
53
+ zh: string;
54
+ };
55
+ showProductFeatures: boolean;
56
+ } | null;
57
+ }
58
+ export declare function useCheckoutSession(sessionId: string): UseCheckoutSessionReturn;
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useCheckoutSession = useCheckoutSession;
7
+ var _react = require("react");
8
+ var _ahooks = require("ahooks");
9
+ var _api = _interopRequireWildcard(require("../../shared/api"));
10
+ var _checkoutAugmented = require("../../types/checkout-augmented");
11
+ var _session = require("../core/session");
12
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
13
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
+ function useCheckoutSession(sessionId) {
15
+ const [isLoading, setIsLoading] = (0, _react.useState)(true);
16
+ const [error, setError] = (0, _react.useState)(null);
17
+ const [sessionData, setSessionData] = (0, _react.useState)(null);
18
+ const abortRef = (0, _react.useRef)(null);
19
+ const mountedRef = (0, _react.useRef)(true);
20
+ const resolvedIdRef = (0, _react.useRef)(null);
21
+ const isPaymentLink = sessionId?.startsWith("plink_");
22
+ const resolveSessionId = (0, _ahooks.useMemoizedFn)(async () => {
23
+ if (!isPaymentLink) return sessionId;
24
+ if (resolvedIdRef.current) return resolvedIdRef.current;
25
+ const {
26
+ data
27
+ } = await _api.default.post(_api.API.START_PAYMENT_LINK(sessionId));
28
+ const resolved = data?.checkoutSession?.id;
29
+ if (!resolved) throw new Error("Failed to start payment link session");
30
+ resolvedIdRef.current = resolved;
31
+ return resolved;
32
+ });
33
+ const fetchSession = (0, _ahooks.useMemoizedFn)(async (forceRefresh = false) => {
34
+ if (abortRef.current) {
35
+ abortRef.current.abort();
36
+ }
37
+ abortRef.current = new AbortController();
38
+ if (!sessionId) {
39
+ setIsLoading(false);
40
+ setError("Session ID is required");
41
+ return;
42
+ }
43
+ try {
44
+ if (!forceRefresh) {
45
+ setIsLoading(true);
46
+ }
47
+ setError(null);
48
+ const realId = await resolveSessionId();
49
+ const url = forceRefresh ? `${_api.API.RETRIEVE_SESSION(realId)}?forceRefresh=1` : _api.API.RETRIEVE_SESSION(realId);
50
+ const {
51
+ data
52
+ } = await _api.default.get(url, {
53
+ signal: abortRef.current.signal
54
+ });
55
+ if (!mountedRef.current) return;
56
+ const cs = data?.checkoutSession;
57
+ if (cs) {
58
+ if (cs.status !== "complete" && cs.expires_at && cs.expires_at <= Math.round(Date.now() / 1e3)) {
59
+ setError("SESSION_EXPIRED");
60
+ setIsLoading(false);
61
+ return;
62
+ }
63
+ if (!cs.line_items?.length) {
64
+ setError("EMPTY_LINE_ITEMS");
65
+ setIsLoading(false);
66
+ return;
67
+ }
68
+ }
69
+ setSessionData(data);
70
+ setIsLoading(false);
71
+ } catch (err) {
72
+ if (!mountedRef.current) return;
73
+ const {
74
+ isCancelled,
75
+ message
76
+ } = (0, _checkoutAugmented.getAxiosErrorDetails)(err);
77
+ if (isCancelled) return;
78
+ setError(message || "Failed to load checkout session");
79
+ setIsLoading(false);
80
+ }
81
+ });
82
+ const refresh = (0, _react.useCallback)(async (forceRefresh = false) => {
83
+ await fetchSession(forceRefresh);
84
+ }, [fetchSession]);
85
+ (0, _react.useEffect)(() => {
86
+ mountedRef.current = true;
87
+ fetchSession();
88
+ return () => {
89
+ mountedRef.current = false;
90
+ if (abortRef.current) {
91
+ abortRef.current.abort();
92
+ }
93
+ };
94
+ }, [sessionId]);
95
+ const session = sessionData?.checkoutSession;
96
+ const resolvedSessionId = resolvedIdRef.current || (isPaymentLink ? "" : sessionId);
97
+ let errorCode = null;
98
+ if (error === "SESSION_EXPIRED") {
99
+ errorCode = "SESSION_EXPIRED";
100
+ } else if (error === "EMPTY_LINE_ITEMS") {
101
+ errorCode = "EMPTY_LINE_ITEMS";
102
+ }
103
+ const vendorCount = session?.line_items?.reduce((count, item) => {
104
+ return count + (item?.price?.product?.vendor_config?.length || 0);
105
+ }, 0) || 0;
106
+ return {
107
+ isLoading,
108
+ error,
109
+ errorCode,
110
+ refresh,
111
+ setSessionData,
112
+ sessionData,
113
+ resolvedSessionId,
114
+ vendorCount,
115
+ product: session ? (0, _session.parseProduct)(session) : null,
116
+ subscription: session ? (0, _session.parseSubscription)(session) : null,
117
+ pageInfo: session ? (0, _session.parsePageInfo)(session) : null
118
+ };
119
+ }
@@ -0,0 +1,10 @@
1
+ export interface UseCheckoutStatusReturn {
2
+ isLoading: boolean;
3
+ error: string | null;
4
+ errorCode: 'SESSION_EXPIRED' | 'EMPTY_LINE_ITEMS' | null;
5
+ canSubmit: boolean;
6
+ isCompleted: boolean;
7
+ isDonation: boolean;
8
+ livemode: boolean;
9
+ }
10
+ export declare function useCheckoutStatus(): UseCheckoutStatusReturn;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useCheckoutStatus = useCheckoutStatus;
7
+ var _SessionContext = require("../context/SessionContext");
8
+ function useCheckoutStatus() {
9
+ const {
10
+ isLoading,
11
+ error,
12
+ errorCode,
13
+ session,
14
+ isDonation
15
+ } = (0, _SessionContext.useSessionContext)();
16
+ const canSubmit = !isLoading && !error && session?.status === "open";
17
+ const isCompleted = session?.status === "complete";
18
+ const livemode = !!session?.livemode;
19
+ return {
20
+ isLoading,
21
+ error,
22
+ errorCode,
23
+ canSubmit,
24
+ isCompleted,
25
+ isDonation,
26
+ livemode
27
+ };
28
+ }
@@ -0,0 +1,8 @@
1
+ import type { TPrice } from '@blocklet/payment-types';
2
+ export interface UseCrossSellReturn {
3
+ item: TPrice | null;
4
+ required: boolean;
5
+ add: () => Promise<void>;
6
+ remove: () => Promise<void>;
7
+ }
8
+ export declare function useCrossSell(): UseCrossSellReturn;