|
1
|
|
|
// make sure ajaxurl is defined |
|
2
|
|
|
if (typeof ajaxurl === 'undefined' || ajaxurl === null) { |
|
|
|
|
|
|
3
|
|
|
// variable is undefined or null |
|
4
|
|
|
ajaxurl = WPInv.ajax_url; |
|
|
|
|
|
|
5
|
|
|
} |
|
6
|
|
|
window.wpiSubmit = typeof window.wpiSubmit !== 'undefined' ? window.wpiSubmit : true; |
|
7
|
|
|
jQuery(function($) { |
|
8
|
|
|
var valid = false; |
|
9
|
|
|
$('#wpinv_checkout_form').on('submit', function(e) { |
|
10
|
|
|
var $form = $(this).closest('#wpinv_checkout_form'); |
|
11
|
|
|
$('.wpinv_errors').remove(); |
|
12
|
|
|
if (valid) { |
|
13
|
|
|
return true; |
|
14
|
|
|
} |
|
15
|
|
|
var fields = ['first_name', 'email', 'address', 'city', 'country', 'state']; |
|
16
|
|
|
var err = []; |
|
17
|
|
|
$.each(fields, function(i, field) { |
|
18
|
|
|
if ($('#wpinv_' + field).length && !$('#wpinv_' + field).val()) { |
|
19
|
|
|
err.push(field); |
|
20
|
|
|
} |
|
21
|
|
|
}); |
|
22
|
|
|
if (err && err.length > 0) { |
|
23
|
|
|
$('#wpinv_' + err[0]).focus(); |
|
24
|
|
|
return false; |
|
25
|
|
|
} else { |
|
26
|
|
|
e.preventDefault(); |
|
27
|
|
|
wpinvBlock($form); |
|
28
|
|
|
var data = $form.serialize(); |
|
29
|
|
|
data = wpinvRemoveQueryVar(data, 'action'); |
|
30
|
|
|
data = wpinvRemoveQueryVar(data, 'wpinv_ajax'); |
|
31
|
|
|
$.post(ajaxurl, data + '&action=wpinv_checkout', function(res) { |
|
|
|
|
|
|
32
|
|
|
if (res && typeof res == 'object' && res.success) { |
|
33
|
|
|
valid = true; |
|
34
|
|
|
var data = new Object(); |
|
35
|
|
|
data.form = $form; |
|
36
|
|
|
data.totals = res.data; |
|
37
|
|
|
jQuery('body').trigger('wpinv_checkout_submit', data); |
|
38
|
|
|
if (window.wpiSubmit) { |
|
39
|
|
|
$form.submit(); |
|
40
|
|
|
} |
|
41
|
|
|
} else { |
|
42
|
|
|
$form.unblock(); |
|
43
|
|
|
if (res && res.search("wpinv_adddress_confirm") !== -1) { |
|
44
|
|
|
$('#wpinv_adddress_confirm').show(); |
|
45
|
|
|
} |
|
46
|
|
|
$('#wpinv_purchase_submit', $form).before(res); |
|
47
|
|
|
} |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
return false; |
|
51
|
|
|
}); |
|
52
|
|
|
var elB = $('#wpinv-fields'); |
|
53
|
|
|
$('#wpinv_country', elB).change(function(e) { |
|
|
|
|
|
|
54
|
|
|
$('.wpinv_errors').remove(); |
|
55
|
|
|
wpinvBlock(jQuery('#wpinv_state_box')); |
|
56
|
|
|
var $this = $(this); |
|
|
|
|
|
|
57
|
|
|
data = { |
|
|
|
|
|
|
58
|
|
|
action: 'wpinv_get_states_field', |
|
59
|
|
|
country: $(this).val(), |
|
60
|
|
|
field_name: 'wpinv_state', |
|
61
|
|
|
}; |
|
62
|
|
|
$.post(ajaxurl, data, function(response) { |
|
|
|
|
|
|
63
|
|
|
if ('nostates' === response) { |
|
64
|
|
|
var text_field = '<input type="text" required="required" class="wpi-input required" id="wpinv_state" name="wpinv_state">'; |
|
65
|
|
|
$('#wpinv_state', elB).replaceWith(text_field); |
|
66
|
|
|
} else { |
|
67
|
|
|
$('#wpinv_state', elB).replaceWith(response); |
|
68
|
|
|
var changeState = function() { |
|
69
|
|
|
console.log('69 : wpinv_recalculate_taxes(' + $(this).val() + ')'); |
|
|
|
|
|
|
70
|
|
|
wpinv_recalculate_taxes($(this).val()); |
|
71
|
|
|
}; |
|
72
|
|
|
$("#wpinv_state").unbind("change", changeState); |
|
73
|
|
|
$("#wpinv_state").bind("change", changeState); |
|
74
|
|
|
} |
|
75
|
|
|
$('#wpinv_state', elB).find('option[value=""]').remove(); |
|
76
|
|
|
$('#wpinv_state', elB).addClass('form-control wpi-input required'); |
|
77
|
|
|
}).done(function(data) { |
|
|
|
|
|
|
78
|
|
|
jQuery('#wpinv_state_box').unblock(); |
|
79
|
|
|
console.log('78 : wpinv_recalculate_taxes()'); |
|
|
|
|
|
|
80
|
|
|
wpinv_recalculate_taxes(); |
|
81
|
|
|
}); |
|
82
|
|
|
return false; |
|
83
|
|
|
}); |
|
84
|
|
|
$('select#wpinv_state', elB).change(function(e) { |
|
|
|
|
|
|
85
|
|
|
$('.wpinv_errors').remove(); |
|
86
|
|
|
console.log('86 : wpinv_recalculate_taxes()'); |
|
|
|
|
|
|
87
|
|
|
wpinv_recalculate_taxes($(this).val()); |
|
88
|
|
|
}); |
|
89
|
|
|
var WPInv_Checkout = { |
|
90
|
|
|
checkout_form: $('form#wpinv_checkout_form'), |
|
91
|
|
|
init: function() { |
|
92
|
|
|
if (!$(this.checkout_form).length) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
// Payment methods |
|
96
|
|
|
this.checkout_form.on('click', 'input[name="wpi-gateway"]', this.payment_method_selected); |
|
97
|
|
|
this.init_payment_methods(); |
|
98
|
|
|
//this.recalculate_taxes(); |
|
99
|
|
|
}, |
|
100
|
|
|
init_payment_methods: function() { |
|
101
|
|
|
var $checkout_form = this.checkout_form; |
|
102
|
|
|
var $payment_methods = $('.wpi-payment_methods input[name="wpi-gateway"]'); |
|
103
|
|
|
// If there is one method, we can hide the radio input |
|
104
|
|
|
if (1 === $payment_methods.length) { |
|
105
|
|
|
$payment_methods.eq(0).hide(); |
|
106
|
|
|
} |
|
107
|
|
|
// If there are none selected, select the first. |
|
108
|
|
|
if (0 === $payment_methods.filter(':checked').length) { |
|
109
|
|
|
$payment_methods.eq(0).prop('checked', true); |
|
110
|
|
|
} |
|
111
|
|
|
// Trigger click event for selected method |
|
112
|
|
|
$payment_methods.filter(':checked').eq(0).trigger('click'); |
|
113
|
|
|
// Validate and apply a discount |
|
114
|
|
|
$checkout_form.on('click', '#wpi-apply-discount', this.applyDiscount); |
|
115
|
|
|
// Prevent the checkout form from submitting when hitting enter key in the discount field |
|
116
|
|
|
$checkout_form.on('keypress', '#wpinv_discount_code', function(event) { |
|
117
|
|
|
if (event.keyCode == '13') { |
|
|
|
|
|
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
}); |
|
121
|
|
|
// Apply the discount when hitting enter key in the discount field instead |
|
122
|
|
|
$checkout_form.on('keyup', '#wpinv_discount_code', function(event) { |
|
123
|
|
|
if (event.keyCode == '13') { |
|
124
|
|
|
$('#wpi-apply-discount', $checkout_form).trigger('click'); |
|
125
|
|
|
} |
|
126
|
|
|
}); |
|
127
|
|
|
// Remove a discount |
|
128
|
|
|
$(document.body).on('click', '.wpi-discount-remove', this.removeDiscount); |
|
129
|
|
|
}, |
|
130
|
|
|
payment_method_selected: function() { |
|
131
|
|
|
if ($('.wpi-payment_methods input.wpi-pmethod').length > 1) { |
|
132
|
|
|
var target_payment_box = $('div.payment_box.' + $(this).attr('ID')); |
|
133
|
|
|
if ($(this).is(':checked') && !target_payment_box.is(':visible')) { |
|
134
|
|
|
$('div.payment_box').filter(':visible').slideUp(250); |
|
135
|
|
|
if ($(this).is(':checked')) { |
|
136
|
|
|
var content = $('div.payment_box.' + $(this).attr('ID')).html(); |
|
137
|
|
|
content = content ? content.trim() : ''; |
|
138
|
|
|
if (content) { |
|
139
|
|
|
$('div.payment_box.' + $(this).attr('ID')).slideDown(250); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} else { |
|
144
|
|
|
$('div.payment_box').show(); |
|
145
|
|
|
} |
|
146
|
|
|
$('#wpinv_payment_mode_select').attr('data-gateway', $(this).val()); |
|
147
|
|
|
if ($(this).data('button-text')) { |
|
148
|
|
|
$('#wpinv-payment-button').val($(this).data('button-text')); |
|
149
|
|
|
} else { |
|
150
|
|
|
$('#wpinv-payment-button').val($('#wpinv-payment-button').data('value')); |
|
151
|
|
|
} |
|
152
|
|
|
}, |
|
153
|
|
|
applyDiscount: function(e) { |
|
|
|
|
|
|
154
|
|
|
e.preventDefault(); |
|
155
|
|
|
var $this = $(this), |
|
156
|
|
|
$box = $this.closest('.panel-body'), |
|
157
|
|
|
discount_code = $('#wpinv_discount_code', $box).val(), |
|
158
|
|
|
$msg = $('.wpinv-discount-msg', $box), |
|
159
|
|
|
$msgS = $('.alert-success', $msg), |
|
160
|
|
|
$msgF = $('.alert-error', $msg); |
|
161
|
|
|
if (discount_code == '') { |
|
162
|
|
|
$('#wpinv_discount_code', $box).focus(); |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
var data = { |
|
166
|
|
|
action: 'wpinv_apply_discount', |
|
167
|
|
|
code: discount_code, |
|
168
|
|
|
_nonce: WPInv.nonce |
|
|
|
|
|
|
169
|
|
|
}; |
|
170
|
|
|
$('.wpinv_errors').remove(); |
|
171
|
|
|
$msg.hide(); |
|
172
|
|
|
$msgS.hide().find('.wpi-msg').html(''); |
|
173
|
|
|
$msgF.hide().find('.wpi-msg').html(''); |
|
174
|
|
|
wpinvBlock($box); |
|
175
|
|
|
$.ajax({ |
|
176
|
|
|
type: "POST", |
|
177
|
|
|
data: data, |
|
178
|
|
|
dataType: "json", |
|
179
|
|
|
url: WPInv.ajax_url, |
|
180
|
|
|
xhrFields: { |
|
181
|
|
|
withCredentials: true |
|
182
|
|
|
}, |
|
183
|
|
|
success: function(res) { |
|
184
|
|
|
wpinvUnblock($box); |
|
185
|
|
|
var success = false; |
|
186
|
|
|
if (res && typeof res == 'object') { |
|
187
|
|
|
if (res.success) { |
|
188
|
|
|
success = true; |
|
189
|
|
|
jQuery('#wpinv_checkout_cart_form', $this.checkout_form).replaceWith(res.data.html); |
|
190
|
|
|
jQuery('.wpinv-chdeckout-total').text(res.data.total); |
|
191
|
|
|
$('#wpinv_discount_code', $box).val(''); |
|
192
|
|
|
//console.log('217 : wpinv_recalculate_taxes()'); |
|
193
|
|
|
//wpinv_recalculate_taxes(); |
|
194
|
|
|
if (res.data.free) { |
|
195
|
|
|
$('#wpinv_payment_mode_select', $this.checkout_form).hide(); |
|
196
|
|
|
gw = 'manual'; |
|
|
|
|
|
|
197
|
|
|
} else { |
|
198
|
|
|
$('#wpinv_payment_mode_select', $this.checkout_form).show(); |
|
199
|
|
|
gw = $('#wpinv_payment_mode_select', $this.checkout_form).data('gateway'); |
|
200
|
|
|
} |
|
201
|
|
|
$('input[name="wpi-gateway"]', $this.checkout_form).val(gw); |
|
202
|
|
|
$(document.body).trigger('wpinv_discount_applied', [res]); |
|
203
|
|
|
} |
|
204
|
|
|
if (res.msg) { |
|
205
|
|
|
$msg.show(); |
|
206
|
|
|
if (success) { |
|
207
|
|
|
$msgS.show().find('.wpi-msg').html(res.msg); |
|
208
|
|
|
} else { |
|
209
|
|
|
$msgF.show().find('.wpi-msg').html(res.msg); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
}).fail(function(res) { |
|
215
|
|
|
wpinvUnblock($box); |
|
216
|
|
|
if (window.console && window.console.log) { |
|
217
|
|
|
console.log(res); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
}); |
|
220
|
|
|
return false; |
|
221
|
|
|
}, |
|
222
|
|
|
removeDiscount: function(e) { |
|
|
|
|
|
|
223
|
|
|
e.preventDefault(); |
|
224
|
|
|
var $this = $(this), |
|
225
|
|
|
$block = $this.closest('#wpinv_checkout_cart_wrap'), |
|
226
|
|
|
discount_code = $this.data('code'); |
|
227
|
|
|
if (discount_code == '') { |
|
228
|
|
|
return false; |
|
229
|
|
|
} |
|
230
|
|
|
var data = { |
|
231
|
|
|
action: 'wpinv_remove_discount', |
|
232
|
|
|
code: discount_code, |
|
233
|
|
|
_nonce: WPInv.nonce |
|
|
|
|
|
|
234
|
|
|
}; |
|
235
|
|
|
wpinvBlock($block); |
|
236
|
|
|
$.ajax({ |
|
237
|
|
|
type: "POST", |
|
238
|
|
|
data: data, |
|
239
|
|
|
dataType: "json", |
|
240
|
|
|
url: WPInv.ajax_url, |
|
241
|
|
|
xhrFields: { |
|
242
|
|
|
withCredentials: true |
|
243
|
|
|
}, |
|
244
|
|
|
success: function(res) { |
|
245
|
|
|
if (res && typeof res == 'object') { |
|
246
|
|
|
if (res.success) { |
|
247
|
|
|
jQuery('#wpinv_checkout_cart_form', $this.checkout_form).replaceWith(res.data.html); |
|
248
|
|
|
jQuery('.wpinv-chdeckout-total').text(res.data.total); |
|
249
|
|
|
if (res.data.free) { |
|
250
|
|
|
$('#wpinv_payment_mode_select', $this.checkout_form).hide(); |
|
251
|
|
|
gw = 'manual'; |
|
|
|
|
|
|
252
|
|
|
} else { |
|
253
|
|
|
$('#wpinv_payment_mode_select', $this.checkout_form).show(); |
|
254
|
|
|
gw = $('#wpinv_payment_mode_select', $this.checkout_form).data('gateway'); |
|
255
|
|
|
} |
|
256
|
|
|
$('input[name="wpi-gateway"]', $this.checkout_form).val(gw); |
|
257
|
|
|
//console.log('291 : wpinv_recalculate_taxes()'); |
|
258
|
|
|
//wpinv_recalculate_taxes(); |
|
259
|
|
|
$(document.body).trigger('wpinv_discount_removed', [res]); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
}).fail(function(res) { |
|
264
|
|
|
wpinvUnblock($block); |
|
265
|
|
|
if (window.console && window.console.log) { |
|
266
|
|
|
console.log(res); |
|
|
|
|
|
|
267
|
|
|
} |
|
268
|
|
|
}); |
|
269
|
|
|
return false; |
|
270
|
|
|
}, |
|
271
|
|
|
recalculate_taxes: function() { |
|
272
|
|
|
console.log('308 : wpinv_recalculate_taxes()'); |
|
|
|
|
|
|
273
|
|
|
wpinv_recalculate_taxes(); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
WPInv_Checkout.init(); |
|
277
|
|
|
}); |
|
278
|
|
|
|
|
279
|
|
|
function wpinvBlock(el, message) { |
|
280
|
|
|
message = typeof message != 'undefined' && message !== '' ? ' ' + message : ''; |
|
281
|
|
|
el.block({ |
|
282
|
|
|
message: '<i class="fa fa-refresh fa-spin"></i>' + message, |
|
283
|
|
|
overlayCSS: { |
|
284
|
|
|
background: '#fff', |
|
285
|
|
|
opacity: 0.6 |
|
286
|
|
|
}, |
|
287
|
|
|
ignoreIfBlocked: true |
|
288
|
|
|
}); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
function wpinvUnblock(el) { |
|
292
|
|
|
el.unblock(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
function wpinvRemoveQueryVar(url, parameter) { |
|
296
|
|
|
//prefer to use l.search if you have a location/link object |
|
297
|
|
|
var urlparts = url.split('?'); |
|
298
|
|
|
var urlparts2 = url.split('&'); |
|
299
|
|
|
if (urlparts.length >= 2) { |
|
300
|
|
|
var prefix = encodeURIComponent(parameter) + '='; |
|
301
|
|
|
var pars = urlparts[1].split(/[&;]/g); |
|
302
|
|
|
//reverse iteration as may be destructive |
|
303
|
|
|
for (var i = pars.length; i-- > 0;) { |
|
304
|
|
|
//idiom for string.startsWith |
|
305
|
|
|
if (pars[i].lastIndexOf(prefix, 0) !== -1) { |
|
306
|
|
|
pars.splice(i, 1); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
url = urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : ""); |
|
310
|
|
|
return url; |
|
311
|
|
|
} else if (urlparts2.length >= 2) { |
|
312
|
|
|
var prefix = encodeURIComponent(parameter) + '='; |
|
|
|
|
|
|
313
|
|
|
var pars = url.split(/[&;]/g); |
|
|
|
|
|
|
314
|
|
|
//reverse iteration as may be destructive |
|
315
|
|
|
for (var i = pars.length; i-- > 0;) { |
|
|
|
|
|
|
316
|
|
|
//idiom for string.startsWith |
|
317
|
|
|
if (pars[i].lastIndexOf(prefix, 0) !== -1) { |
|
318
|
|
|
pars.splice(i, 1); |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
url = pars.join('&'); |
|
322
|
|
|
return url; |
|
323
|
|
|
} else { |
|
324
|
|
|
return url; |
|
325
|
|
|
} |
|
326
|
|
|
} |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.