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