@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,15 @@
1
+ import type { TLineItemExpanded } from '@blocklet/payment-types';
2
+ export type BillingIntervalType = 'month' | 'year' | 'week' | 'day';
3
+ export interface BillingIntervalOption {
4
+ interval: BillingIntervalType;
5
+ priceId: string;
6
+ amount: string;
7
+ savings: string | null;
8
+ }
9
+ export interface BillingIntervalInfo {
10
+ current: BillingIntervalType | null;
11
+ available: BillingIntervalOption[];
12
+ /** The first item used to derive billing info */
13
+ firstItem: TLineItemExpanded;
14
+ }
15
+ export declare function parseBillingInterval(items: TLineItemExpanded[]): BillingIntervalInfo | null;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parseBillingInterval = parseBillingInterval;
7
+ function parseBillingInterval(items) {
8
+ if (!items.length) return null;
9
+ const firstItem = items[0];
10
+ const price = firstItem.upsell_price || firstItem.price;
11
+ const recurring = price?.recurring;
12
+ if (!recurring) return null;
13
+ const currentInterval = recurring.interval;
14
+ const upsell = price?.upsell;
15
+ const upsellTo = upsell?.upsells_to;
16
+ const available = [];
17
+ if (upsellTo?.recurring?.interval && upsellTo.recurring.interval !== currentInterval) {
18
+ available.push({
19
+ interval: upsellTo.recurring.interval,
20
+ priceId: upsell?.upsells_to_id || "",
21
+ amount: upsellTo.unit_amount || "0",
22
+ savings: null
23
+ });
24
+ }
25
+ if (firstItem.upsell_price_id && firstItem.price?.recurring) {
26
+ const origInterval = firstItem.price.recurring.interval;
27
+ if (origInterval && origInterval !== currentInterval) {
28
+ available.push({
29
+ interval: origInterval,
30
+ priceId: firstItem.price_id,
31
+ amount: firstItem.price.unit_amount || "0",
32
+ savings: null
33
+ });
34
+ }
35
+ }
36
+ if (available.length === 0) return null;
37
+ return {
38
+ current: currentInterval,
39
+ available,
40
+ firstItem
41
+ };
42
+ }
@@ -0,0 +1,4 @@
1
+ import type { TCheckoutSessionExpanded, TPrice } from '@blocklet/payment-types';
2
+ export declare function addCrossSellItem(sessionId: string, crossSellItemId: string, session: TCheckoutSessionExpanded | undefined | null, currencyId: string | null | undefined, refresh: (force?: boolean) => Promise<void>): Promise<void>;
3
+ export declare function removeCrossSellItem(sessionId: string, session: TCheckoutSessionExpanded | undefined | null, currencyId: string | null | undefined, refresh: (force?: boolean) => Promise<void>): Promise<void>;
4
+ export declare function fetchCrossSellItem(sessionId: string): Promise<TPrice | null>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addCrossSellItem = addCrossSellItem;
7
+ exports.fetchCrossSellItem = fetchCrossSellItem;
8
+ exports.removeCrossSellItem = removeCrossSellItem;
9
+ var _api = _interopRequireWildcard(require("../../shared/api"));
10
+ var _lineItems = require("./lineItems");
11
+ 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); }
12
+ 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; }
13
+ async function addCrossSellItem(sessionId, crossSellItemId, session, currencyId, refresh) {
14
+ await _api.default.put(_api.API.CROSS_SELL(sessionId), {
15
+ to: crossSellItemId
16
+ });
17
+ await (0, _lineItems.recalculatePromotionIfNeeded)(session, sessionId, currencyId);
18
+ await refresh(true);
19
+ }
20
+ async function removeCrossSellItem(sessionId, session, currencyId, refresh) {
21
+ await _api.default.delete(_api.API.CROSS_SELL(sessionId));
22
+ await (0, _lineItems.recalculatePromotionIfNeeded)(session, sessionId, currencyId);
23
+ await refresh(true);
24
+ }
25
+ const pendingFetches = /* @__PURE__ */new Map();
26
+ function fetchCrossSellItem(sessionId) {
27
+ const existing = pendingFetches.get(sessionId);
28
+ if (existing) return existing;
29
+ const promise = (async () => {
30
+ try {
31
+ const {
32
+ data
33
+ } = await _api.default.get(`${_api.API.CROSS_SELL(sessionId)}?skipError=true`);
34
+ if (data && !data.error) {
35
+ return data;
36
+ }
37
+ } catch {}
38
+ return null;
39
+ })();
40
+ pendingFetches.set(sessionId, promise);
41
+ promise.finally(() => pendingFetches.delete(sessionId));
42
+ return promise;
43
+ }
@@ -0,0 +1,5 @@
1
+ import type { TCheckoutSessionExpanded, TCustomer } from '@blocklet/payment-types';
2
+ import type { FieldConfig, CheckoutFormData } from '../types';
3
+ export declare function buildFields(session: TCheckoutSessionExpanded | undefined | null): FieldConfig[];
4
+ export declare function createInitialValues(session: TCheckoutSessionExpanded | undefined | null, customer: TCustomer | undefined | null): CheckoutFormData;
5
+ export declare function setNestedValue<T extends Record<string, unknown>>(obj: T, path: string, value: unknown): T;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildFields = buildFields;
7
+ exports.createInitialValues = createInitialValues;
8
+ exports.setNestedValue = setNestedValue;
9
+ function buildFields(session) {
10
+ const fields = [];
11
+ fields.push({
12
+ name: "customer_name",
13
+ type: "text",
14
+ required: true
15
+ });
16
+ fields.push({
17
+ name: "customer_email",
18
+ type: "email",
19
+ required: true
20
+ });
21
+ if (session?.phone_number_collection?.enabled) {
22
+ fields.push({
23
+ name: "customer_phone",
24
+ type: "phone",
25
+ required: true
26
+ });
27
+ }
28
+ const addressMode = session?.billing_address_collection;
29
+ if (addressMode === "required") {
30
+ fields.push({
31
+ name: "billing_address.country",
32
+ type: "country",
33
+ required: true,
34
+ group: "address"
35
+ }, {
36
+ name: "billing_address.line1",
37
+ type: "text",
38
+ required: true,
39
+ group: "address"
40
+ }, {
41
+ name: "billing_address.line2",
42
+ type: "text",
43
+ required: false,
44
+ group: "address"
45
+ }, {
46
+ name: "billing_address.city",
47
+ type: "text",
48
+ required: true,
49
+ group: "address"
50
+ }, {
51
+ name: "billing_address.state",
52
+ type: "text",
53
+ required: true,
54
+ group: "address"
55
+ }, {
56
+ name: "billing_address.postal_code",
57
+ type: "text",
58
+ required: true,
59
+ group: "address"
60
+ });
61
+ } else if (addressMode === "auto") {
62
+ fields.push({
63
+ name: "billing_address.state",
64
+ type: "text",
65
+ required: false,
66
+ group: "address"
67
+ }, {
68
+ name: "billing_address.postal_code",
69
+ type: "text",
70
+ required: false,
71
+ group: "address"
72
+ });
73
+ }
74
+ return fields;
75
+ }
76
+ function createInitialValues(session, customer) {
77
+ return {
78
+ customer_name: customer?.name || "",
79
+ customer_email: session?.customer_details?.email || customer?.email || "",
80
+ customer_phone: customer?.phone || "",
81
+ payment_method: "",
82
+ payment_currency: session?.currency_id || "",
83
+ billing_address: {
84
+ country: customer?.address?.country || "",
85
+ state: customer?.address?.state || "",
86
+ city: customer?.address?.city || "",
87
+ line1: customer?.address?.line1 || "",
88
+ line2: customer?.address?.line2 || "",
89
+ postal_code: customer?.address?.postal_code || ""
90
+ }
91
+ };
92
+ }
93
+ function setNestedValue(obj, path, value) {
94
+ const parts = path.split(".");
95
+ if (parts.length === 1) {
96
+ return {
97
+ ...obj,
98
+ [path]: value
99
+ };
100
+ }
101
+ const [head, ...rest] = parts;
102
+ return {
103
+ ...obj,
104
+ [head]: setNestedValue(obj[head] || {}, rest.join("."), value)
105
+ };
106
+ }
@@ -0,0 +1,11 @@
1
+ import type { TLineItemExpanded } from '@blocklet/payment-types';
2
+ export declare const BASE_INTERVAL = 30000;
3
+ export declare const MAX_INTERVAL = 120000;
4
+ export declare function checkHasDynamicPricing(items: TLineItemExpanded[]): boolean;
5
+ export declare function fetchExchangeRate(sessionId: string, currencyId: string | null | undefined): Promise<{
6
+ rate: string | null;
7
+ provider: string | null;
8
+ providerDisplay: string | null;
9
+ fetchedAt: number | null;
10
+ }>;
11
+ export declare function getBackoffInterval(failCount: number): number;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MAX_INTERVAL = exports.BASE_INTERVAL = void 0;
7
+ exports.checkHasDynamicPricing = checkHasDynamicPricing;
8
+ exports.fetchExchangeRate = fetchExchangeRate;
9
+ exports.getBackoffInterval = getBackoffInterval;
10
+ var _api = _interopRequireWildcard(require("../../shared/api"));
11
+ 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); }
12
+ 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; }
13
+ const BASE_INTERVAL = exports.BASE_INTERVAL = 3e4;
14
+ const MAX_INTERVAL = exports.MAX_INTERVAL = 12e4;
15
+ function checkHasDynamicPricing(items) {
16
+ return items.some(item => (item.upsell_price || item.price)?.pricing_type === "dynamic");
17
+ }
18
+ async function fetchExchangeRate(sessionId, currencyId) {
19
+ const {
20
+ data
21
+ } = await _api.default.get(_api.API.EXCHANGE_RATE(sessionId), {
22
+ params: currencyId ? {
23
+ currency_id: currencyId
24
+ } : void 0
25
+ });
26
+ const rateValue = data.rate || data.exchange_rate;
27
+ if (rateValue) {
28
+ const providerDisplay = data.provider_display || data.provider_name || data.provider_id || null;
29
+ return {
30
+ rate: rateValue,
31
+ provider: data.provider_name || data.rate_provider_name || null,
32
+ providerDisplay,
33
+ fetchedAt: data.fetched_at || data.timestamp_ms || null
34
+ };
35
+ }
36
+ return {
37
+ rate: null,
38
+ provider: null,
39
+ providerDisplay: null,
40
+ fetchedAt: null
41
+ };
42
+ }
43
+ function getBackoffInterval(failCount) {
44
+ return Math.min(BASE_INTERVAL * 2 ** failCount, MAX_INTERVAL);
45
+ }
@@ -0,0 +1,10 @@
1
+ export { parseProduct, parseSubscription, parsePageInfo } from './session';
2
+ export { flattenCurrencies, hasDidWallet, getInitialCurrencyId, findMethodAndCurrency, buildPaymentTypes, } from './paymentMethod';
3
+ export { BASE_INTERVAL, MAX_INTERVAL, checkHasDynamicPricing, fetchExchangeRate, getBackoffInterval, } from './exchangeRate';
4
+ export { calculateAmounts, calculateQuoteMeta, calculateTrial, type CalculatedAmounts, type QuoteMeta, } from './pricing';
5
+ export { extractPromotionCodeFromSession, applyPromotionCode, removePromotionCode, recalculatePromotion, isPromotionActive, hasAppliedDiscounts, } from './promotion';
6
+ export { recalculatePromotionIfNeeded, adjustQuantity, performUpsell, performDownsell, changeDonationAmount, getCrossSellItem, } from './lineItems';
7
+ export { addCrossSellItem, removeCrossSellItem, fetchCrossSellItem } from './crossSell';
8
+ export { parseBillingInterval, type BillingIntervalType, type BillingIntervalOption, type BillingIntervalInfo, } from './billingInterval';
9
+ export { buildFields, createInitialValues, setNestedValue } from './customerForm';
10
+ export { QUOTE_ERROR_CODES, RELAY_SOCKET_PREFIX, getAppId, getRelayChannel, getRelayProtocol, getSocketHost, buildSubmitPayload, isQuoteError, abortStripePayment, submitCheckout, confirmFastCheckout, updateSlippage, } from './submit';
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "BASE_INTERVAL", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _exchangeRate.BASE_INTERVAL;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "MAX_INTERVAL", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _exchangeRate.MAX_INTERVAL;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "QUOTE_ERROR_CODES", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _submit.QUOTE_ERROR_CODES;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "RELAY_SOCKET_PREFIX", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _submit.RELAY_SOCKET_PREFIX;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "abortStripePayment", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _submit.abortStripePayment;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "addCrossSellItem", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _crossSell.addCrossSellItem;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "adjustQuantity", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _lineItems.adjustQuantity;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "applyPromotionCode", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _promotion.applyPromotionCode;
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "buildFields", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _customerForm.buildFields;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "buildPaymentTypes", {
61
+ enumerable: true,
62
+ get: function () {
63
+ return _paymentMethod.buildPaymentTypes;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "buildSubmitPayload", {
67
+ enumerable: true,
68
+ get: function () {
69
+ return _submit.buildSubmitPayload;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "calculateAmounts", {
73
+ enumerable: true,
74
+ get: function () {
75
+ return _pricing.calculateAmounts;
76
+ }
77
+ });
78
+ Object.defineProperty(exports, "calculateQuoteMeta", {
79
+ enumerable: true,
80
+ get: function () {
81
+ return _pricing.calculateQuoteMeta;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "calculateTrial", {
85
+ enumerable: true,
86
+ get: function () {
87
+ return _pricing.calculateTrial;
88
+ }
89
+ });
90
+ Object.defineProperty(exports, "changeDonationAmount", {
91
+ enumerable: true,
92
+ get: function () {
93
+ return _lineItems.changeDonationAmount;
94
+ }
95
+ });
96
+ Object.defineProperty(exports, "checkHasDynamicPricing", {
97
+ enumerable: true,
98
+ get: function () {
99
+ return _exchangeRate.checkHasDynamicPricing;
100
+ }
101
+ });
102
+ Object.defineProperty(exports, "confirmFastCheckout", {
103
+ enumerable: true,
104
+ get: function () {
105
+ return _submit.confirmFastCheckout;
106
+ }
107
+ });
108
+ Object.defineProperty(exports, "createInitialValues", {
109
+ enumerable: true,
110
+ get: function () {
111
+ return _customerForm.createInitialValues;
112
+ }
113
+ });
114
+ Object.defineProperty(exports, "extractPromotionCodeFromSession", {
115
+ enumerable: true,
116
+ get: function () {
117
+ return _promotion.extractPromotionCodeFromSession;
118
+ }
119
+ });
120
+ Object.defineProperty(exports, "fetchCrossSellItem", {
121
+ enumerable: true,
122
+ get: function () {
123
+ return _crossSell.fetchCrossSellItem;
124
+ }
125
+ });
126
+ Object.defineProperty(exports, "fetchExchangeRate", {
127
+ enumerable: true,
128
+ get: function () {
129
+ return _exchangeRate.fetchExchangeRate;
130
+ }
131
+ });
132
+ Object.defineProperty(exports, "findMethodAndCurrency", {
133
+ enumerable: true,
134
+ get: function () {
135
+ return _paymentMethod.findMethodAndCurrency;
136
+ }
137
+ });
138
+ Object.defineProperty(exports, "flattenCurrencies", {
139
+ enumerable: true,
140
+ get: function () {
141
+ return _paymentMethod.flattenCurrencies;
142
+ }
143
+ });
144
+ Object.defineProperty(exports, "getAppId", {
145
+ enumerable: true,
146
+ get: function () {
147
+ return _submit.getAppId;
148
+ }
149
+ });
150
+ Object.defineProperty(exports, "getBackoffInterval", {
151
+ enumerable: true,
152
+ get: function () {
153
+ return _exchangeRate.getBackoffInterval;
154
+ }
155
+ });
156
+ Object.defineProperty(exports, "getCrossSellItem", {
157
+ enumerable: true,
158
+ get: function () {
159
+ return _lineItems.getCrossSellItem;
160
+ }
161
+ });
162
+ Object.defineProperty(exports, "getInitialCurrencyId", {
163
+ enumerable: true,
164
+ get: function () {
165
+ return _paymentMethod.getInitialCurrencyId;
166
+ }
167
+ });
168
+ Object.defineProperty(exports, "getRelayChannel", {
169
+ enumerable: true,
170
+ get: function () {
171
+ return _submit.getRelayChannel;
172
+ }
173
+ });
174
+ Object.defineProperty(exports, "getRelayProtocol", {
175
+ enumerable: true,
176
+ get: function () {
177
+ return _submit.getRelayProtocol;
178
+ }
179
+ });
180
+ Object.defineProperty(exports, "getSocketHost", {
181
+ enumerable: true,
182
+ get: function () {
183
+ return _submit.getSocketHost;
184
+ }
185
+ });
186
+ Object.defineProperty(exports, "hasAppliedDiscounts", {
187
+ enumerable: true,
188
+ get: function () {
189
+ return _promotion.hasAppliedDiscounts;
190
+ }
191
+ });
192
+ Object.defineProperty(exports, "hasDidWallet", {
193
+ enumerable: true,
194
+ get: function () {
195
+ return _paymentMethod.hasDidWallet;
196
+ }
197
+ });
198
+ Object.defineProperty(exports, "isPromotionActive", {
199
+ enumerable: true,
200
+ get: function () {
201
+ return _promotion.isPromotionActive;
202
+ }
203
+ });
204
+ Object.defineProperty(exports, "isQuoteError", {
205
+ enumerable: true,
206
+ get: function () {
207
+ return _submit.isQuoteError;
208
+ }
209
+ });
210
+ Object.defineProperty(exports, "parseBillingInterval", {
211
+ enumerable: true,
212
+ get: function () {
213
+ return _billingInterval.parseBillingInterval;
214
+ }
215
+ });
216
+ Object.defineProperty(exports, "parsePageInfo", {
217
+ enumerable: true,
218
+ get: function () {
219
+ return _session.parsePageInfo;
220
+ }
221
+ });
222
+ Object.defineProperty(exports, "parseProduct", {
223
+ enumerable: true,
224
+ get: function () {
225
+ return _session.parseProduct;
226
+ }
227
+ });
228
+ Object.defineProperty(exports, "parseSubscription", {
229
+ enumerable: true,
230
+ get: function () {
231
+ return _session.parseSubscription;
232
+ }
233
+ });
234
+ Object.defineProperty(exports, "performDownsell", {
235
+ enumerable: true,
236
+ get: function () {
237
+ return _lineItems.performDownsell;
238
+ }
239
+ });
240
+ Object.defineProperty(exports, "performUpsell", {
241
+ enumerable: true,
242
+ get: function () {
243
+ return _lineItems.performUpsell;
244
+ }
245
+ });
246
+ Object.defineProperty(exports, "recalculatePromotion", {
247
+ enumerable: true,
248
+ get: function () {
249
+ return _promotion.recalculatePromotion;
250
+ }
251
+ });
252
+ Object.defineProperty(exports, "recalculatePromotionIfNeeded", {
253
+ enumerable: true,
254
+ get: function () {
255
+ return _lineItems.recalculatePromotionIfNeeded;
256
+ }
257
+ });
258
+ Object.defineProperty(exports, "removeCrossSellItem", {
259
+ enumerable: true,
260
+ get: function () {
261
+ return _crossSell.removeCrossSellItem;
262
+ }
263
+ });
264
+ Object.defineProperty(exports, "removePromotionCode", {
265
+ enumerable: true,
266
+ get: function () {
267
+ return _promotion.removePromotionCode;
268
+ }
269
+ });
270
+ Object.defineProperty(exports, "setNestedValue", {
271
+ enumerable: true,
272
+ get: function () {
273
+ return _customerForm.setNestedValue;
274
+ }
275
+ });
276
+ Object.defineProperty(exports, "submitCheckout", {
277
+ enumerable: true,
278
+ get: function () {
279
+ return _submit.submitCheckout;
280
+ }
281
+ });
282
+ Object.defineProperty(exports, "updateSlippage", {
283
+ enumerable: true,
284
+ get: function () {
285
+ return _submit.updateSlippage;
286
+ }
287
+ });
288
+ var _session = require("./session");
289
+ var _paymentMethod = require("./paymentMethod");
290
+ var _exchangeRate = require("./exchangeRate");
291
+ var _pricing = require("./pricing");
292
+ var _promotion = require("./promotion");
293
+ var _lineItems = require("./lineItems");
294
+ var _crossSell = require("./crossSell");
295
+ var _billingInterval = require("./billingInterval");
296
+ var _customerForm = require("./customerForm");
297
+ var _submit = require("./submit");
@@ -0,0 +1,7 @@
1
+ import type { TCheckoutSessionExpanded, TLineItemExpanded, TPrice } from '@blocklet/payment-types';
2
+ export declare function recalculatePromotionIfNeeded(session: TCheckoutSessionExpanded | undefined | null, sessionId: string, currencyId: string | null | undefined): Promise<void>;
3
+ export declare function adjustQuantity(sessionId: string, itemId: string, qty: number, currencyId: string | null | undefined, session: TCheckoutSessionExpanded | undefined | null, refresh: (force?: boolean) => Promise<void>): Promise<void>;
4
+ export declare function performUpsell(sessionId: string, fromId: string, toId: string, session: TCheckoutSessionExpanded | undefined | null, currencyId: string | null | undefined, refresh: (force?: boolean) => Promise<void>): Promise<void>;
5
+ export declare function performDownsell(sessionId: string, priceId: string, session: TCheckoutSessionExpanded | undefined | null, currencyId: string | null | undefined, refresh: (force?: boolean) => Promise<void>): Promise<void>;
6
+ export declare function changeDonationAmount(sessionId: string, priceId: string, amount: string, session: TCheckoutSessionExpanded | undefined | null, currencyId: string | null | undefined, refresh: (force?: boolean) => Promise<void>): Promise<void>;
7
+ export declare function getCrossSellItem(items: TLineItemExpanded[]): TPrice | null;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.adjustQuantity = adjustQuantity;
7
+ exports.changeDonationAmount = changeDonationAmount;
8
+ exports.getCrossSellItem = getCrossSellItem;
9
+ exports.performDownsell = performDownsell;
10
+ exports.performUpsell = performUpsell;
11
+ exports.recalculatePromotionIfNeeded = recalculatePromotionIfNeeded;
12
+ var _api = _interopRequireWildcard(require("../../shared/api"));
13
+ var _promotion = require("./promotion");
14
+ 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); }
15
+ 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; }
16
+ async function recalculatePromotionIfNeeded(session, sessionId, currencyId) {
17
+ if (!(0, _promotion.hasAppliedDiscounts)(session)) return;
18
+ try {
19
+ await (0, _promotion.recalculatePromotion)(sessionId, currencyId);
20
+ } catch {}
21
+ }
22
+ async function adjustQuantity(sessionId, itemId, qty, currencyId, session, refresh) {
23
+ await _api.default.put(_api.API.ADJUST_QUANTITY(sessionId), {
24
+ itemId,
25
+ quantity: qty,
26
+ currency_id: currencyId
27
+ });
28
+ await recalculatePromotionIfNeeded(session, sessionId, currencyId);
29
+ await refresh(true);
30
+ }
31
+ async function performUpsell(sessionId, fromId, toId, session, currencyId, refresh) {
32
+ if ((session?.line_items?.length || 0) > 1) {
33
+ try {
34
+ await _api.default.delete(_api.API.CROSS_SELL(sessionId));
35
+ } catch {}
36
+ }
37
+ await _api.default.put(_api.API.UPSELL(sessionId), {
38
+ from: fromId,
39
+ to: toId
40
+ });
41
+ await recalculatePromotionIfNeeded(session, sessionId, currencyId);
42
+ await refresh(true);
43
+ }
44
+ async function performDownsell(sessionId, priceId, session, currencyId, refresh) {
45
+ if ((session?.line_items?.length || 0) > 1) {
46
+ try {
47
+ await _api.default.delete(_api.API.CROSS_SELL(sessionId));
48
+ } catch {}
49
+ }
50
+ await _api.default.put(_api.API.DOWNSELL(sessionId), {
51
+ from: priceId
52
+ });
53
+ await recalculatePromotionIfNeeded(session, sessionId, currencyId);
54
+ await refresh(true);
55
+ }
56
+ async function changeDonationAmount(sessionId, priceId, amount, session, currencyId, refresh) {
57
+ const {
58
+ data
59
+ } = await _api.default.put(_api.API.CHANGE_AMOUNT(sessionId), {
60
+ priceId,
61
+ amount
62
+ });
63
+ if (data?.discounts?.length) {
64
+ await recalculatePromotionIfNeeded(session, sessionId, currencyId);
65
+ }
66
+ await refresh(true);
67
+ }
68
+ function getCrossSellItem(items) {
69
+ for (const item of items) {
70
+ const crossSell = item.price?.cross_sell;
71
+ if (crossSell?.cross_sells_to) {
72
+ return crossSell.cross_sells_to;
73
+ }
74
+ }
75
+ return null;
76
+ }