PageSourceSearch

https://www.simplytoimpress.com/scripts/promo_code_error.js?8fd0e3000d13049074f57950713fac835df4f6d2

js simplytoimpress.com collected 2026-09-26 08:26:28 UTC 6,711 bytes, 226 lines download raw bytes

1(function (window, $) {
2    'use strict';
3
4    const INTERACTIVE_CODES = {
5        INTERACTIVE_REPLACE: true,
6        INTERACTIVE_CONFIRM: true,
7        INTERACTIVE_SPECIAL: true
8    };
9
10    function getPromoInputSelectors() {
11        return '#promo_code, #discountText, #discountTextBox, input[name=discountText]:visible';
12    }
13
14    function findPromoErrorElement($input) {
15        if (!$input || !$input.length) {
16            return $();
17        }
18
19        const $field = $input.closest('.promo-code-field');
20        if ($field.length) {
21            return $field.children('.promo-code-error').first();
22        }
23
24        return $();
25    }
26
27    function clearPromoCodeInlineError($scope) {
28        const $root = $scope && $scope.length ? $scope : $(document);
29        $root.find('.promo-code-error').text('').hide();
30    }
31
32    function showPromoCodeInlineError(message, $input) {
33        if (!message) {
34            return;
35        }
36
37        const $targetInput = $input && $input.length ? $input : $(getPromoInputSelectors()).first();
38        if (!$targetInput.length) {
39            return;
40        }
41
42        const $error = findPromoErrorElement($targetInput);
43        if (!$error.length) {
44            return;
45        }
46
47        $error.text(message).show();
48    }
49
50    function isStructuredPromoResponse(payload) {
51        return !!(
52            payload
53            && typeof payload.success !== 'undefined'
54            && payload.error !== undefined
55            && payload.error !== null
56            && typeof payload.error === 'object'
57        );
58    }
59
60    function normalizePromoCodeResponse(source) {
61        if (!source) {
62            return null;
63        }
64
65        // Direct promo envelope: { success, error: { code, message, details } }
66        if (isStructuredPromoResponse(source)) {
67            return source;
68        }
69
70        // Micro API wrapper: { status, msg, data: { success, error } }
71        if (source.data && isStructuredPromoResponse(source.data)) {
72            return source.data;
73        }
74
75        // Do not treat generic cart failures (qty/remove/etc.) as promo errors.
76        return null;
77    }
78
79    function getPromoCodeErrorDetails(response) {
80        if (!response || response.success !== false || !response.error) {
81            return null;
82        }
83
84        return response.error.details || null;
85    }
86
87    function getPromoCodeErrorCode(response) {
88        if (!response || response.success !== false || !response.error) {
89            return '';
90        }
91
92        return response.error.code || '';
93    }
94
95    function parseSpecialPromoPayload(response, message) {
96        var details = getPromoCodeErrorDetails(response);
97        if (details && (details.message || details.buttons)) {
98            return {
99                message: details.message || message || '',
100                buttons: details.buttons || []
101            };
102        }
103
104        if (typeof message === 'string' && message.charAt(0) === '{') {
105            try {
106                var parsed = JSON.parse(message);
107                if (parsed && (parsed.message || parsed.buttons)) {
108                    return {
109                        message: parsed.message || '',
110                        buttons: parsed.buttons || []
111                    };
112                }
113            } catch (e) {
114                // fall through
115            }
116        }
117
118        if (message && typeof message === 'object') {
119            return {
120                message: message.message || '',
121                buttons: message.buttons || []
122            };
123        }
124
125        return {
126            message: message || '',
127            buttons: []
128        };
129    }
130
131    function isInteractivePromoCodeError(response) {
132        return !!INTERACTIVE_CODES[getPromoCodeErrorCode(response)];
133    }
134
135    function isReplacePromoCodeError(response) {
136        return getPromoCodeErrorCode(response) === 'INTERACTIVE_REPLACE';
137    }
138
139    function isConfirmPromoCodeError(response) {
140        return getPromoCodeErrorCode(response) === 'INTERACTIVE_CONFIRM';
141    }
142
143    function isSpecialPromoCodeError(response) {
144        return getPromoCodeErrorCode(response) === 'INTERACTIVE_SPECIAL';
145    }
146
147    function getPromoCodeErrorMessage(response) {
148        if (!response || response.success !== false || !response.error) {
149            return '';
150        }
151
152        return response.error.message || '';
153    }
154
155    function handlePromoCodeValidationFailure(response, $input) {
156        if (!response || response.success !== false) {
157            return false;
158        }
159
160        if (isInteractivePromoCodeError(response)) {
161            return false;
162        }
163
164        showPromoCodeInlineError(getPromoCodeErrorMessage(response), $input);
165        return findPromoErrorElement($input && $input.length ? $input : $(getPromoInputSelectors()).first()).length > 0;
166    }
167
168    function bindClearErrorWhenInputEmpty() {
169        $(document).on('input', getPromoInputSelectors(), function () {
170            if (!$.trim($(this).val())) {
171                findPromoErrorElement($(this)).text('').hide();
172            }
173        });
174    }
175
176    function clearPromoCodeInput($input) {
177        const $targetInput = $input && $input.length ? $input : $(getPromoInputSelectors());
178        $targetInput.val('');
179    }
180
181    function parseCartHandlerPromoResponse(data) {
182        if (!data) {
183            return null;
184        }
185
186        if (typeof data === 'object') {
187            return normalizePromoCodeResponse(data);
188        }
189
190        try {
191            const parsed = JSON.parse(data);
192            return normalizePromoCodeResponse(parsed);
193        } catch (e) {
194            return {
195                success: false,
196                error: {
197                    code: 'UNKNOWN',
198                    message: data,
199                    details: {}
200                }
201            };
202        }
203    }
204
205    $(function () {
206        bindClearErrorWhenInputEmpty();
207    });
208
209    window.PromoCodeError = {
210        clear: clearPromoCodeInlineError,
211        show: showPromoCodeInlineError,
212        clearInput: clearPromoCodeInput,
213        normalize: normalizePromoCodeResponse,
214        getCode: getPromoCodeErrorCode,
215        getDetails: getPromoCodeErrorDetails,
216        parseSpecialPayload: parseSpecialPromoPayload,
217        isInteractive: isInteractivePromoCodeError,
218        isReplace: isReplacePromoCodeError,
219        isConfirm: isConfirmPromoCodeError,
220        isSpecial: isSpecialPromoCodeError,
221        getMessage: getPromoCodeErrorMessage,
222        handleValidationFailure: handlePromoCodeValidationFailure,
223        parseCartHandlerResponse: parseCartHandlerPromoResponse,
224        bindClearErrorWhenInputEmpty: bindClearErrorWhenInputEmpty
225    };
226})(window, window.jQuery);

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.