/
CheckoutPage.duck.js
489 lines (428 loc) · 15 KB
/
CheckoutPage.duck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import pick from 'lodash/pick';
import { initiatePrivileged, transitionPrivileged } from '../../util/api';
import { denormalisedResponseEntities } from '../../util/data';
import { storableError } from '../../util/errors';
import * as log from '../../util/log';
import { fetchCurrentUserHasOrdersSuccess, fetchCurrentUser } from '../../ducks/user.duck';
// ================ Action types ================ //
export const SET_INITIAL_VALUES = 'app/CheckoutPage/SET_INITIAL_VALUES';
export const INITIATE_ORDER_REQUEST = 'app/CheckoutPage/INITIATE_ORDER_REQUEST';
export const INITIATE_ORDER_SUCCESS = 'app/CheckoutPage/INITIATE_ORDER_SUCCESS';
export const INITIATE_ORDER_ERROR = 'app/CheckoutPage/INITIATE_ORDER_ERROR';
export const CONFIRM_PAYMENT_REQUEST = 'app/CheckoutPage/CONFIRM_PAYMENT_REQUEST';
export const CONFIRM_PAYMENT_SUCCESS = 'app/CheckoutPage/CONFIRM_PAYMENT_SUCCESS';
export const CONFIRM_PAYMENT_ERROR = 'app/CheckoutPage/CONFIRM_PAYMENT_ERROR';
export const SPECULATE_TRANSACTION_REQUEST = 'app/CheckoutPage/SPECULATE_TRANSACTION_REQUEST';
export const SPECULATE_TRANSACTION_SUCCESS = 'app/CheckoutPage/SPECULATE_TRANSACTION_SUCCESS';
export const SPECULATE_TRANSACTION_ERROR = 'app/CheckoutPage/SPECULATE_TRANSACTION_ERROR';
export const STRIPE_CUSTOMER_REQUEST = 'app/CheckoutPage/STRIPE_CUSTOMER_REQUEST';
export const STRIPE_CUSTOMER_SUCCESS = 'app/CheckoutPage/STRIPE_CUSTOMER_SUCCESS';
export const STRIPE_CUSTOMER_ERROR = 'app/CheckoutPage/STRIPE_CUSTOMER_ERROR';
export const INITIATE_INQUIRY_REQUEST = 'app/CheckoutPage/INITIATE_INQUIRY_REQUEST';
export const INITIATE_INQUIRY_SUCCESS = 'app/CheckoutPage/INITIATE_INQUIRY_SUCCESS';
export const INITIATE_INQUIRY_ERROR = 'app/CheckoutPage/INITIATE_INQUIRY_ERROR';
// ================ Reducer ================ //
const initialState = {
listing: null,
orderData: null,
speculateTransactionInProgress: false,
speculateTransactionError: null,
speculatedTransaction: null,
isClockInSync: false,
transaction: null,
initiateOrderError: null,
confirmPaymentError: null,
stripeCustomerFetched: false,
initiateInquiryInProgress: false,
initiateInquiryError: null,
};
export default function checkoutPageReducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case SET_INITIAL_VALUES:
return { ...initialState, ...payload };
case SPECULATE_TRANSACTION_REQUEST:
return {
...state,
speculateTransactionInProgress: true,
speculateTransactionError: null,
speculatedTransaction: null,
};
case SPECULATE_TRANSACTION_SUCCESS: {
// Check that the local devices clock is within a minute from the server
const lastTransitionedAt = payload.transaction?.attributes?.lastTransitionedAt;
const localTime = new Date();
const minute = 60000;
return {
...state,
speculateTransactionInProgress: false,
speculatedTransaction: payload.transaction,
isClockInSync: Math.abs(lastTransitionedAt?.getTime() - localTime.getTime()) < minute,
};
}
case SPECULATE_TRANSACTION_ERROR:
console.error(payload); // eslint-disable-line no-console
return {
...state,
speculateTransactionInProgress: false,
speculateTransactionError: payload,
};
case INITIATE_ORDER_REQUEST:
return { ...state, initiateOrderError: null };
case INITIATE_ORDER_SUCCESS:
return { ...state, transaction: payload };
case INITIATE_ORDER_ERROR:
console.error(payload); // eslint-disable-line no-console
return { ...state, initiateOrderError: payload };
case CONFIRM_PAYMENT_REQUEST:
return { ...state, confirmPaymentError: null };
case CONFIRM_PAYMENT_SUCCESS:
return state;
case CONFIRM_PAYMENT_ERROR:
console.error(payload); // eslint-disable-line no-console
return { ...state, confirmPaymentError: payload };
case STRIPE_CUSTOMER_REQUEST:
return { ...state, stripeCustomerFetched: false };
case STRIPE_CUSTOMER_SUCCESS:
return { ...state, stripeCustomerFetched: true };
case STRIPE_CUSTOMER_ERROR:
console.error(payload); // eslint-disable-line no-console
return { ...state, stripeCustomerFetchError: payload };
case INITIATE_INQUIRY_REQUEST:
return { ...state, initiateInquiryInProgress: true, initiateInquiryError: null };
case INITIATE_INQUIRY_SUCCESS:
return { ...state, initiateInquiryInProgress: false };
case INITIATE_INQUIRY_ERROR:
return { ...state, initiateInquiryInProgress: false, initiateInquiryError: payload };
default:
return state;
}
}
// ================ Selectors ================ //
// ================ Action creators ================ //
export const setInitialValues = initialValues => ({
type: SET_INITIAL_VALUES,
payload: pick(initialValues, Object.keys(initialState)),
});
const initiateOrderRequest = () => ({ type: INITIATE_ORDER_REQUEST });
const initiateOrderSuccess = order => ({
type: INITIATE_ORDER_SUCCESS,
payload: order,
});
const initiateOrderError = e => ({
type: INITIATE_ORDER_ERROR,
error: true,
payload: e,
});
const confirmPaymentRequest = () => ({ type: CONFIRM_PAYMENT_REQUEST });
const confirmPaymentSuccess = orderId => ({
type: CONFIRM_PAYMENT_SUCCESS,
payload: orderId,
});
const confirmPaymentError = e => ({
type: CONFIRM_PAYMENT_ERROR,
error: true,
payload: e,
});
export const speculateTransactionRequest = () => ({ type: SPECULATE_TRANSACTION_REQUEST });
export const speculateTransactionSuccess = transaction => ({
type: SPECULATE_TRANSACTION_SUCCESS,
payload: { transaction },
});
export const speculateTransactionError = e => ({
type: SPECULATE_TRANSACTION_ERROR,
error: true,
payload: e,
});
export const stripeCustomerRequest = () => ({ type: STRIPE_CUSTOMER_REQUEST });
export const stripeCustomerSuccess = () => ({ type: STRIPE_CUSTOMER_SUCCESS });
export const stripeCustomerError = e => ({
type: STRIPE_CUSTOMER_ERROR,
error: true,
payload: e,
});
export const initiateInquiryRequest = () => ({ type: INITIATE_INQUIRY_REQUEST });
export const initiateInquirySuccess = () => ({ type: INITIATE_INQUIRY_SUCCESS });
export const initiateInquiryError = e => ({
type: INITIATE_INQUIRY_ERROR,
error: true,
payload: e,
});
/* ================ Thunks ================ */
export const initiateOrder = (
orderParams,
processAlias,
transactionId,
transitionName,
isPrivilegedTransition
) => (dispatch, getState, sdk) => {
dispatch(initiateOrderRequest());
// If we already have a transaction ID, we should transition, not
// initiate.
const isTransition = !!transactionId;
const { deliveryMethod, quantity, bookingDates, ...otherOrderParams } = orderParams;
const quantityMaybe = quantity ? { stockReservationQuantity: quantity } : {};
const bookingParamsMaybe = bookingDates || {};
// Parameters only for client app's server
const orderData = deliveryMethod ? { deliveryMethod } : {};
// Parameters for Marketplace API
const transitionParams = {
...quantityMaybe,
...bookingParamsMaybe,
...otherOrderParams,
};
const bodyParams = isTransition
? {
id: transactionId,
transition: transitionName,
params: transitionParams,
}
: {
processAlias,
transition: transitionName,
params: transitionParams,
};
const queryParams = {
include: ['booking', 'provider'],
expand: true,
};
const handleSucces = response => {
const entities = denormalisedResponseEntities(response);
const order = entities[0];
dispatch(initiateOrderSuccess(order));
dispatch(fetchCurrentUserHasOrdersSuccess(true));
return order;
};
const handleError = e => {
dispatch(initiateOrderError(storableError(e)));
const transactionIdMaybe = transactionId ? { transactionId: transactionId.uuid } : {};
log.error(e, 'initiate-order-failed', {
...transactionIdMaybe,
listingId: orderParams.listingId.uuid,
...quantityMaybe,
...bookingParamsMaybe,
...orderData,
});
throw e;
};
if (isTransition && isPrivilegedTransition) {
// transition privileged
return transitionPrivileged({ isSpeculative: false, orderData, bodyParams, queryParams })
.then(handleSucces)
.catch(handleError);
} else if (isTransition) {
// transition non-privileged
return sdk.transactions
.transition(bodyParams, queryParams)
.then(handleSucces)
.catch(handleError);
} else if (isPrivilegedTransition) {
// initiate privileged
return initiatePrivileged({ isSpeculative: false, orderData, bodyParams, queryParams })
.then(handleSucces)
.catch(handleError);
} else {
// initiate non-privileged
return sdk.transactions
.initiate(bodyParams, queryParams)
.then(handleSucces)
.catch(handleError);
}
};
export const confirmPayment = (transactionId, transitionName, transitionParams = {}) => (
dispatch,
getState,
sdk
) => {
dispatch(confirmPaymentRequest());
const bodyParams = {
id: transactionId,
transition: transitionName,
params: transitionParams,
};
const queryParams = {
include: ['booking', 'provider'],
expand: true,
};
return sdk.transactions
.transition(bodyParams, queryParams)
.then(response => {
const order = response.data.data;
dispatch(confirmPaymentSuccess(order.id));
return order;
})
.catch(e => {
dispatch(confirmPaymentError(storableError(e)));
const transactionIdMaybe = transactionId ? { transactionId: transactionId.uuid } : {};
log.error(e, 'initiate-order-failed', {
...transactionIdMaybe,
});
throw e;
});
};
export const sendMessage = params => (dispatch, getState, sdk) => {
const message = params.message;
const orderId = params.id;
if (message) {
return sdk.messages
.send({ transactionId: orderId, content: message })
.then(() => {
return { orderId, messageSuccess: true };
})
.catch(e => {
log.error(e, 'initial-message-send-failed', { txId: orderId });
return { orderId, messageSuccess: false };
});
} else {
return Promise.resolve({ orderId, messageSuccess: true });
}
};
/**
* Initiate transaction against default-inquiry process
* Note: At this point inquiry transition is made directly against Marketplace API.
* So, client app's server is not involved here unlike with transitions including payments.
*
* @param {*} inquiryParams contains listingId and protectedData
* @param {*} processAlias 'default-inquiry/release-1'
* @param {*} transitionName 'transition/inquire-without-payment'
* @returns
*/
export const initiateInquiryWithoutPayment = (inquiryParams, processAlias, transitionName) => (
dispatch,
getState,
sdk
) => {
dispatch(initiateInquiryRequest());
if (!processAlias) {
const error = new Error('No transaction process attached to listing');
log.error(error, 'listing-process-missing', {
listingId: listing?.id?.uuid,
});
dispatch(initiateInquiryError(storableError(error)));
return Promise.reject(error);
}
const bodyParams = {
transition: transitionName,
processAlias,
params: inquiryParams,
};
const queryParams = {
include: ['provider'],
expand: true,
};
return sdk.transactions
.initiate(bodyParams, queryParams)
.then(response => {
const transactionId = response.data.data.id;
dispatch(initiateInquirySuccess());
return transactionId;
})
.catch(e => {
dispatch(initiateInquiryError(storableError(e)));
throw e;
});
};
/**
* Initiate or transition the speculative transaction with the given
* booking details
*
* The API allows us to do speculative transaction initiation and
* transitions. This way we can create a test transaction and get the
* actual pricing information as if the transaction had been started,
* without affecting the actual data.
*
* We store this speculative transaction in the page store and use the
* pricing info for the booking breakdown to get a proper estimate for
* the price with the chosen information.
*/
export const speculateTransaction = (
orderParams,
processAlias,
transactionId,
transitionName,
isPrivilegedTransition
) => (dispatch, getState, sdk) => {
dispatch(speculateTransactionRequest());
// If we already have a transaction ID, we should transition, not
// initiate.
const isTransition = !!transactionId;
const { deliveryMethod, quantity, bookingDates, ...otherOrderParams } = orderParams;
const quantityMaybe = quantity ? { stockReservationQuantity: quantity } : {};
const bookingParamsMaybe = bookingDates || {};
// Parameters only for client app's server
const orderData = deliveryMethod ? { deliveryMethod } : {};
// Parameters for Marketplace API
const transitionParams = {
...quantityMaybe,
...bookingParamsMaybe,
...otherOrderParams,
cardToken: 'CheckoutPage_speculative_card_token',
};
const bodyParams = isTransition
? {
id: transactionId,
transition: transitionName,
params: transitionParams,
}
: {
processAlias,
transition: transitionName,
params: transitionParams,
};
const queryParams = {
include: ['booking', 'provider'],
expand: true,
};
const handleSuccess = response => {
const entities = denormalisedResponseEntities(response);
if (entities.length !== 1) {
throw new Error('Expected a resource in the speculate response');
}
const tx = entities[0];
dispatch(speculateTransactionSuccess(tx));
};
const handleError = e => {
log.error(e, 'speculate-transaction-failed', {
listingId: transitionParams.listingId.uuid,
...quantityMaybe,
...bookingParamsMaybe,
...orderData,
});
return dispatch(speculateTransactionError(storableError(e)));
};
if (isTransition && isPrivilegedTransition) {
// transition privileged
return transitionPrivileged({ isSpeculative: true, orderData, bodyParams, queryParams })
.then(handleSuccess)
.catch(handleError);
} else if (isTransition) {
// transition non-privileged
return sdk.transactions
.transitionSpeculative(bodyParams, queryParams)
.then(handleSuccess)
.catch(handleError);
} else if (isPrivilegedTransition) {
// initiate privileged
return initiatePrivileged({ isSpeculative: true, orderData, bodyParams, queryParams })
.then(handleSuccess)
.catch(handleError);
} else {
// initiate non-privileged
return sdk.transactions
.initiateSpeculative(bodyParams, queryParams)
.then(handleSuccess)
.catch(handleError);
}
};
// StripeCustomer is a relantionship to currentUser
// We need to fetch currentUser with correct params to include relationship
export const stripeCustomer = () => (dispatch, getState, sdk) => {
dispatch(stripeCustomerRequest());
return dispatch(fetchCurrentUser({ include: ['stripeCustomer.defaultPaymentMethod'] }))
.then(response => {
dispatch(stripeCustomerSuccess());
})
.catch(e => {
dispatch(stripeCustomerError(storableError(e)));
});
};