@wix/headless-bookings 1.0.1 → 1.0.2
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/booking/Booking.js +4 -0
- package/cjs/dist/services/payment/payment.def.d.ts +6 -0
- package/cjs/dist/services/payment/payment.js +56 -5
- package/dist/react/core/booking/Booking.js +4 -0
- package/dist/services/payment/payment.def.d.ts +6 -0
- package/dist/services/payment/payment.js +57 -6
- package/package.json +3 -2
|
@@ -296,9 +296,13 @@ function PaymentData(props) {
|
|
|
296
296
|
const { children } = props;
|
|
297
297
|
const bookingService = (0, services_manager_react_1.useService)(booking_js_1.BookingServiceDefinition);
|
|
298
298
|
const serviceSelections = bookingService.serviceSelections.get();
|
|
299
|
+
const timezone = bookingService.timezone.get();
|
|
299
300
|
const pricingServiceSelections = serviceSelections.map((selection) => ({
|
|
300
301
|
service: selection.service,
|
|
301
302
|
lineItemId: selection.instanceId,
|
|
303
|
+
localStartDate: selection.timeSlot?.startDate,
|
|
304
|
+
localEndDate: selection.timeSlot?.endDate,
|
|
305
|
+
timeZone: timezone,
|
|
302
306
|
}));
|
|
303
307
|
return children({ pricingServiceSelections });
|
|
304
308
|
}
|
|
@@ -48,6 +48,12 @@ export interface PricingServiceSelection {
|
|
|
48
48
|
service: Service;
|
|
49
49
|
/** Optional line item ID. If not provided, one will be generated. */
|
|
50
50
|
lineItemId?: string;
|
|
51
|
+
/** Local start date of the selected slot in ISO-8601 format (e.g. "2024-01-30T13:30:00"). */
|
|
52
|
+
localStartDate?: string;
|
|
53
|
+
/** Local end date of the selected slot in ISO-8601 format (e.g. "2024-01-30T14:30:00"). */
|
|
54
|
+
localEndDate?: string;
|
|
55
|
+
/** IANA time zone identifier for the selected slot (e.g. "America/New_York"). */
|
|
56
|
+
timeZone?: string;
|
|
51
57
|
}
|
|
52
58
|
/**
|
|
53
59
|
* Public config for PaymentService (SSR mode only):
|
|
@@ -10,6 +10,7 @@ exports.loadPaymentConfig = loadPaymentConfig;
|
|
|
10
10
|
const services_definitions_1 = require("@wix/services-definitions");
|
|
11
11
|
const signals_1 = require("@wix/services-definitions/core-services/signals");
|
|
12
12
|
const auto_sdk_ecom_totals_calculator_1 = require("@wix/auto_sdk_ecom_totals-calculator");
|
|
13
|
+
const auto_sdk_bookings_pricing_1 = require("@wix/auto_sdk_bookings_pricing");
|
|
13
14
|
const booking_js_1 = require("../booking/booking.js");
|
|
14
15
|
const auto_sdk_bookings_services_1 = require("@wix/auto_sdk_bookings_services");
|
|
15
16
|
const payment_def_js_1 = require("./payment.def.js");
|
|
@@ -91,13 +92,56 @@ function buildLineItem(service, totalParticipants, lineItemId) {
|
|
|
91
92
|
paymentOption,
|
|
92
93
|
};
|
|
93
94
|
}
|
|
95
|
+
// durationRange is an @internal field on AvailabilityConstraints (bookings services SDK v1.0.226+).
|
|
96
|
+
// It signals that the service uses flexible duration pricing (hourly/daily rentals),
|
|
97
|
+
// where the correct price must be fetched via previewPrice() rather than the fixed unit price.
|
|
98
|
+
function getDurationRange(service) {
|
|
99
|
+
return service.schedule?.availabilityConstraints?.durationRange;
|
|
100
|
+
}
|
|
101
|
+
async function getPreviewedPrice(service, localStartDate, localEndDate, timeZone) {
|
|
102
|
+
if (!service._id)
|
|
103
|
+
return null;
|
|
104
|
+
const lineItemInput = {
|
|
105
|
+
serviceId: service._id,
|
|
106
|
+
localStartDate,
|
|
107
|
+
localEndDate,
|
|
108
|
+
...(timeZone ? { timeZone } : {}),
|
|
109
|
+
};
|
|
110
|
+
const result = await (0, auto_sdk_bookings_pricing_1.previewPrice)([lineItemInput]);
|
|
111
|
+
return result.priceInfo?.calculatedPrice ?? null;
|
|
112
|
+
}
|
|
113
|
+
async function buildLineItemWithPreview(item, totalParticipants) {
|
|
114
|
+
if (item.localStartDate &&
|
|
115
|
+
item.localEndDate &&
|
|
116
|
+
getDurationRange(item.service)) {
|
|
117
|
+
const previewedPrice = await getPreviewedPrice(item.service, item.localStartDate, item.localEndDate, item.timeZone).catch(() => null);
|
|
118
|
+
if (previewedPrice != null) {
|
|
119
|
+
const depositValue = item.service.payment?.fixed?.deposit?.value;
|
|
120
|
+
const hasDeposit = !!depositValue && Number(depositValue) > 0;
|
|
121
|
+
const deposit = hasDeposit
|
|
122
|
+
? Number(depositValue) * totalParticipants
|
|
123
|
+
: undefined;
|
|
124
|
+
return {
|
|
125
|
+
_id: item.lineItemId,
|
|
126
|
+
catalogReference: {
|
|
127
|
+
catalogItemId: item.service._id || '',
|
|
128
|
+
appId: constants_js_1.BOOKING_APP_ID,
|
|
129
|
+
},
|
|
130
|
+
price: String(previewedPrice),
|
|
131
|
+
...(deposit ? { depositAmount: String(deposit) } : {}),
|
|
132
|
+
quantity: 1,
|
|
133
|
+
paymentOption: getPaymentOption(item.service, hasDeposit),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return buildLineItem(item.service, totalParticipants, item.lineItemId);
|
|
138
|
+
}
|
|
94
139
|
async function calculatePaymentDetails(pricingServiceSelections, totalParticipants) {
|
|
95
|
-
// Ensure all booking services have a lineItemId
|
|
96
140
|
const data = pricingServiceSelections.map((selection) => ({
|
|
97
141
|
...selection,
|
|
98
142
|
lineItemId: selection.lineItemId || (0, guid_js_1.generateLineItemId)(),
|
|
99
143
|
}));
|
|
100
|
-
const lineItems = data.map((item) =>
|
|
144
|
+
const lineItems = await Promise.all(data.map((item) => buildLineItemWithPreview(item, totalParticipants)));
|
|
101
145
|
const response = await (0, auto_sdk_ecom_totals_calculator_1.calculateTotals)({
|
|
102
146
|
lineItems,
|
|
103
147
|
});
|
|
@@ -168,22 +212,29 @@ exports.PaymentService = services_definitions_1.implementService.withConfig()(pa
|
|
|
168
212
|
}
|
|
169
213
|
// Mode 3: Reactive - observe BookingService signals (client-side only)
|
|
170
214
|
else if (typeof window !== 'undefined' && bookingService) {
|
|
171
|
-
// Computed signal that
|
|
215
|
+
// Computed signal that changes when services or selected time slots change (not participants)
|
|
172
216
|
const servicesKey = signalsService.computed(() => {
|
|
173
217
|
const selections = bookingService?.serviceSelections.get() || [];
|
|
174
|
-
|
|
218
|
+
const timezone = bookingService?.timezone.get();
|
|
219
|
+
return (selections
|
|
220
|
+
.map((s) => `${s.service._id}:${s.timeSlot?.startDate ?? ''}:${s.timeSlot?.endDate ?? ''}`)
|
|
221
|
+
.join(',') + `:${timezone ?? ''}`);
|
|
175
222
|
});
|
|
176
223
|
signalsService.effect(() => {
|
|
177
|
-
// Subscribe to servicesKey to trigger effect
|
|
224
|
+
// Subscribe to servicesKey to trigger effect when service or time slot changes
|
|
178
225
|
servicesKey.get();
|
|
179
226
|
// Read current selections without subscribing (peek)
|
|
180
227
|
const selections = bookingService?.serviceSelections.peek() || [];
|
|
228
|
+
const timezone = bookingService?.timezone.peek();
|
|
181
229
|
// For single service mode, take the first selection
|
|
182
230
|
const firstSelection = selections[0];
|
|
183
231
|
const numParticipants = firstSelection?.totalParticipants || 1;
|
|
184
232
|
const pricingServiceSelections = selections.map((selection) => ({
|
|
185
233
|
service: selection.service,
|
|
186
234
|
lineItemId: selection.instanceId,
|
|
235
|
+
localStartDate: selection.timeSlot?.startDate,
|
|
236
|
+
localEndDate: selection.timeSlot?.endDate,
|
|
237
|
+
timeZone: timezone,
|
|
187
238
|
}));
|
|
188
239
|
if (pricingServiceSelections.length) {
|
|
189
240
|
serviceOptionsSignal.set(deriveServiceOptions(pricingServiceSelections));
|
|
@@ -284,9 +284,13 @@ export function PaymentData(props) {
|
|
|
284
284
|
const { children } = props;
|
|
285
285
|
const bookingService = useService(BookingServiceDefinition);
|
|
286
286
|
const serviceSelections = bookingService.serviceSelections.get();
|
|
287
|
+
const timezone = bookingService.timezone.get();
|
|
287
288
|
const pricingServiceSelections = serviceSelections.map((selection) => ({
|
|
288
289
|
service: selection.service,
|
|
289
290
|
lineItemId: selection.instanceId,
|
|
291
|
+
localStartDate: selection.timeSlot?.startDate,
|
|
292
|
+
localEndDate: selection.timeSlot?.endDate,
|
|
293
|
+
timeZone: timezone,
|
|
290
294
|
}));
|
|
291
295
|
return children({ pricingServiceSelections });
|
|
292
296
|
}
|
|
@@ -48,6 +48,12 @@ export interface PricingServiceSelection {
|
|
|
48
48
|
service: Service;
|
|
49
49
|
/** Optional line item ID. If not provided, one will be generated. */
|
|
50
50
|
lineItemId?: string;
|
|
51
|
+
/** Local start date of the selected slot in ISO-8601 format (e.g. "2024-01-30T13:30:00"). */
|
|
52
|
+
localStartDate?: string;
|
|
53
|
+
/** Local end date of the selected slot in ISO-8601 format (e.g. "2024-01-30T14:30:00"). */
|
|
54
|
+
localEndDate?: string;
|
|
55
|
+
/** IANA time zone identifier for the selected slot (e.g. "America/New_York"). */
|
|
56
|
+
timeZone?: string;
|
|
51
57
|
}
|
|
52
58
|
/**
|
|
53
59
|
* Public config for PaymentService (SSR mode only):
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
import { implementService } from '@wix/services-definitions';
|
|
7
7
|
import { SignalsServiceDefinition } from '@wix/services-definitions/core-services/signals';
|
|
8
8
|
import { calculateTotals, } from '@wix/auto_sdk_ecom_totals-calculator';
|
|
9
|
+
import { previewPrice, } from '@wix/auto_sdk_bookings_pricing';
|
|
9
10
|
import { BookingServiceDefinition } from '../booking/booking.js';
|
|
10
|
-
import { RateType } from '@wix/auto_sdk_bookings_services';
|
|
11
|
+
import { RateType, } from '@wix/auto_sdk_bookings_services';
|
|
11
12
|
import { PaymentServiceDefinition, } from './payment.def.js';
|
|
12
13
|
import { generateLineItemId } from '../../utils/guid.js';
|
|
13
14
|
export { PaymentServiceDefinition, } from './payment.def.js';
|
|
@@ -86,13 +87,56 @@ function buildLineItem(service, totalParticipants, lineItemId) {
|
|
|
86
87
|
paymentOption,
|
|
87
88
|
};
|
|
88
89
|
}
|
|
90
|
+
// durationRange is an @internal field on AvailabilityConstraints (bookings services SDK v1.0.226+).
|
|
91
|
+
// It signals that the service uses flexible duration pricing (hourly/daily rentals),
|
|
92
|
+
// where the correct price must be fetched via previewPrice() rather than the fixed unit price.
|
|
93
|
+
function getDurationRange(service) {
|
|
94
|
+
return service.schedule?.availabilityConstraints?.durationRange;
|
|
95
|
+
}
|
|
96
|
+
async function getPreviewedPrice(service, localStartDate, localEndDate, timeZone) {
|
|
97
|
+
if (!service._id)
|
|
98
|
+
return null;
|
|
99
|
+
const lineItemInput = {
|
|
100
|
+
serviceId: service._id,
|
|
101
|
+
localStartDate,
|
|
102
|
+
localEndDate,
|
|
103
|
+
...(timeZone ? { timeZone } : {}),
|
|
104
|
+
};
|
|
105
|
+
const result = await previewPrice([lineItemInput]);
|
|
106
|
+
return result.priceInfo?.calculatedPrice ?? null;
|
|
107
|
+
}
|
|
108
|
+
async function buildLineItemWithPreview(item, totalParticipants) {
|
|
109
|
+
if (item.localStartDate &&
|
|
110
|
+
item.localEndDate &&
|
|
111
|
+
getDurationRange(item.service)) {
|
|
112
|
+
const previewedPrice = await getPreviewedPrice(item.service, item.localStartDate, item.localEndDate, item.timeZone).catch(() => null);
|
|
113
|
+
if (previewedPrice != null) {
|
|
114
|
+
const depositValue = item.service.payment?.fixed?.deposit?.value;
|
|
115
|
+
const hasDeposit = !!depositValue && Number(depositValue) > 0;
|
|
116
|
+
const deposit = hasDeposit
|
|
117
|
+
? Number(depositValue) * totalParticipants
|
|
118
|
+
: undefined;
|
|
119
|
+
return {
|
|
120
|
+
_id: item.lineItemId,
|
|
121
|
+
catalogReference: {
|
|
122
|
+
catalogItemId: item.service._id || '',
|
|
123
|
+
appId: BOOKING_APP_ID,
|
|
124
|
+
},
|
|
125
|
+
price: String(previewedPrice),
|
|
126
|
+
...(deposit ? { depositAmount: String(deposit) } : {}),
|
|
127
|
+
quantity: 1,
|
|
128
|
+
paymentOption: getPaymentOption(item.service, hasDeposit),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return buildLineItem(item.service, totalParticipants, item.lineItemId);
|
|
133
|
+
}
|
|
89
134
|
async function calculatePaymentDetails(pricingServiceSelections, totalParticipants) {
|
|
90
|
-
// Ensure all booking services have a lineItemId
|
|
91
135
|
const data = pricingServiceSelections.map((selection) => ({
|
|
92
136
|
...selection,
|
|
93
137
|
lineItemId: selection.lineItemId || generateLineItemId(),
|
|
94
138
|
}));
|
|
95
|
-
const lineItems = data.map((item) =>
|
|
139
|
+
const lineItems = await Promise.all(data.map((item) => buildLineItemWithPreview(item, totalParticipants)));
|
|
96
140
|
const response = await calculateTotals({
|
|
97
141
|
lineItems,
|
|
98
142
|
});
|
|
@@ -163,22 +207,29 @@ export const PaymentService = implementService.withConfig()(PaymentServiceDefini
|
|
|
163
207
|
}
|
|
164
208
|
// Mode 3: Reactive - observe BookingService signals (client-side only)
|
|
165
209
|
else if (typeof window !== 'undefined' && bookingService) {
|
|
166
|
-
// Computed signal that
|
|
210
|
+
// Computed signal that changes when services or selected time slots change (not participants)
|
|
167
211
|
const servicesKey = signalsService.computed(() => {
|
|
168
212
|
const selections = bookingService?.serviceSelections.get() || [];
|
|
169
|
-
|
|
213
|
+
const timezone = bookingService?.timezone.get();
|
|
214
|
+
return (selections
|
|
215
|
+
.map((s) => `${s.service._id}:${s.timeSlot?.startDate ?? ''}:${s.timeSlot?.endDate ?? ''}`)
|
|
216
|
+
.join(',') + `:${timezone ?? ''}`);
|
|
170
217
|
});
|
|
171
218
|
signalsService.effect(() => {
|
|
172
|
-
// Subscribe to servicesKey to trigger effect
|
|
219
|
+
// Subscribe to servicesKey to trigger effect when service or time slot changes
|
|
173
220
|
servicesKey.get();
|
|
174
221
|
// Read current selections without subscribing (peek)
|
|
175
222
|
const selections = bookingService?.serviceSelections.peek() || [];
|
|
223
|
+
const timezone = bookingService?.timezone.peek();
|
|
176
224
|
// For single service mode, take the first selection
|
|
177
225
|
const firstSelection = selections[0];
|
|
178
226
|
const numParticipants = firstSelection?.totalParticipants || 1;
|
|
179
227
|
const pricingServiceSelections = selections.map((selection) => ({
|
|
180
228
|
service: selection.service,
|
|
181
229
|
lineItemId: selection.instanceId,
|
|
230
|
+
localStartDate: selection.timeSlot?.startDate,
|
|
231
|
+
localEndDate: selection.timeSlot?.endDate,
|
|
232
|
+
timeZone: timezone,
|
|
182
233
|
}));
|
|
183
234
|
if (pricingServiceSelections.length) {
|
|
184
235
|
serviceOptionsSignal.set(deriveServiceOptions(pricingServiceSelections));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/headless-bookings",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@wix/auto_sdk_bookings_bookings": "^1.0.183",
|
|
38
38
|
"@wix/auto_sdk_bookings_event-time-slots": "^1.0.64",
|
|
39
|
+
"@wix/auto_sdk_bookings_pricing": "^1.0.87",
|
|
39
40
|
"@wix/auto_sdk_bookings_services": "^1.0.226",
|
|
40
41
|
"@wix/auto_sdk_ecom_cart-v-2": "^1.0.86",
|
|
41
42
|
"@wix/forms": "^1.0.384",
|
|
@@ -74,5 +75,5 @@
|
|
|
74
75
|
"groupId": "com.wixpress.headless-components"
|
|
75
76
|
}
|
|
76
77
|
},
|
|
77
|
-
"falconPackageHash": "
|
|
78
|
+
"falconPackageHash": "402687344b7c916e00531f83dab622fae9b9efda7b82abadc49c484b"
|
|
78
79
|
}
|