1( function( $ ) { 2 var SimplePayment = { 3 _setup: false, 4 params: {op: 'purchase', classname: 'needs-validation'}, 5 _validations: [], 6 _form: null, 7 bootstrap: null, 8 9 init: function(params) { 10 if (SimplePayment.bootstrap == null) SimplePayment.bootstrap = (typeof $().modal == 'function'); 11 if (typeof(params) !== 'undefined' && params) SimplePayment.params = Object.assign({}, SimplePayment.params, params); 12 13 if ($(document).triggerHandler( 'simple_payment_init' ) === false ) return(this); 14 15 window.addEventListener('load', function () { 16 if (typeof(sp_settings) !== 'undefined') SimplePayment.params = Object.assign({}, SimplePayment.params, sp_settings); 17 var btn = $("[sp-data='checkout']"); 18 var target = null; 19 if (btn.length) btn.bind('click', function () { 20 SimplePayment.submit(); 21 }); else if (SimplePayment.params['display'] == 'modal') { 22 target = SimplePayment.params['target']; 23 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 24 SimplePayment.modal(target); 25 } else if (SimplePayment.params['display'] == 'iframe') { 26 target = SimplePayment.params['target']; 27 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 28 SimplePayment.submit(); 29 } 30 31 // Loop over them and prevent submission 32 var forms = document.getElementsByClassName(SimplePayment.params['classname']); 33 Array.prototype.filter.call(forms, function (form) { 34 if (target && !$(form).attr('target')) $(form).attr('target', target); 35 form.addEventListener('submit', function (event) { 36 SimplePayment.validate(); 37 if (form.checkValidity() === false) { 38 form.classList.add('was-validated'); 39 event.preventDefault() 40 event.stopPropagation(); 41 return(false); 42 } 43 form.classList.add( 'validated' ); 44 SimplePayment.pre(target); 45 }, false); 46 }); 47 }, false); 48 return(this); 49 }, 50 51 settings: function(params) { 52 if (typeof(params) !== 'undefined' && params) SimplePayment.params = Object.assign({}, SimplePayment.params, params); 53 }, 54 55 filter: function(input, fn) { 56 if (!input) return; 57 ["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function(event) { 58 input.oldValue = ""; 59 input.addEventListener(event, function() { 60 if (fn(this.value)) { 61 this.oldValue = this.value; 62 this.oldSelectionStart = this.selectionStart; 63 this.oldSelectionEnd = this.selectionEnd; 64 } else if (this.hasOwnProperty("oldValue")) { 65 this.value = this.oldValue; 66 this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd); 67 } 68 }); 69 }); 70 }, 71 72 validation: function(type, ele, fn) { 73 if (!ele) return; 74 this._validations[type] = [ele, fn]; 75 }, 76 77 validExpiry: function(month, year) { 78 if (!month && !year) return(true); 79 var now = new Date(); 80 now.setHours(0,0,0,0); 81 var expiry = new Date(year, month, 0); 82 expiry.setHours(0,0,0,0); 83 return(now <= expiry); 84 }, 85 86 validCard: function(number) { 87 if (!number) return(true); 88 var regex = new RegExp("^[0-9]{16}$"); 89 return(regex.test(number) && SimplePayment.luhn(number)); 90 }, 91 92 luhn: function(val) { 93 var sum = 0; 94 for (var i = 0; i < val.length; i++) { 95 var intVal = parseInt(val.substr(i, 1)); 96 if (i % 2 == 0) { 97 intVal *= 2; 98 if (intVal > 9) { 99 intVal = 1 + (intVal % 10); 100 } 101 } 102 sum += intVal; 103 } 104 return((sum % 10) == 0); 105 }, 106 107 validate: function(event) { 108 var valid = true; 109 Object.values(this._validations).forEach(validation => { 110 var ele = validation[0]; 111 var fn = validation[1]; 112 if (ele && fn) { 113 var passed = fn(ele); 114 ele = Array.isArray(ele) ? ele[0] : ele; 115 ele.setCustomValidity(!passed ? 'Failed Validation' : ''); 116 valid = valid && passed; 117 } 118 }); 119 return(valid); 120 }, 121 122 form: function(elem) { 123 this._form = elem; 124 }, 125 126 setup: function() { 127 var inputs = $("[sp-data]"); 128 for (var i = 0; i < inputs.length; i++) { 129 var type = $(inputs[i]).attr('type'); 130 var value = $(inputs[i]).val(); 131 if (type == 'checkbox') { 132 value = $(inputs[i]).attr('checked') ? value : null 133 } 134 this.params[$(inputs[i]).attr('sp-data')] = value; 135 } 136 return(this._setup = true); 137 }, 138 139 modal: function(target, _url) { 140 var _modal; 141 var _url = typeof(_url) !== 'undefined' && _url ? _url : 'about:blank'; 142 143 if (SimplePayment.bootstrap) {
144 _modal = $('<div class="modal" tabindex="-1" role="dialog" sp-data="modal" ' + (SimplePayment.params['modal_disable_close'] ? 'data-backdrop="static" data-keyboard="false"' : '') + 'aria-labelledby="" aria-hidden="true"></div>'); 145 _modal.append('<div class="modal-dialog modal-dialog-centered" role="document">' 146 + '<div class="modal-content"><div class="modal-body"><div class="embed-responsive embed-responsive-1by1">' 147 + '<iframe name="' + (typeof(target) !== 'undefined' && target ? target : SimplePayment.params['target']) + '" src="' + _url + '" allowpaymentrequest="true" allow="payment" class="embed-responsive-item h100 w100"></iframe>' 148 + '</div></div></div></div>'); 149 } else { 150 _modal = $('<div class="sp-legacy-modal" tabindex="-1" role="dialog" sp-data="modal" aria-labelledby="" aria-hidden="true"></div>'); 151 _modal.append('<div class="sp-modal-dialog" role="document">' 152 + (SimplePayment.params['modal_disable_close'] ? '' : '<a href="javascript:SimplePayment.close(' + (target ? "'" + target + "'" : '') + ');" class="sp-close">X</a>') 153 + '<iframe name="' + (typeof(target) !== 'undefined' && target ? target : SimplePayment.params['target']) + '" allowpaymentrequest="true" allow="payment" src="' + _url + '"></iframe>' 154 + '</div>'); 155 } 156 $('body').append(_modal); 157 return($('[sp-data="modal"]')); 158 }, 159 160 pre: function(target, _url) { 161 $(document).triggerHandler( 'simple_payment_pre_submit' ) 162 var target = typeof(target) !== 'undefined' && target ? target : SimplePayment.params['target']; 163 var _url = typeof(_url) !== 'undefined' && _url ? _url : 'about:blank'; 164 if (SimplePayment.params['display'] == 'iframe') { 165 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 166 var iframe = $('[name="' + target + '"]'); 167 if (!iframe.length) $('[sp-data="container"]').append('<iframe name="' + target + '" src="'+ _url + '" allowpaymentrequest="true" allow="payment" sp-data="iframe"></iframe>'); 168 $('[name="' + target + '"]').closest(':hidden').show(); 169 } 170 if (SimplePayment.params['display'] == 'modal') { 171 if (!jQuery.fn.modal) { 172 // We do not have bootstrap, or old and it is not supporting modal 173 jQuery.fn.extend({ 174 modal: function( action ) { 175 return this.each(function() { 176 if (action == 'hide') jQuery(this).hide(); 177 else jQuery(this).show(); 178 }) 179 }, 180 }); 181 } 182 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 183 var modal = SimplePayment.params['modal'] ? jQuery(SimplePayment.params['modal']) : jQuery('[name="' + target + '"]').closest('[sp-data="modal"]'); 184 if (!modal || modal.length == 0) { 185 modal = this.modal(target, _url); 186 } 187 modal.modal('show'); 188 } 189 }, 190 191 close: function(target) { 192 if (SimplePayment.params['modal']) jQuery(SimplePayment.params['modal']).modal('hide'); 193 else { 194 var target = typeof(target) !== 'undefined' && target ? target : SimplePayment.params['target']; 195 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 196 jQuery('[name="' + target + '"]').closest('[sp-data="modal"]').modal('hide'); 197 } 198 }, 199 200 show: function(_url, target) { 201 this.pre(target, _url); 202 }, 203 204 submit: function(params, target) { 205 if (typeof(params) !== 'undefined' && params) this.init(params); 206 if (!this._setup) this.setup(); 207 var target = typeof(target) !== 'undefined' && target ? target : SimplePayment.params['target']; 208 if (!this._form) { 209 target = SimplePayment.params['type'] == 'hidden' || !target ? 'sp-frame' : target; 210 this._form = $('<form method="post" target="' + target + '" action="' + (SimplePayment.params['callback'] ? SimplePayment.params['callback'] : '') + '"></form>'); 211 } else this._form.empty(); 212 this._form._submit_function_ = this._form.submit;
213 Object.keys(this.params).forEach(key => { 214 this._form.append('<input type="hidden" name="' + key + '" value="' + (this.params[key] ? this.params[key] : '') + '" />'); 215 }); 216 $('body').append(this._form); 217 this.pre( target ); 218 this._form._submit_function_(); 219 } 220 }; 221 this.SimplePayment = SimplePayment.init(typeof(sp_settings) !== 'undefined' && sp_settings ? sp_settings : null); 222})(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.