@blocklet/payment-react
Version:
Reusable react components for payment kit v2
103 lines (102 loc) • 3.05 kB
TypeScript
/**
* useDynamicPricing Hook
*
* Extracts and centralizes all dynamic pricing related calculations
* for the checkout summary component.
*
* Final Freeze Architecture: Quotes are created at Submit time only.
* This hook handles preview-time display calculations.
*/
import type { TCheckoutSession, TLineItemExpanded, TPaymentCurrency, TPaymentIntent } from '@blocklet/payment-types';
export interface LiveRateInfo {
rate?: string;
provider_id?: string;
provider_name?: string;
provider_display?: string;
base_currency?: string;
timestamp_ms?: number;
fetched_at?: number;
}
export interface LiveQuoteSnapshot {
id: string;
quoted_amount: string;
exchange_rate: string;
expires_at: number;
rate_timestamp_ms?: number | null;
renewed?: boolean;
}
export interface DiscountInfo {
promotion_code?: string;
coupon?: string;
discount_amount?: string;
coupon_details?: {
percent_off?: number;
amount_off?: string;
currency_id?: string;
currency_options?: Record<string, {
amount_off?: string;
}>;
};
}
export interface DynamicPricingOptions {
items: TLineItemExpanded[];
currency: TPaymentCurrency;
liveRate?: LiveRateInfo;
liveQuoteSnapshot?: LiveQuoteSnapshot;
checkoutSession?: TCheckoutSession;
paymentIntent?: TPaymentIntent | null;
locale?: string;
isStripePayment?: boolean;
isSubscription?: boolean;
slippageConfig?: {
mode?: 'percent' | 'rate';
percent?: number | null;
min_acceptable_rate?: string;
base_currency?: string;
};
trialInDays?: number;
trialEnd?: number;
discounts?: DiscountInfo[];
}
export interface RateInfo {
exchangeRate: string | null;
baseCurrency: string;
providerName: string | null;
providerId: string | null;
timestampMs: number | null;
fetchedAt: number | null;
}
export interface QuoteMeta {
exchangeRate: string | null;
baseCurrency: string;
expiresAt: number | null;
providerName: string | null;
providerId: string | null;
rateTimestampMs: number | null;
slippagePercent: number | null;
}
/**
* Custom hook for dynamic pricing calculations
*/
export declare function useDynamicPricing(options: DynamicPricingOptions): {
hasDynamicPricing: boolean;
isPriceLocked: boolean;
lockExpired: boolean;
quoteMeta: QuoteMeta | null;
rateInfo: RateInfo;
quoteLockedAt: number | null;
calculatedTokenAmount: string | null;
calculatedDiscountAmount: any;
currentSlippagePercent: number;
rateDisplay: string | null;
providerDisplay: string;
formatTotalDisplay: (totalAmountValue: string, fallback?: string) => string;
calculateUsdDisplay: (totalAmountValue: string) => string | null;
buildQuoteDetailRows: (t: (key: string) => string) => {
label: string;
value: string | React.ReactNode;
isSlippage?: boolean;
tooltip?: string;
}[];
};
export default useDynamicPricing;