PageSourceSearch

https://apps.alzfdn.org/html/common/themes/sdk_apple.js

js alzfdn.org collected 2026-10-01 10:43:45 UTC 7,565 bytes, 272 lines download raw bytes

1(function () {
2	'use strict';
3
4	loadScript('https://applepay.cdn-apple.com/jsapi/v1.1.0/apple-pay-sdk.js');
5
6	if (!document.getElementById("anetApplePayDialogEmbed")) {
7		document.body.insertAdjacentHTML(
8			"beforeend",
9			'<div id="anetApplePayDialogEmbed" title="Payment" style="display:none;"> <div id="confirmText"></div>' +
10			'<div class="wallet_pop_dialog" ><span>Amount</span><div id="confirmAmount" class="wallet_pop_amount"></div></div>' +
11			'<div id="apple-pay-button"></div>' +
12			'<button icon="ui-icon-close"class="ui-confirmdialog-no border0  bg2 coinv margin05em" onclick="$(\'#anetApplePayDialogEmbed\').dialog(\'close\');">Cancel</button>' +
13			'</div>'
14		);
15	}
16
17	var jq = jQuery.noConflict(true);
18	jq("#anetApplePayDialogEmbed").dialog({
19		modal: true,
20		autoOpen: false,
21		width: 500,
22		appendTo: "body",
23		resizable: false,
24		position: {
25			my: "center",
26			at: "center",
27			of: window
28		},
29		closeOnEscape: true,
30		open: function () {
31			var $dialog = $(this).dialog("widget");
32
33			var top = Math.max(
34				0,
35				(window.innerHeight - $dialog.outerHeight()) / 2
36			);
37
38			var left = Math.max(
39				0,
40				(window.innerWidth - $dialog.outerWidth()) / 2
41			);
42
43			$dialog.css({
44				position: "fixed",
45				top: top + "px",
46				left: left + "px"
47			});
48		}
49	});
50
51
52	// Prevent double load
53	if (window.AppleCheckout) {
54		return;
55	}
56
57	const NAMESPACE = 'ANET_CHECKOUT_V1';
58
59	var embedCode = document.getElementById("embed_js").src;
60	var instance = embedCode.substring(0, embedCode.indexOf("/html"));
61	var initiativeContext = window.top.location.origin;
62
63	/* =====================================================
64	   Public API
65	   ===================================================== */
66	window.AppleCheckout = {
67		start: start
68	};
69
70	// Global config accessible to all functions in this module
71	let appleConfig = null;
72
73	/**
74	 * Entry point
75	 * Same call works for direct and embedded flows
76	 */
77	function start(config) {
78		// store config globally so other functions can access it
79		appleConfig = config || appleConfig;
80		// expose current config on the public API as well
81		window.AppleCheckout.config = appleConfig;
82		if (isTopWindow()) {
83			initializeApplePayButton();
84		} else {
85			requestTopWindowApple(appleConfig);
86		}
87	}
88
89
90	function initializeApplePayButton() {
91		const applePayButtonElement = document.getElementById('apple-pay-button');
92
93		applePayButtonElement.innerHTML = '';
94		const applePayButton = document.createElement('apple-pay-button');
95		applePayButton.setAttribute('buttonstyle', 'black');
96		applePayButton.setAttribute('type', 'pay');
97		applePayButton.setAttribute('locale', 'en-US');
98		applePayButton.setAttribute('aria-hidden', 'true');
99		applePayButton.setAttribute('lang', 'en-US');
100
101		applePayButtonElement.appendChild(applePayButton);
102
103		applePayButton.addEventListener('click', function (event) {
104			event.preventDefault();
105			beginApplePayment();
106		});
107
108		applePayButtonElement.style.display = 'block';
109
110		$("#anetApplePayDialogEmbed").dialog("option", "title", appleConfig.confirmTitle);
111		$("#confirmText").html(appleConfig.confirmText);
112		const raw = parseFloat(appleConfig.amount);
113		const formatted = raw.toLocaleString("en-US", {
114			style: "currency",
115			currency: "USD"
116		});
117		$("#confirmAmount").html(formatted);
118		$("#anetApplePayDialogEmbed").dialog('open');
119	}
120
121	/* =====================================================
122	   Direct flow (executed ONLY in top window)
123	   ===================================================== */
124	function beginApplePayment() {
125		// Create the Apple Pay payment request as appropriate.
126		var paymentRequest = {
127			countryCode: 'US',
128			currencyCode: 'USD',
129			merchantCapabilities: ['supports3DS', 'supportsCredit', 'supportsDebit'],
130			supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
131			total: { label: 'Payment', amount: appleConfig.amount },
132		};
133
134		// Create the Apple Pay session.
135		var session = new ApplePaySession(7, paymentRequest);
136
137		// Setup handler for validation the merchant session.
138		session.onvalidatemerchant = function (event) {
139			console.log("Validating merchant with event:", event);
140
141			// Create the payload.
142			var data = {
143				validationUrl: event.validationURL,
144				companyId: appleConfig.companyId,
145				initiativeContext: initiativeContext
146			};
147
148			// Post the payload to the server to validate the
149			// merchant session using the merchant certificate.
150			$.ajax({
151				url: instance + appleConfig.url,
152				method: "POST",
153				contentType: "application/json; charset=utf-8",
154				data: JSON.stringify(data),
155			}).then(function (merchantSession) {
156				// Complete validation by passing the merchant session to the Apple Pay session.
157				console.log("Merchant session validated:", merchantSession);
158				session.completeMerchantValidation(merchantSession);
159			}).catch(function (error) {
160				console.error("Error validating merchant:", error);
161				session.abort();
162				alert("Error validating merchant:" + error.responseText);
163			});
164		};
165
166		// Setup handler to receive the token when payment is authorized.
167		session.onpaymentauthorized = function (event) {
168			// Get the payment data for use to capture funds from
169			// the encrypted Apple Pay token in your server.
170			var paymentToken = event.payment.token.paymentData;
171
172			var authorizationResult = {
173				status: ApplePaySession.STATUS_SUCCESS,
174				errors: []
175			};
176
177			const payload = { paymentToken: JSON.stringify(paymentToken) }
178			notifyIframes('PAYMENT_SUCCESS', payload);
179			$("#anetApplePayDialogEmbed").dialog('close');
180
181			session.completePayment(authorizationResult);
182		};
183
184		// Start the session to display the Apple Pay sheet.
185		session.begin();
186	}
187
188	/* =====================================================
189	   Embedded flow (iframe → top window)
190	   ===================================================== */
191	function requestTopWindowApple(config) {
192		window.parent.postMessage({
193			ns: NAMESPACE,
194			type: 'ANET_APPLE_START_PAYMENT',
195			payload: config
196		}, '*');
197	}
198
199
200
201	window.addEventListener('message', function (event) {
202
203		if (!event.data || typeof event.data !== 'object') {
204			return;
205		}
206
207		if (event.data.ns !== 'ANET_CHECKOUT_V1') {
208			return;
209		}
210
211		if (event.data.type === 'ANET_APPLE_START_PAYMENT') {
212			appleConfig = event.data.payload || appleConfig;
213			window.AppleCheckout.config = appleConfig;
214			initializeApplePayButton();
215		}
216	});
217
218	/* =====================================================
219	   Utilities
220	   ===================================================== */
221	function isTopWindow() {
222		try {
223			return window === window.parent;
224		} catch (e) {
225			return false;
226		}
227	}
228
229	function notifyIframes(type, payload) {
230		const message = {
231			ns: NAMESPACE,
232			type: type,
233			payload: payload
234		};
235
236		const iframes = document.getElementsByTagName('iframe');
237
238		for (let i = 0; i < iframes.length; i++) {
239			try {
240				iframes[i].contentWindow.postMessage(message, '*');
241			} catch (e) {
242				// Ignore cross-origin access errors
243			}
244		}
245	}
246
247	// Generic script loader with deduplication and callback
248	function loadScript(src, callback) {
249		// Avoid loading the same script twice
250		if (document.querySelector('script[src="' + src + '"]')) {
251			if (callback) callback();
252			return;
253		}
254
255		const script = document.createElement('script');
256		script.src = src;
257		script.type = 'text/javascript';
258		script.async = false; // keep execution order
259
260		script.onload = function () {
261			if (callback) callback();
262		};
263
264		script.onerror = function () {
265			console.error('Failed to load script:', src);
266		};
267
268		document.head.appendChild(script);
269	}
270
271
272})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.