-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageCheckout.js
423 lines (382 loc) · 13.4 KB
/
PageCheckout.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
import BillingDetails from './BillingDetails/BillingDetails.vue';
import OrderOverview from './OrderOverview/OrderOverview.vue';
import ServerError from 'presentation/components/ServerError/ServerError.vue';
import {
POST_EMBED_TOKEN,
POST_UPDATE_BUYER,
POST_RESTORE_CART,
POST_UPDATE_PAYMENT
} from '../../../rest/paths';
import { cache } from '../../../apollo'
import { post } from '../../../rest/index';
import {
CHARGE,
AUTHORIZATION,
GR4VY_STATE_WIDGET_LOADING,
GR4VY_STATE_WIDGET_ERROR,
GR4VY_STATE_PAYMENT_COMPONENT_READY_FLAG,
GR4VY_FIELD_PAYMENT_TITLE,
CUSTOMER,
GR4vy_PAYMENT_METHOD,
GR4VY_STATE_SHIPPING_SAVED_FLAG,
GR4VY_STATE_LOAD_GR4VY_WIDGET_FLAG,
} from '../../../constants'
import { shallowRef, watch, provide, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import useCart from 'hooks/useCart';
import useCartTools from 'hooks/useCartTools';
import { setup } from '@gr4vy/embed';
import Loading from 'vue-loading-overlay';
import 'vue-loading-overlay/dist/css/index.css';
import useLocale from 'hooks/useLocale'
export default {
components: {
// CheckoutTopSection,
OrderOverview,
BillingDetails,
ServerError,
Loading,
},
setup() {
const { t } = useI18n();
const shippingMethod = shallowRef(null);
const billingAddress = shallowRef(null);
const shippingAddress = shallowRef(null);
const validBillingForm = shallowRef(false);
const validShippingForm = shallowRef(true);
const showError = shallowRef(false);
const error = shallowRef(null);
const { cart, loading } = useCart();
const cartTools = useCartTools();
let gr4vyEmbed;
const router = useRouter();
const { locale } = useLocale();
//record timestamp for refresh token.
const tokenTimestamp = shallowRef(null);
//Newly created order from Cart.
const orderId = shallowRef(null);
//Show loading spinner when order is submitted.
const isOrderSubmitted = shallowRef(false);
//PaymentMethod.vue component mounted
const isPaymentComponentReady = ref(false)
//Prevent Gr4vy widget token if shipping needs to be updated (concurrency error).
const isShippingReady = ref(false);
//Allow Widget to load only once.
const loadGr4vyWidget = ref(true);
//Flag to indicate that cart got updated due to generating G4vy token
const isGr4vyTokenCartUpdated = shallowRef(false);
//Flag if transaction failed. Don't re-render the widget or error message will be cleared.
const isTransactionFailed = shallowRef(false)
//Show loading spinner while loading Gr4vy widget
const widgetLoading = ref(false);
//Show error message if loading Gr4vy widget throws error.
const widgetError = ref(false);
//Payment Widget Title configured in Admin.
const paymentTitle = shallowRef('Payment Title');
provide(GR4VY_STATE_WIDGET_LOADING, widgetLoading);
provide(GR4VY_STATE_WIDGET_ERROR, widgetError);
provide(GR4VY_STATE_PAYMENT_COMPONENT_READY_FLAG, isPaymentComponentReady);
provide(GR4VY_STATE_SHIPPING_SAVED_FLAG, isShippingReady);
provide(GR4VY_STATE_LOAD_GR4VY_WIDGET_FLAG, loadGr4vyWidget)
provide(GR4VY_FIELD_PAYMENT_TITLE, paymentTitle);
//Logged in customer or Guest customer.
const isLoggedIn = !!JSON.parse(localStorage.getItem(CUSTOMER))
//@todo: what happened to the payment method passed to this?
const saveAddress = async () => {
if (!validBillingForm.value) {
showError.value = true;
return Promise.reject();
}
showError.value = false;
return cartTools
.setAddressForCart({
billingAddress,
shippingAddress,
})
.then(() => {
return Promise.resolve();
})
.catch((e) => {
error.value = e;
return Promise.reject();
});
};
//Load Gr4vy Widget configuration.
const loadWidgetConfig = async () => {
try {
widgetLoading.value = true;
const { result: config, errors} = await post(POST_EMBED_TOKEN, { locale: cart.locale});
//save timestamp to detect expiry of embed token
tokenTimestamp.value = Date.now();
//Once token is generated, cart is updated. stop going in infinite loop.
isGr4vyTokenCartUpdated.value = true;
//If payment integration is disabled, don't show widget.
if(!config.active) {
widgetLoading.value = false;
paymentTitle.value = "";
return
}
if (errors) {
console.error(errors);
throw new Error(errors)
}
//generating the token modifies the cart, so need to clear apollo cache.
cache.evict({ id: 'activeCart' });
cache.gc();
widgetLoading.value = false;
paymentTitle.value = config.title
setupGr4vyWidget(config);
return config;
}
catch(e) {
console.log("Error: ", e);
widgetLoading.value = false;
widgetError.value = true;
throw new Error(e);
}
}
//Render Gr4vy widget
const setupGr4vyWidget = (
{
gr4vyId,
buyerId,
environment,
amount,
currency,
country,
embedToken,
intent,
cartItems,
metadata,
paymentStore,
paymentSource,
requiredSecurityCode,
statementDescriptor,
themeOptions,
}
) => {
//map Gr4vy intent to CT Transaction type.
const IntentTransactionTypeMapping = {
capture: CHARGE,
authorize: AUTHORIZATION
}
//initialize the Gr4vy embed Widget.
const embedConfig = {}
embedConfig.gr4vyId = gr4vyId;
embedConfig.buyerId = buyerId;
embedConfig.environment = environment;
embedConfig.element = ".gr4vycontainer";
embedConfig.form = "#payment-form"
embedConfig.amount = amount;
embedConfig.currency = currency;
embedConfig.country = country;
embedConfig.token = embedToken;
embedConfig.intent = intent;
embedConfig.cartItems = cartItems;
embedConfig.metadata = metadata;
embedConfig.locale = locale.value;
if (isLoggedIn && paymentStore) embedConfig.store = paymentStore == 'ask' ? paymentStore : !! paymentStore;
else embedConfig.store = false; //don't show store payment details for guest users
if (paymentSource) embedConfig.paymentSource = paymentSource;
if (requiredSecurityCode) embedConfig.requireSecurityCode = !!requiredSecurityCode;
if (statementDescriptor) embedConfig.statementDescriptor = statementDescriptor;
if (themeOptions) embedConfig.theme = themeOptions;
gr4vyEmbed = setup({
...embedConfig,
//Before Transaction is created: Save Address, Update Buyer, Create Order from Cart.
onBeforeTransaction: async ()=> {
isOrderSubmitted.value = true;
let buyerInfo;
let refreshToken;
try {
await saveAddress();
buyerInfo = await updateBuyer();
//get new embed token if the existing one was created more than 1 hr ago.
if (!validEmbedToken()) {
const { result: { embedToken }, errors} = await post(POST_EMBED_TOKEN, { locale: cart.locale});
if (errors) {
console.error(errors);
throw new Error(errors)
}
tokenTimestamp.value = Date.now();
refreshToken = embedToken;
}
}
catch(e) {
//no need to restore cart as order is not yet submitted.
console.error("Error: ", e)
error.value = e;
isOrderSubmitted.value = false;
throw new Error(e);
}
//Place order
try {
const order = await cartTools.createMyOrderFromCart({
method: GR4vy_PAYMENT_METHOD,
cart: cart.value,
type: IntentTransactionTypeMapping[intent]
});
cache.evict({ id: 'activeCart' });
cache.gc();
orderId.value = order?.data?.createMyOrderFromCart?.orderId;
return ({
externalIdentifier: orderId.value,
shippingDetailsId: buyerInfo.shippingDetailsId,
...(refreshToken ? { token: refreshToken }:{})
});
}
catch(e) {
console.error("Error: ", e)
error.value = e;
await restoreCart(); //restore cart if order is submitted.
throw new Error(e);
}
},
//Completed Payment Transaction: Update Payment in order
onComplete: async (transaction) => {
console.log("Transaction: ", transaction);
try {
await updatePayment(transaction.id);
isOrderSubmitted.value = false;
router.push({
name: 'pay',
params: { method: GR4vy_PAYMENT_METHOD },
});
}
catch(e) {
console.error("Error: ", e)
error.value = e;
await restoreCart(transaction);
throw new Error(e);
}
},
//In case of an error event, restore cart, since order is already submitted.
onEvent: async (name, data) => {
console.log("Event Name: ", name, ", Data: ", data)
switch (name) {
case 'argumentError':
widgetError.value = true;
break;
case 'transactionCreated':
break;
case 'transactionFailed':
await restoreCart(data);
break;
case 'apiError':
await restoreCart(data);
break;
case 'transactionCancelled':
await restoreCart(data);
break;
default:
break;
}
}
});
}
//Hook to submit payment on Click of Submit Order Button.
const placeOrder = () => {
gr4vyEmbed.submit();
};
//order is already submitted. So recreate the cart from that order.
const restoreCart = async (transaction) => {
orderId.value && await post(POST_RESTORE_CART, {orderId: orderId.value, transaction});
cache.evict({ id: 'activeCart' });
cache.gc();
isTransactionFailed.value = true;
isOrderSubmitted.value = false;
}
//update order, payment and transaction based on transaction status
const updatePayment = async (transactionId) => {
const { errors } = await post(POST_UPDATE_PAYMENT, {gr4vyTransactionId: transactionId})
if(errors) {
console.error(errors);
error.value = errors;
throw new Error(errors);
}
cache.evict({ id: 'activeCart' });
cache.gc();
}
//update buyer.
const updateBuyer = async () => {
const { result, errors } = await post(POST_UPDATE_BUYER, { locale: cart.value.locale});
if(errors) {
console.error(errors);
throw new Error(errors);
}
//updating Buyer modifies the cart, so need to clear apollo cache.
cache.evict({ id: 'activeCart' });
cache.gc();
return result;
}
//Check if the gr4vy embed token has expired.
const validEmbedToken = () => {
const tokeExpiry = 1 * 60 * 60 *1000; //1hr to milliseconds
const current = Date.now();
if ( (current - tokenTimestamp.value) > tokeExpiry) {
return false;
}
return true;
}
watch([cart, loading], ([cart, loading]) => {
if (!cart && !loading && !isOrderSubmitted.value) {
router.replace({ path: '/' });
}
//If cart changes, unless it's order submit, regenerate the Gr4vy widget.
if(cart && !loading && !isOrderSubmitted.value && !isGr4vyTokenCartUpdated.value && !isTransactionFailed.value) {
loadGr4vyWidget.value = true;
error.value = null;
}
//when the cart re-loads the first time after token is generated, reload it.
if(isGr4vyTokenCartUpdated.value) isGr4vyTokenCartUpdated.value = false;
//when the cart re-loads after a failed transaction, don't reload widget the first time, next time allow it.
if(isTransactionFailed.value) isTransactionFailed.value = false;
});
//Once payment component is mounted and shipping updated, load widget once.
watch([isPaymentComponentReady, isShippingReady, loadGr4vyWidget], async ([p, s, l]) => {
if(p && s && l) {
await loadWidgetConfig();
loadGr4vyWidget.value = false; //reset flag after loading widget.
}
})
const setValidBillingForm = (valid) => {
validBillingForm.value = valid;
};
const setValidShippingForm = (valid) => {
validShippingForm.value = valid;
};
const updateBilling = (billingDetails) => {
billingAddress.value = JSON.parse(
JSON.stringify(billingDetails)
);
};
const updateShipping = (shippingDetails) => {
shippingAddress.value = JSON.parse(
JSON.stringify(shippingDetails)
);
};
const updateShippingMethod = (shippingId) => {
shippingMethod.value = shippingId;
};
return {
...cartTools,
shippingMethod,
billingAddress,
shippingAddress,
validBillingForm,
validShippingForm,
showError,
setValidBillingForm,
setValidShippingForm,
updateBilling,
updateShipping,
updateShippingMethod,
error,
cart,
t,
placeOrder,
isOrderSubmitted,
};
},
};