generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathmerch.js
765 lines (717 loc) · 22.5 KB
/
merch.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import {
createTag, getConfig, loadArea, loadScript, loadStyle, localizeLink, SLD,
} from '../../utils/utils.js';
import { replaceKey } from '../../features/placeholders.js';
export const CHECKOUT_LINK_CONFIG_PATH = '/commerce/checkout-link.json'; // relative to libs.
export const PRICE_TEMPLATE_DISCOUNT = 'discount';
export const PRICE_TEMPLATE_OPTICAL = 'optical';
export const PRICE_TEMPLATE_REGULAR = 'price';
export const PRICE_TEMPLATE_STRIKETHROUGH = 'strikethrough';
export const PRICE_TEMPLATE_ANNUAL = 'annual';
const PRICE_TEMPLATE_MAPPING = new Map([
['priceDiscount', PRICE_TEMPLATE_DISCOUNT],
[PRICE_TEMPLATE_DISCOUNT, PRICE_TEMPLATE_DISCOUNT],
['priceOptical', PRICE_TEMPLATE_OPTICAL],
[PRICE_TEMPLATE_OPTICAL, PRICE_TEMPLATE_OPTICAL],
['priceStrikethrough', PRICE_TEMPLATE_STRIKETHROUGH],
[PRICE_TEMPLATE_STRIKETHROUGH, PRICE_TEMPLATE_STRIKETHROUGH],
['priceAnnual', PRICE_TEMPLATE_ANNUAL],
[PRICE_TEMPLATE_ANNUAL, PRICE_TEMPLATE_ANNUAL],
]);
export const PLACEHOLDER_KEY_DOWNLOAD = 'download';
export const CC_SINGLE_APPS = [
['3D_TEXTURING'],
['3DI'],
['ACROBAT', 'ACROBAT_STOCK_BUNDLE'],
['AFTEREFFECTS', 'AFTER_EFFECTS_STOCK_BUNDLE'],
['AUDITION', 'AUDITION_STOCK_BUNDLE'],
['CC_EXPRESS'],
['FLASH', 'FLASH_STOCK_BUNDLE'],
['DREAMWEAVER', 'DREAMWEAVER_STOCK_BUNDLE'],
['EDGE_ANIMATE'],
['ILLUSTRATOR', 'ILLUSTRATOR_STOCK_BUNDLE'],
['INCOPY', 'INCOPY_STOCK_BUNDLE'],
['INDESIGN', 'INDESIGN_STOCK_BUNDLE'],
['PHOTOGRAPHY', 'PHOTOGRAPHY_STOCK_BUNDLE'],
['PHOTOSHOP_LIGHTROOM'],
['PHOTOSHOP', 'PHOTOSHOP_STOCK_BUNDLE'],
['PREMIERE', 'PREMIERE_STOCK_BUNDLE'],
['RUSH'],
['XD'],
];
const GeoMap = {
ar: 'AR_es',
be_en: 'BE_en',
be_fr: 'BE_fr',
be_nl: 'BE_nl',
br: 'BR_pt',
ca: 'CA_en',
ch_de: 'CH_de',
ch_fr: 'CH_fr',
ch_it: 'CH_it',
cl: 'CL_es',
co: 'CO_es',
la: 'DO_es',
mx: 'MX_es',
pe: 'PE_es',
africa: 'MU_en',
dk: 'DK_da',
de: 'DE_de',
ee: 'EE_et',
eg_ar: 'EG_ar',
eg_en: 'EG_en',
es: 'ES_es',
fr: 'FR_fr',
gr_el: 'GR_el',
gr_en: 'GR_en',
ie: 'IE_en',
il_he: 'IL_iw',
it: 'IT_it',
lv: 'LV_lv',
lt: 'LT_lt',
lu_de: 'LU_de',
lu_en: 'LU_en',
lu_fr: 'LU_fr',
my_en: 'MY_en',
my_ms: 'MY_ms',
hu: 'HU_hu',
mt: 'MT_en',
mena_en: 'DZ_en',
mena_ar: 'DZ_ar',
nl: 'NL_nl',
no: 'NO_nb',
pl: 'PL_pl',
pt: 'PT_pt',
ro: 'RO_ro',
si: 'SI_sl',
sk: 'SK_sk',
fi: 'FI_fi',
se: 'SE_sv',
tr: 'TR_tr',
uk: 'GB_en',
at: 'AT_de',
cz: 'CZ_cs',
bg: 'BG_bg',
ru: 'RU_ru',
ua: 'UA_uk',
au: 'AU_en',
in_en: 'IN_en',
in_hi: 'IN_hi',
id_en: 'ID_en',
id_id: 'ID_in',
nz: 'NZ_en',
sa_ar: 'SA_ar',
sa_en: 'SA_en',
sg: 'SG_en',
cn: 'CN_zh-Hans',
tw: 'TW_zh-Hant',
hk_zh: 'HK_zh-hant',
jp: 'JP_ja',
kr: 'KR_ko',
za: 'ZA_en',
ng: 'NG_en',
cr: 'CR_es',
ec: 'EC_es',
pr: 'US_es', // not a typo, should be US
gt: 'GT_es',
cis_en: 'AZ_en',
cis_ru: 'AZ_ru',
sea: 'SG_en',
th_en: 'TH_en',
th_th: 'TH_th',
};
export function getMiloLocaleSettings(locale) {
const localePrefix = locale?.prefix || 'US_en';
const geo = localePrefix.replace('/', '') ?? '';
let [country = 'US', language = 'en'] = (
GeoMap[geo] ?? geo
).split('_', 2);
country = country.toUpperCase();
language = language.toLowerCase();
return {
language,
country,
locale: `${language}_${country}`,
};
}
/* Optional checkout link params that are appended to checkout urls as is */
export const CHECKOUT_ALLOWED_KEYS = [
'af',
'ai',
'apc',
'appctxid',
'cli',
'co',
'csm',
'ctx',
'ctxRtUrl',
'DCWATC',
'dp', // Enable digital payments for iframe context
'fr', // represents the commerce app redirecting to UC
'gsp',
'ijt',
'lang',
'lo',
'mal',
'ms',
'mv',
'mv2',
'nglwfdata',
'ot',
'otac',
'pa',
'pcid', // Unified Paywall configuration ID for analytics
'promoid',
'q',
'rf',
'sc',
'scl',
'sdid',
'sid', // x-adobe-clientsession
'spint',
'svar',
'th',
'thm',
'trackingid',
'usid',
'workflowid',
'context.guid',
'so.ca',
'so.su',
'so.tr',
'so.va',
// below keys are mapped to shorted versions.
'quantity',
'authCode',
'checkoutPromoCode',
'rurl',
'curl',
'ctxrturl',
'country',
'language',
'clientId',
'context',
'productArrangementCode',
'offerType',
'marketSegment',
];
export const CC_SINGLE_APPS_ALL = CC_SINGLE_APPS.flatMap((item) => item);
export const CC_ALL_APPS = ['CC_ALL_APPS',
'CC_ALL_APPS_STOCK_BUNDLE', 'CC_PRO'];
const NAME_LOCALE = 'LOCALE';
const NAME_PRODUCT_FAMILY = 'PRODUCT_FAMILY';
const FREE_TRIAL_PATH = 'FREE_TRIAL_PATH';
const BUY_NOW_PATH = 'BUY_NOW_PATH';
const FREE_TRIAL_HASH = 'FREE_TRIAL_HASH';
const BUY_NOW_HASH = 'BUY_NOW_HASH';
const OFFER_TYPE_TRIAL = 'TRIAL';
const LOADING_ENTITLEMENTS = 'loading-entitlements';
let log;
let upgradeOffer = null;
/**
* Given a url, calculates the hostname of MAS platform.
* Supports, www prod, stage, local and feature branches.
* if params are missing, it will return the latest calculated or default value.
* @param {string} hostname optional
* @param {string} maslibs optional
* @returns base url for mas platform
*/
export function getMasBase(hostname, maslibs) {
let { baseUrl } = getMasBase;
if (!baseUrl) {
if (maslibs === 'stage') {
baseUrl = 'https://www.stage.adobe.com/mas';
} else if (maslibs === 'local') {
baseUrl = 'http://localhost:9001';
} else if (maslibs) {
const extension = /.page$/.test(hostname) ? 'page' : 'live';
baseUrl = `https://${maslibs}.${SLD}.${extension}`;
} else {
baseUrl = 'https://www.adobe.com/mas';
}
getMasBase.baseUrl = baseUrl;
}
return baseUrl;
}
export async function polyfills() {
if (polyfills.promise) return polyfills.promise;
let isSupported = false;
document.createElement('div', {
// eslint-disable-next-line getter-return
get is() {
isSupported = true;
},
});
if (isSupported) {
polyfills.promise = Promise.resolve();
} else {
const { base } = getConfig();
polyfills.promise = await loadScript(`${base}/deps/custom-elements.js`);
}
return polyfills.promise;
}
export async function fetchEntitlements() {
fetchEntitlements.promise = fetchEntitlements.promise ?? import('../global-navigation/utilities/getUserEntitlements.js')
.then(({ default: getUserEntitlements }) => getUserEntitlements(
{
params:
[
{ name: 'include', value: 'OFFER.PRODUCT_ARRANGEMENT' }],
format: 'raw',
},
));
return fetchEntitlements.promise;
}
export async function fetchCheckoutLinkConfigs(base = '') {
fetchCheckoutLinkConfigs.promise = fetchCheckoutLinkConfigs.promise
?? fetch(`${base}${CHECKOUT_LINK_CONFIG_PATH}`).catch((e) => {
log?.error('Failed to fetch checkout link configs', e);
}).then((mappings) => {
if (!mappings?.ok) return { data: [] };
return mappings.json();
});
return fetchCheckoutLinkConfigs.promise;
}
export async function getCheckoutLinkConfig(productFamily, productCode, paCode) {
let { base } = getConfig();
if (/\.page$/.test(document.location.origin)) {
/* c8 ignore next 2 */
base = base.replace('.live', '.page');
}
const checkoutLinkConfigs = await fetchCheckoutLinkConfigs(base);
if (!checkoutLinkConfigs.data.length) return undefined;
const { locale: { region } } = getConfig();
const {
paCodeConfigs,
productCodeConfigs,
productFamilyConfigs,
} = checkoutLinkConfigs.data.reduce((acc, config) => {
if (config[NAME_PRODUCT_FAMILY] === paCode) {
acc.paCodeConfigs.push(config);
} else if (config[NAME_PRODUCT_FAMILY] === productCode) {
acc.productCodeConfigs.push(config);
} else if (config[NAME_PRODUCT_FAMILY] === productFamily) {
acc.productFamilyConfigs.push(config);
}
return acc;
}, { paCodeConfigs: [], productCodeConfigs: [], productFamilyConfigs: [] });
// helps to fallback to product family config
// if no locale specific config is found below.
const productCheckoutLinkConfigs = [
...paCodeConfigs, ...productCodeConfigs, ...productFamilyConfigs,
];
if (!productCheckoutLinkConfigs.length) return undefined;
const checkoutLinkConfig = productCheckoutLinkConfigs.find(
({ [NAME_LOCALE]: locale }) => locale === '',
);
const checkoutLinkConfigOverride = productCheckoutLinkConfigs.find(
({ [NAME_LOCALE]: locale }) => locale === region,
) ?? {};
const overrides = Object.fromEntries(
Object.entries(checkoutLinkConfigOverride).filter(([, value]) => value),
);
const finalConfig = { ...checkoutLinkConfig, ...overrides };
Object.entries(finalConfig).forEach(([key, value]) => {
if (value === 'X' || value === '❌') {
finalConfig[key] = '';
}
});
return finalConfig;
}
export async function getDownloadAction(
options,
imsSignedInPromise,
[{
offerType,
productArrangementCode,
productArrangement: { productCode, productFamily: offerFamily } = {},
}],
) {
if (options.entitlement !== true) return undefined;
const loggedIn = await imsSignedInPromise;
if (!loggedIn) return undefined;
const entitlements = await fetchEntitlements();
if (!entitlements?.length) return undefined;
const checkoutLinkConfig = await getCheckoutLinkConfig(
offerFamily,
productCode,
productArrangementCode,
);
if (!checkoutLinkConfig?.DOWNLOAD_URL) return undefined;
const offer = entitlements.find((
{ offer: { product_arrangement: { family: subscriptionFamily } } },
) => {
if (CC_ALL_APPS.includes(subscriptionFamily)) return true; // has all apps
if (CC_ALL_APPS.includes(offerFamily)) return false; // hasn't all apps and cta is all apps
const singleAppFamily = CC_SINGLE_APPS // has single and and cta is single app
.find((singleAppFamilies) => singleAppFamilies.includes(offerFamily));
return singleAppFamily?.includes(subscriptionFamily);
});
if (!offer) return undefined;
const config = getConfig();
const text = await replaceKey(checkoutLinkConfig.DOWNLOAD_TEXT
|| PLACEHOLDER_KEY_DOWNLOAD, config);
const url = localizeLink(checkoutLinkConfig.DOWNLOAD_URL);
const type = offerType?.toLowerCase() ?? '';
return { text, className: `download ${type}`, url };
}
export async function getUpgradeAction(
options,
imsSignedInPromise,
[{ productArrangement: { productFamily: offerFamily } = {} }],
) {
if (!options.upgrade) return undefined;
const loggedIn = await imsSignedInPromise;
if (!loggedIn) return undefined;
const entitlements = await fetchEntitlements();
if (upgradeOffer === null) {
upgradeOffer = undefined;
// will enter only once
upgradeOffer = await document.querySelector('.merch-offers.upgrade [data-wcs-osi]');
}
await upgradeOffer?.onceSettled();
if (upgradeOffer && entitlements?.length && offerFamily) {
const { default: handleUpgradeOffer } = await import('./upgrade.js');
const upgradeAction = await handleUpgradeOffer(
offerFamily,
upgradeOffer,
entitlements,
CC_SINGLE_APPS_ALL,
CC_ALL_APPS,
);
return upgradeAction;
}
return undefined;
}
async function openFragmentModal(path, getModal) {
const root = createTag('div');
root.style.visibility = 'hidden';
createTag('a', { href: `${path}` }, '', { parent: root });
const modal = await getModal(null, {
id: 'checkout-link-modal',
content: root,
closeEvent: 'closeModal',
class: 'commerce-frame',
});
await loadArea(modal);
root.style.visibility = '';
return modal;
}
export function appendTabName(url) {
const metaPreselectPlan = document.querySelector('meta[name="preselect-plan"]');
if (!metaPreselectPlan?.content) return url;
let urlWithPlan;
try {
urlWithPlan = new URL(url);
} catch (err) {
window.lana?.log(`Invalid URL ${url} : ${err}`);
return url;
}
urlWithPlan.searchParams.set('plan', metaPreselectPlan.content);
return urlWithPlan.href;
}
export function appendExtraOptions(url, extraOptions) {
if (!extraOptions) return url;
const extraOptionsObj = JSON.parse(extraOptions);
let urlWithExtraOptions;
try {
urlWithExtraOptions = new URL(url);
} catch (err) {
window.lana?.log(`Invalid URL ${url} : ${err}`);
return url;
}
Object.keys(extraOptionsObj).forEach((key) => {
if (CHECKOUT_ALLOWED_KEYS.includes(key)) {
urlWithExtraOptions.searchParams.set(key, extraOptionsObj[key]);
}
});
return urlWithExtraOptions.href;
}
async function openExternalModal(url, getModal, extraOptions) {
await loadStyle(`${getConfig().base}/blocks/iframe/iframe.css`);
const root = createTag('div', { class: 'milo-iframe' });
const urlWithTabName = appendTabName(url);
const urlWithExtraOptions = appendExtraOptions(urlWithTabName, extraOptions);
createTag('iframe', {
src: urlWithExtraOptions,
frameborder: '0',
marginwidth: '0',
marginheight: '0',
allowfullscreen: 'true',
loading: 'lazy',
}, '', { parent: root });
return getModal(null, {
id: 'checkout-link-modal',
content: root,
closeEvent: 'closeModal',
class: 'commerce-frame',
});
}
const isInternalModal = (url) => /\/fragments\//.test(url);
export async function openModal(e, url, offerType, hash, extraOptions) {
e.preventDefault();
e.stopImmediatePropagation();
const { getModal } = await import('../modal/modal.js');
await import('../modal/modal.merch.js');
const offerTypeClass = offerType === OFFER_TYPE_TRIAL ? 'twp' : 'crm';
let modal;
if (hash) {
const prevHash = window.location.hash.replace('#', '') === hash ? '' : window.location.hash;
window.location.hash = hash;
window.addEventListener('milo:modal:closed', () => {
window.history.pushState({}, document.title, `#${prevHash}`);
}, { once: true });
}
if (isInternalModal(url)) {
const fragmentPath = url.split(/(hlx|aem).(page|live)/).pop();
modal = await openFragmentModal(fragmentPath, getModal);
} else {
modal = await openExternalModal(url, getModal, extraOptions);
}
if (modal) {
modal.classList.add(offerTypeClass);
}
}
export function setCtaHash(el, checkoutLinkConfig, offerType) {
if (!(el && checkoutLinkConfig && offerType)) return undefined;
const hash = checkoutLinkConfig[`${(offerType === OFFER_TYPE_TRIAL) ? FREE_TRIAL_HASH : BUY_NOW_HASH}`];
if (hash) {
el.setAttribute('data-modal-id', hash);
}
return hash;
}
const isProdModal = (url) => {
try {
return (new URL(url)).hostname.endsWith('.adobe.com');
} catch (e) {
return false;
}
};
export async function getModalAction(offers, options, el) {
const [{
offerType,
productArrangementCode,
productArrangement: { productCode, productFamily: offerFamily } = {},
}] = offers ?? [{}];
if (options.modal !== true) return undefined;
const checkoutLinkConfig = await getCheckoutLinkConfig(
offerFamily,
productCode,
productArrangementCode,
);
if (!checkoutLinkConfig) return undefined;
const columnName = (offerType === OFFER_TYPE_TRIAL) ? FREE_TRIAL_PATH : BUY_NOW_PATH;
const hash = setCtaHash(el, checkoutLinkConfig, offerType);
let url = checkoutLinkConfig[columnName];
if (!url) return undefined;
url = isInternalModal(url) || isProdModal(url)
? localizeLink(checkoutLinkConfig[columnName]) : checkoutLinkConfig[columnName];
return { url, handler: (e) => openModal(e, url, offerType, hash, options.extraOptions) };
}
export async function getCheckoutAction(offers, options, imsSignedInPromise, el) {
try {
await imsSignedInPromise;
const [downloadAction, upgradeAction, modalAction] = await Promise.all([
getDownloadAction(options, imsSignedInPromise, offers),
getUpgradeAction(options, imsSignedInPromise, offers),
getModalAction(offers, options, el),
]);
return downloadAction || upgradeAction || modalAction;
} catch (e) {
log?.error('Failed to resolve checkout action', e);
return [];
}
}
/**
* Activates commerce service and returns a promise resolving to its ready-to-use instance.
*/
export async function initService(force = false, attributes = {}) {
if (force) {
initService.promise = undefined;
document.head.querySelector('mas-commerce-service')?.remove();
fetchEntitlements.promise = undefined;
fetchCheckoutLinkConfigs.promise = undefined;
}
const { commerce, env: miloEnv, locale: miloLocale } = getConfig();
const extraAttrs = [
'checkout-workflow-step',
'force-tax-exclusive',
'checkout-client-id',
'allow-override',
];
extraAttrs.forEach((attr) => {
const camelCaseAttr = attr.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
// eslint-disable-next-line no-prototype-builtins
if (commerce?.hasOwnProperty(camelCaseAttr)) {
const value = commerce[camelCaseAttr];
delete commerce[camelCaseAttr];
commerce[attr] = value;
}
});
initService.promise = initService.promise ?? polyfills().then(async () => {
await import('../../deps/mas/commerce.js');
const { language, locale, country } = getMiloLocaleSettings(miloLocale);
let service = document.head.querySelector('mas-commerce-service');
if (!service) {
service = createTag('mas-commerce-service', {
locale,
language,
...attributes,
...commerce,
});
if (miloEnv?.name !== 'prod') {
service.setAttribute('allow-override', '');
}
service.registerCheckoutAction(getCheckoutAction);
document.head.append(service);
await service.readyPromise;
service.imsSignedInPromise?.then((isSignedIn) => {
if (isSignedIn) fetchEntitlements();
});
}
if (country === 'AU') {
await loadStyle(`${getConfig().base}/blocks/merch/au-merch.css`);
}
return service;
});
return initService.promise;
}
export async function getCommerceContext(el, params) {
const wcsOsi = params.get('osi');
if (!wcsOsi) return null;
const perpetual = params.get('perp') === 'true' || undefined;
const promotionCode = (
params.get('promo') ?? params.get('promotionCode') ?? el.closest('[data-promotion-code]')?.dataset.promotionCode
) || undefined;
return { promotionCode, perpetual, wcsOsi };
}
/**
* Checkout parameter can be set on the merch link,
* code config (scripts.js) or be a default from tacocat.
* To get the default, 'undefined' should be passed, empty string will trigger an error!
*
* clientId - code config -> default (adobe_com)
* workflow - merch link -> metadata -> default (UCv3)
* workflowStep - merch link -> default (email)
* marketSegment - merch link -> default (COM)
* @param {{params: URLSearchParams, service: {settings: object}}} context
* @returns checkout context object required to build a checkout url
*/
export async function getCheckoutContext(el, params) {
const context = await getCommerceContext(el, params);
if (!context) return null;
const { settings } = await initService();
const { checkoutClientId } = settings;
const checkoutMarketSegment = params.get('marketSegment');
const checkoutWorkflow = params.get('workflow') ?? settings.checkoutWorkflow;
const checkoutWorkflowStep = params?.get('workflowStep') ?? settings.checkoutWorkflowStep;
const entitlement = params?.get('entitlement');
const upgrade = params?.get('upgrade');
const modal = params?.get('modal');
const extraOptions = {};
params.forEach((value, key) => {
if (CHECKOUT_ALLOWED_KEYS.includes(key)) {
extraOptions[key] = value;
}
});
return {
...context,
checkoutClientId,
checkoutWorkflow,
checkoutWorkflowStep,
checkoutMarketSegment,
entitlement,
upgrade,
modal,
extraOptions: JSON.stringify(extraOptions),
};
}
export async function getPriceContext(el, params) {
const context = await getCommerceContext(el, params);
if (!context) return null;
const displayOldPrice = context.promotionCode ? params.get('old') : undefined;
const displayPerUnit = params.get('seat');
const displayRecurrence = params.get('term');
const displayTax = params.get('tax');
const forceTaxExclusive = params.get('exclusive');
// The PRICE_TEMPLATE_MAPPING supports legacy OST links
const template = PRICE_TEMPLATE_MAPPING.get(params.get('type')) ?? PRICE_TEMPLATE_REGULAR;
return {
...context,
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
template,
};
}
export function reopenModal(cta) {
if (cta && cta.getAttribute('data-modal-id') === window.location.hash.replace('#', '')) {
cta.click();
}
}
export async function buildCta(el, params) {
const large = !!el.closest('.marquee');
const strong = el.firstElementChild?.tagName === 'STRONG' || el.parentElement?.tagName === 'STRONG';
if (el.closest('.merch-offers.upgrade')) {
params.append('entitlement', 'false');
}
const context = await getCheckoutContext(el, params);
if (!context) return null;
const service = await initService();
const text = el.textContent?.replace(/^CTA +/, '');
const cta = service.createCheckoutLink(context, text);
if (el.href.includes('#_tcl')) {
el.href = el.href.replace('#_tcl', '');
} else {
cta.classList.add('con-button');
cta.classList.toggle('button-l', large);
cta.classList.toggle('blue', strong);
}
if (context.entitlement !== 'false') {
cta.classList.add(LOADING_ENTITLEMENTS);
cta.onceSettled().finally(() => {
cta.classList.remove(LOADING_ENTITLEMENTS);
// after opening a modal, navigating to another page and back we need to reopen the modal
reopenModal(cta);
});
}
return cta;
}
async function buildPrice(el, params) {
const context = await getPriceContext(el, params);
if (!context) return null;
const service = await initService();
const price = service.createInlinePrice(context);
return price;
}
export default async function init(el) {
if (!el?.classList?.contains('merch')) return undefined;
const { searchParams } = new URL(el.href);
const isCta = searchParams.get('type') === 'checkoutUrl';
const merch = await (isCta ? buildCta : buildPrice)(el, searchParams);
const service = await initService();
log = service.Log.module('merch');
if (merch) {
log.debug('Rendering:', { options: { ...merch.dataset }, merch, el });
el.replaceWith(merch);
return merch;
}
log.warn('Failed to get context:', { el });
return null;
}
export async function handleHashChange() {
const modalDialog = document.querySelector('.dialog-modal');
if (window.location.hash) {
const modalId = window.location.hash.replace('#', '');
const cta = document.querySelector(`.con-button[data-modal-id="${modalId}"]`);
if (!modalDialog) {
reopenModal(cta);
}
} else if (modalDialog) {
const { closeModal } = await import('../modal/modal.js');
closeModal(modalDialog);
}
}
window.addEventListener('hashchange', handleHashChange);