@wix/headless-bookings 0.0.30 → 0.0.32
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/cjs/dist/react/core/payment/Payment.d.ts +193 -0
- package/cjs/dist/react/core/payment/Payment.js +202 -0
- package/cjs/dist/react/index.d.ts +2 -0
- package/cjs/dist/react/index.js +2 -0
- package/cjs/dist/react/payment/Payment.d.ts +342 -0
- package/cjs/dist/react/payment/Payment.js +294 -0
- package/cjs/dist/react/payment/PaymentLineItem.d.ts +144 -0
- package/cjs/dist/react/payment/PaymentLineItem.js +141 -0
- package/cjs/dist/react/time-slot-list/TimeSlot.d.ts +23 -11
- package/cjs/dist/react/time-slot-list/TimeSlot.js +34 -16
- package/cjs/dist/react/time-slot-list/TimeSlotList.d.ts +6 -0
- package/cjs/dist/react/time-slot-list/TimeSlotList.js +23 -5
- package/cjs/dist/services/index.d.ts +1 -0
- package/cjs/dist/services/index.js +1 -0
- package/cjs/dist/services/payment/payment.d.ts +32 -0
- package/cjs/dist/services/payment/payment.def.d.ts +66 -0
- package/cjs/dist/services/payment/payment.def.js +9 -0
- package/cjs/dist/services/payment/payment.js +167 -0
- package/cjs/dist/utils/money.d.ts +11 -0
- package/cjs/dist/utils/money.js +16 -0
- package/dist/react/core/payment/Payment.d.ts +193 -0
- package/dist/react/core/payment/Payment.js +202 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/payment/Payment.d.ts +342 -0
- package/dist/react/payment/Payment.js +294 -0
- package/dist/react/payment/PaymentLineItem.d.ts +144 -0
- package/dist/react/payment/PaymentLineItem.js +141 -0
- package/dist/react/time-slot-list/TimeSlot.d.ts +23 -11
- package/dist/react/time-slot-list/TimeSlot.js +34 -16
- package/dist/react/time-slot-list/TimeSlotList.d.ts +6 -0
- package/dist/react/time-slot-list/TimeSlotList.js +23 -5
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/payment/payment.d.ts +32 -0
- package/dist/services/payment/payment.def.d.ts +66 -0
- package/dist/services/payment/payment.def.js +9 -0
- package/dist/services/payment/payment.js +167 -0
- package/dist/utils/money.d.ts +11 -0
- package/dist/utils/money.js +16 -0
- package/package.json +2 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Service Definition
|
|
3
|
+
* Defines interfaces and service contract for payment calculation management
|
|
4
|
+
*/
|
|
5
|
+
import type { Signal } from '@wix/services-definitions/core-services/signals';
|
|
6
|
+
import type { CalculateTotalsResponse } from '@wix/auto_sdk_ecom_totals-calculator';
|
|
7
|
+
import type { Service } from '@wix/auto_sdk_bookings_services';
|
|
8
|
+
/**
|
|
9
|
+
* Payment option for line items
|
|
10
|
+
*/
|
|
11
|
+
export type PaymentOption = 'FULL_PAYMENT_ONLINE' | 'FULL_PAYMENT_OFFLINE';
|
|
12
|
+
/**
|
|
13
|
+
* Additional computed info for a line item (not in SDK response)
|
|
14
|
+
*/
|
|
15
|
+
export interface LineItemAdditionalInfo {
|
|
16
|
+
/** Line item ID to match with SDK's calculatedLineItems */
|
|
17
|
+
lineItemId: string;
|
|
18
|
+
/** Custom price text (for CUSTOM rate type services) */
|
|
19
|
+
priceText?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Payment calculation result with SDK response and additional computed info
|
|
23
|
+
*/
|
|
24
|
+
export interface PaymentDetails {
|
|
25
|
+
/** The raw SDK CalculateTotals response */
|
|
26
|
+
response: CalculateTotalsResponse;
|
|
27
|
+
/** Additional computed information per line item */
|
|
28
|
+
additionalInfo: {
|
|
29
|
+
lineItems: LineItemAdditionalInfo[];
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Input for calculating payment for a slot/service
|
|
34
|
+
*/
|
|
35
|
+
export interface SlotService {
|
|
36
|
+
service: Service;
|
|
37
|
+
lineItemId: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Public config for PaymentService (SSR mode only):
|
|
41
|
+
* Pass `paymentDetails` pre-fetched via loadPaymentConfig
|
|
42
|
+
*/
|
|
43
|
+
export interface PaymentServiceConfig {
|
|
44
|
+
/** Pre-calculated payment details for SSR (no API call) */
|
|
45
|
+
paymentDetails?: PaymentDetails;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Internal config used by Core component to pass lazy loading data
|
|
49
|
+
* Not exposed publicly - Core merges this with PaymentServiceConfig
|
|
50
|
+
*/
|
|
51
|
+
export interface PaymentServiceInternalConfig extends PaymentServiceConfig {
|
|
52
|
+
/** Slot services for lazy loading (internal use) */
|
|
53
|
+
slotServices?: SlotService[];
|
|
54
|
+
/** Number of participants for lazy loading (internal use) */
|
|
55
|
+
numberOfParticipants?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface PaymentServiceAPI {
|
|
58
|
+
paymentDetailsSignal: Signal<PaymentDetails | null>;
|
|
59
|
+
isLoadingSignal: Signal<boolean>;
|
|
60
|
+
errorSignal: Signal<string | null>;
|
|
61
|
+
}
|
|
62
|
+
export declare const PaymentServiceDefinition: string & {
|
|
63
|
+
__api: PaymentServiceAPI;
|
|
64
|
+
__config: {};
|
|
65
|
+
isServiceDefinition?: boolean;
|
|
66
|
+
} & PaymentServiceAPI;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Service Definition
|
|
3
|
+
* Defines interfaces and service contract for payment calculation management
|
|
4
|
+
*/
|
|
5
|
+
import { defineService } from '@wix/services-definitions';
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Service Definition
|
|
8
|
+
// ============================================================================
|
|
9
|
+
export const PaymentServiceDefinition = defineService('payment');
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Service - Pricing calculation and payment details management
|
|
3
|
+
* Handles calculating total prices, taxes, discounts, and deposit information
|
|
4
|
+
* Supports two modes: SSR (config with pre-fetched data) and lazy loading (reactive)
|
|
5
|
+
*/
|
|
6
|
+
import { implementService } from '@wix/services-definitions';
|
|
7
|
+
import { SignalsServiceDefinition } from '@wix/services-definitions/core-services/signals';
|
|
8
|
+
import { calculateTotals, } from '@wix/auto_sdk_ecom_totals-calculator';
|
|
9
|
+
import { BookingServiceDefinition } from '../booking/booking.js';
|
|
10
|
+
import { RateType } from '@wix/auto_sdk_bookings_services';
|
|
11
|
+
import { PaymentServiceDefinition, } from './payment.def.js';
|
|
12
|
+
export { PaymentServiceDefinition, } from './payment.def.js';
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Constants
|
|
15
|
+
// ============================================================================
|
|
16
|
+
const BOOKINGS_APP_DEF_ID = '13d21c63-b5ec-5912-8397-c3a5ddb27a97';
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// Helper Functions
|
|
19
|
+
// ============================================================================
|
|
20
|
+
function getAdditionalInfo(service, lineItemId) {
|
|
21
|
+
const payment = service.payment;
|
|
22
|
+
const priceText = payment?.rateType === RateType.CUSTOM && payment.custom?.description
|
|
23
|
+
? payment.custom.description
|
|
24
|
+
: undefined;
|
|
25
|
+
return { lineItemId, priceText };
|
|
26
|
+
}
|
|
27
|
+
function getPaymentOption(service) {
|
|
28
|
+
const options = service.payment?.options;
|
|
29
|
+
if (options?.inPerson) {
|
|
30
|
+
return 'FULL_PAYMENT_OFFLINE';
|
|
31
|
+
}
|
|
32
|
+
return 'FULL_PAYMENT_ONLINE';
|
|
33
|
+
}
|
|
34
|
+
function buildLineItem(service, numberOfParticipants, lineItemId) {
|
|
35
|
+
// Get fixed price from service
|
|
36
|
+
const servicePrice = service.payment?.fixed?.price?.value;
|
|
37
|
+
const price = Number(servicePrice || 0) * numberOfParticipants;
|
|
38
|
+
// Get deposit if configured
|
|
39
|
+
const deposit = service.payment?.fixed?.deposit?.value
|
|
40
|
+
? Number(service.payment.fixed.deposit.value) * numberOfParticipants
|
|
41
|
+
: undefined;
|
|
42
|
+
// Get payment option from service configuration
|
|
43
|
+
const paymentOption = getPaymentOption(service);
|
|
44
|
+
return {
|
|
45
|
+
_id: lineItemId,
|
|
46
|
+
catalogReference: {
|
|
47
|
+
catalogItemId: service._id || '',
|
|
48
|
+
appId: BOOKINGS_APP_DEF_ID,
|
|
49
|
+
},
|
|
50
|
+
price: String(price),
|
|
51
|
+
...(deposit ? { depositAmount: String(deposit) } : {}),
|
|
52
|
+
quantity: 1,
|
|
53
|
+
paymentOption,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function calculatePaymentDetails(slotServices, numberOfParticipants) {
|
|
57
|
+
const lineItems = slotServices.map((input) => buildLineItem(input.service, numberOfParticipants, input.lineItemId));
|
|
58
|
+
const response = await calculateTotals({
|
|
59
|
+
lineItems,
|
|
60
|
+
});
|
|
61
|
+
const additionalInfoList = slotServices.map((input) => getAdditionalInfo(input.service, input.lineItemId));
|
|
62
|
+
return {
|
|
63
|
+
response,
|
|
64
|
+
additionalInfo: {
|
|
65
|
+
lineItems: additionalInfoList,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// ============================================================================
|
|
70
|
+
// Service Implementation
|
|
71
|
+
// ============================================================================
|
|
72
|
+
export const PaymentService = implementService.withConfig()(PaymentServiceDefinition, ({ getService, config }) => {
|
|
73
|
+
const signalsService = getService(SignalsServiceDefinition);
|
|
74
|
+
// Try to get BookingService (optional - for reactive mode)
|
|
75
|
+
let bookingService = null;
|
|
76
|
+
try {
|
|
77
|
+
bookingService = getService(BookingServiceDefinition);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
bookingService = null;
|
|
81
|
+
}
|
|
82
|
+
// Initialize signals
|
|
83
|
+
const paymentDetailsSignal = signalsService.signal(config.paymentDetails || null);
|
|
84
|
+
const isLoadingSignal = signalsService.signal(false);
|
|
85
|
+
const errorSignal = signalsService.signal(null);
|
|
86
|
+
// Internal calculate function
|
|
87
|
+
const calculate = async (slotServices, numberOfParticipants) => {
|
|
88
|
+
if (!slotServices.length) {
|
|
89
|
+
paymentDetailsSignal.set(null);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
isLoadingSignal.set(true);
|
|
93
|
+
errorSignal.set(null);
|
|
94
|
+
try {
|
|
95
|
+
const paymentDetails = await calculatePaymentDetails(slotServices, numberOfParticipants);
|
|
96
|
+
paymentDetailsSignal.set(paymentDetails);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const errorMessage = error instanceof Error
|
|
100
|
+
? error.message
|
|
101
|
+
: 'Failed to calculate payment';
|
|
102
|
+
errorSignal.set(errorMessage);
|
|
103
|
+
console.error('Payment calculation error:', error);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
isLoadingSignal.set(false);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// Mode 1: SSR - paymentDetails already set in signal initialization
|
|
110
|
+
if (config.paymentDetails) {
|
|
111
|
+
// No-op - data is pre-fetched
|
|
112
|
+
}
|
|
113
|
+
// Mode 2: Lazy loading - slotServices provided via internal config
|
|
114
|
+
else if (config.slotServices?.length) {
|
|
115
|
+
void calculate(config.slotServices, config.numberOfParticipants ?? 1);
|
|
116
|
+
}
|
|
117
|
+
// Mode 3: Reactive - observe BookingService signals (client-side only)
|
|
118
|
+
else if (typeof window !== 'undefined' && bookingService) {
|
|
119
|
+
signalsService.effect(() => {
|
|
120
|
+
const selections = bookingService?.serviceSelections.get() || [];
|
|
121
|
+
// For single service mode, take the first selection
|
|
122
|
+
const firstSelection = selections[0];
|
|
123
|
+
const numParticipants = firstSelection?.totalParticipants || 1;
|
|
124
|
+
const services = selections.map((selection) => ({
|
|
125
|
+
service: selection.service,
|
|
126
|
+
lineItemId: selection.instanceId,
|
|
127
|
+
}));
|
|
128
|
+
void calculate(services, numParticipants);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
paymentDetailsSignal,
|
|
133
|
+
isLoadingSignal,
|
|
134
|
+
errorSignal,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
/**
|
|
138
|
+
* Load payment configuration for Server-Side Rendering (SSR).
|
|
139
|
+
* Calls the pricing APIs and returns pre-calculated payment details.
|
|
140
|
+
*
|
|
141
|
+
* @param params.slotServices - Slot/service inputs for calculation
|
|
142
|
+
* @param params.numberOfParticipants - Number of participants (default: 1)
|
|
143
|
+
*/
|
|
144
|
+
export async function loadPaymentConfig(params) {
|
|
145
|
+
try {
|
|
146
|
+
const { slotServices, numberOfParticipants = 1 } = params;
|
|
147
|
+
if (!slotServices.length) {
|
|
148
|
+
return {
|
|
149
|
+
type: 'success',
|
|
150
|
+
config: { paymentDetails: undefined },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
const paymentDetails = await calculatePaymentDetails(slotServices, numberOfParticipants);
|
|
154
|
+
return {
|
|
155
|
+
type: 'success',
|
|
156
|
+
config: { paymentDetails },
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
const errorMessage = error instanceof Error ? error.message : 'Failed to load payment config';
|
|
161
|
+
console.error('Failed to load payment config:', error);
|
|
162
|
+
return {
|
|
163
|
+
type: 'error',
|
|
164
|
+
error: errorMessage,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
package/dist/utils/money.d.ts
CHANGED
|
@@ -19,3 +19,14 @@ export declare function convertToMoneyData(sdkMoney: {
|
|
|
19
19
|
currency?: string;
|
|
20
20
|
formattedValue?: string | null;
|
|
21
21
|
} | undefined): MoneyData | null;
|
|
22
|
+
/**
|
|
23
|
+
* Converts ecom MultiCurrencyPrice format to MoneyData format
|
|
24
|
+
* MultiCurrencyPrice uses 'amount' as string, while MoneyData uses 'amount' as number
|
|
25
|
+
*
|
|
26
|
+
* @param price - The MultiCurrencyPrice object with amount as string
|
|
27
|
+
* @param currency - The currency code (from response level)
|
|
28
|
+
* @returns MoneyData object or null if conversion fails
|
|
29
|
+
*/
|
|
30
|
+
export declare function convertMultiCurrencyPriceToMoneyData(price: {
|
|
31
|
+
amount?: string;
|
|
32
|
+
} | undefined, currency: string): MoneyData | null;
|
package/dist/utils/money.js
CHANGED
|
@@ -32,3 +32,19 @@ export function convertToMoneyData(sdkMoney) {
|
|
|
32
32
|
}
|
|
33
33
|
return null;
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Converts ecom MultiCurrencyPrice format to MoneyData format
|
|
37
|
+
* MultiCurrencyPrice uses 'amount' as string, while MoneyData uses 'amount' as number
|
|
38
|
+
*
|
|
39
|
+
* @param price - The MultiCurrencyPrice object with amount as string
|
|
40
|
+
* @param currency - The currency code (from response level)
|
|
41
|
+
* @returns MoneyData object or null if conversion fails
|
|
42
|
+
*/
|
|
43
|
+
export function convertMultiCurrencyPriceToMoneyData(price, currency) {
|
|
44
|
+
if (!price?.amount)
|
|
45
|
+
return null;
|
|
46
|
+
const parsed = parseFloat(price.amount);
|
|
47
|
+
if (isNaN(parsed) || parsed < 0)
|
|
48
|
+
return null;
|
|
49
|
+
return { amount: parsed, currency };
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/headless-bookings",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.32",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"groupId": "com.wixpress.headless-components"
|
|
73
73
|
}
|
|
74
74
|
},
|
|
75
|
-
"falconPackageHash": "
|
|
75
|
+
"falconPackageHash": "b165685e9ccb4d9fe0e8d310393e59018c8d44c874e2c7cd0fc9906a"
|
|
76
76
|
}
|