|
1
|
|
|
/* global pagantis_params */ |
|
2
|
|
|
|
|
3
|
|
|
jQuery(function ($) { |
|
4
|
|
|
|
|
5
|
|
|
//pagantis_params is required to continue, ensure the object exists |
|
6
|
|
|
if (typeof pagantis_params === 'undefined') { |
|
7
|
|
|
return false; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
var ppm_checkout = { |
|
11
|
|
|
$bodyElem: $('body'), |
|
12
|
|
|
$orderReviewElem: $('#order_review'), |
|
13
|
|
|
$checkoutFormElem: $('form.checkout'), |
|
14
|
|
|
$paymentMethodsList: $('.wc_payment_methods'), |
|
15
|
|
|
paymentMethodName: '', |
|
16
|
|
|
isPagantisPaymentMethod: false, |
|
17
|
|
|
selectedPaymentMethod: false, |
|
18
|
|
|
checkout_values: {}, |
|
19
|
|
|
updateTimer: false, |
|
20
|
|
|
xhr: false, |
|
21
|
|
|
init: function () { |
|
22
|
|
|
|
|
23
|
|
|
// Checks if Pagantis is selected Payment methods |
|
24
|
|
|
ppm_checkout.$checkoutFormElem.on('click', 'input[name="payment_method"]', this.pagantis_payment_method_selected); |
|
25
|
|
|
|
|
26
|
|
|
// unbind normal submit event |
|
27
|
|
|
if (ppm_checkout.isPagantisPaymentsSelected()) { |
|
28
|
|
|
ppm_checkout.$checkoutFormElem.off('submit', function () { |
|
29
|
|
|
console.log('submit ahas been unbinded'); |
|
|
|
|
|
|
30
|
|
|
return false; |
|
31
|
|
|
}); |
|
32
|
|
|
ppm_checkout.$checkoutFormElem.on('checkout_place_order', function () { |
|
33
|
|
|
console.log('event checkout_place_order intercepted'); |
|
|
|
|
|
|
34
|
|
|
return false; |
|
35
|
|
|
}); |
|
36
|
|
|
} |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// Store all billing and shipping values. |
|
41
|
|
|
$(document).ready(function () { |
|
42
|
|
|
$('#customer_details input, #customer_details select').each(function () { |
|
43
|
|
|
var fieldName = $(this).attr('name'); |
|
44
|
|
|
var fieldValue = $(this).val(); |
|
45
|
|
|
ppm_checkout.checkout_values[fieldName] = fieldValue; |
|
46
|
|
|
}); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
ppm_checkout.$checkoutFormElem.submit(function (event) { |
|
|
|
|
|
|
51
|
|
|
console.info(' trigger has worked'); |
|
52
|
|
|
ppm_checkout.submitPagantisOrder(); |
|
53
|
|
|
}); |
|
54
|
|
|
}, |
|
55
|
|
|
|
|
56
|
|
|
isPagantisPaymentsSelected: function () { |
|
57
|
|
|
if (ppm_checkout.get_selected_method() == 'pagantis') { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
return false; |
|
61
|
|
|
}, |
|
62
|
|
|
get_selected_method: function () { |
|
63
|
|
|
return ppm_checkout.$checkoutFormElem.find('input[name="payment_method"]:checked').val(); |
|
64
|
|
|
}, |
|
65
|
|
|
pagantis_payment_method_selected: function (e) { |
|
|
|
|
|
|
66
|
|
|
var selectedPaymentMethod = ppm_checkout.get_selected_method(); |
|
67
|
|
|
if (selectedPaymentMethod == 'pagantis') { |
|
68
|
|
|
ppm_checkout.isPagantisPaymentMethod = true; |
|
69
|
|
|
ppm_checkout.paymentMethodName = selectedPaymentMethod; |
|
70
|
|
|
} |
|
71
|
|
|
}, |
|
72
|
|
|
|
|
73
|
|
|
is_valid_json: function (raw_json) { |
|
74
|
|
|
try { |
|
75
|
|
|
var json = $.parseJSON(raw_json); |
|
76
|
|
|
|
|
77
|
|
|
return (json && 'object' === typeof json); |
|
78
|
|
|
} catch (e) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
}, |
|
82
|
|
|
handleUnloadEvent: function (e) { |
|
83
|
|
|
// Modern browsers have their own standard generic messages that they will display. |
|
84
|
|
|
// Confirm, alert, prompt or custom message are not allowed during the unload event |
|
85
|
|
|
// Browsers will display their own standard messages |
|
86
|
|
|
|
|
87
|
|
|
// Check if the browser is Internet Explorer |
|
88
|
|
|
if ((navigator.userAgent.indexOf('MSIE') !== -1) || (!!document.documentMode)) { |
|
|
|
|
|
|
89
|
|
|
// IE handles unload events differently than modern browsers |
|
90
|
|
|
e.preventDefault(); |
|
91
|
|
|
return undefined; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
}, |
|
96
|
|
|
attachUnloadEventsOnSubmit: function () { |
|
97
|
|
|
$(window).on('beforeunload', this.handleUnloadEvent); |
|
98
|
|
|
}, |
|
99
|
|
|
detachUnloadEventsOnSubmit: function () { |
|
100
|
|
|
$(window).unbind('beforeunload', this.handleUnloadEvent); |
|
101
|
|
|
}, |
|
102
|
|
|
reset_update_checkout_timer: function () { |
|
103
|
|
|
clearTimeout(ppm_checkout.updateTimer); |
|
104
|
|
|
}, |
|
105
|
|
|
blockOnSubmit: function ($form) { |
|
106
|
|
|
var form_data = $form.data(); |
|
107
|
|
|
|
|
108
|
|
|
if (1 !== form_data['blockUI.isBlocked']) { |
|
109
|
|
|
$form.block({ |
|
110
|
|
|
message: null, |
|
111
|
|
|
overlayCSS: { |
|
112
|
|
|
background: '#fff', |
|
113
|
|
|
opacity: 0.6 |
|
114
|
|
|
} |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
}, |
|
118
|
|
|
submitOrder: function () { |
|
119
|
|
|
ppm_checkout.blockOnSubmit($(this)); |
|
120
|
|
|
}, |
|
121
|
|
|
submitPagantisOrder: function () { |
|
122
|
|
|
var $form = $(this); |
|
123
|
|
|
|
|
124
|
|
|
if (ppm_checkout.isPagantisPaymentMethod === true) { |
|
125
|
|
|
$form.addClass('processing'); |
|
126
|
|
|
|
|
127
|
|
|
ppm_checkout.blockOnSubmit($form); |
|
128
|
|
|
|
|
129
|
|
|
// Attach event to block reloading the page when the form has been submitted |
|
130
|
|
|
//ppm_checkout.attachUnloadEventsOnSubmit(); |
|
131
|
|
|
console.info(ppm_checkout.checkout_values) |
|
132
|
|
|
if ($.checkout_values.isEmpty(this.checkout_values)) { |
|
133
|
|
|
return; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// ajaxSetup is global, but we use it to ensure JSON is valid once returned. |
|
137
|
|
|
$.ajaxSetup({ |
|
138
|
|
|
dataFilter: function (raw_response, dataType) { |
|
139
|
|
|
// We only want to work with JSON |
|
140
|
|
|
if ('json' !== dataType) { |
|
141
|
|
|
return raw_response; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (ppm_checkout.is_valid_json(raw_response)) { |
|
145
|
|
|
return raw_response; |
|
146
|
|
|
} else { |
|
|
|
|
|
|
147
|
|
|
// Attempt to fix the malformed JSON |
|
148
|
|
|
var maybe_valid_json = raw_response.match(/{"result.*}/); |
|
149
|
|
|
|
|
150
|
|
|
if (null === maybe_valid_json) { |
|
151
|
|
|
console.log('Unable to fix malformed JSON'); |
|
|
|
|
|
|
152
|
|
|
} else if (ppm_checkout.is_valid_json(maybe_valid_json[0])) { |
|
153
|
|
|
console.log('Fixed malformed JSON. Original:'); |
|
154
|
|
|
console.log(raw_response); |
|
155
|
|
|
raw_response = maybe_valid_json[0]; |
|
156
|
|
|
} else { |
|
157
|
|
|
console.log('Unable to fix malformed JSON'); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
return raw_response; |
|
161
|
|
|
} |
|
162
|
|
|
}); |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
var data = { |
|
168
|
|
|
security: pagantis_params.place_order_nonce, |
|
169
|
|
|
payment_method: ppm_checkout.get_payment_method(), |
|
170
|
|
|
checkout_values: this.checkout_values, |
|
171
|
|
|
post_data: $('form.checkout').serialize() |
|
172
|
|
|
}; |
|
173
|
|
|
|
|
174
|
|
|
console.info(data); |
|
175
|
|
|
$.ajax({ |
|
176
|
|
|
type: 'POST', |
|
177
|
|
|
url: pagantis_params.place_order_url, |
|
178
|
|
|
security: pagantis_params.place_order_nonce, |
|
179
|
|
|
payment_method: ppm_checkout.get_payment_method(), |
|
180
|
|
|
checkout_values: this.checkout_values, |
|
181
|
|
|
data: $form.serialize(), |
|
182
|
|
|
dataType: 'json', |
|
183
|
|
|
success: function (result) { |
|
184
|
|
|
// Detach the unload handler that prevents a reload / redirect |
|
185
|
|
|
ppm_checkout.detachUnloadEventsOnSubmit(); |
|
186
|
|
|
try { |
|
187
|
|
|
if ('success' === result.result && $form.triggerHandler('checkout_place_order_success') !== false) { |
|
188
|
|
|
if (-1 === result.redirect.indexOf('https://') || -1 === result.redirect.indexOf('http://')) { |
|
189
|
|
|
window.location = result.redirect; |
|
190
|
|
|
} else { |
|
191
|
|
|
window.location = decodeURI(result.redirect); |
|
192
|
|
|
} |
|
193
|
|
|
} else if ('failure' === result.result) { |
|
194
|
|
|
throw 'Result failure'; |
|
195
|
|
|
} else { |
|
196
|
|
|
throw 'Invalid response'; |
|
197
|
|
|
} |
|
198
|
|
|
} catch (err) { |
|
199
|
|
|
// Reload page |
|
200
|
|
|
if (true === result.reload) { |
|
201
|
|
|
window.location.reload(); |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// Trigger update in case we need a fresh nonce |
|
206
|
|
|
if (true === result.refresh) { |
|
207
|
|
|
$(document.body).trigger('update_checkout'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Add new errors |
|
211
|
|
|
if (result.messages) { |
|
212
|
|
|
pagantis_params.submit_error(result.messages); |
|
213
|
|
|
} else { |
|
214
|
|
|
pagantis_params.submit_error('<div class="woocommerce-error">' + pagantis_params.i18n_checkout_error + '</div>'); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
}, |
|
218
|
|
|
error: function (jqXHR, textStatus, errorThrown) { |
|
219
|
|
|
// Detach the unload handler that prevents a reload / redirect |
|
220
|
|
|
pagantis_params.detachUnloadEventsOnSubmit(); |
|
221
|
|
|
|
|
222
|
|
|
pagantis_params.submit_error('<div class="woocommerce-error">' + errorThrown + '</div>'); |
|
223
|
|
|
} |
|
224
|
|
|
}); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return false; |
|
228
|
|
|
}, |
|
229
|
|
|
submit_error: function (error_message) { |
|
230
|
|
|
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove(); |
|
231
|
|
|
pagantis_params.$checkoutFormElem.prepend('<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>'); |
|
232
|
|
|
pagantis_params.$checkoutFormElem.removeClass('processing').unblock(); |
|
233
|
|
|
pagantis_params.$checkoutFormElem.find('.input-text, select, input:checkbox').trigger('validate').blur(); |
|
234
|
|
|
pagantis_params.scroll_to_notices(); |
|
235
|
|
|
$(document.body).trigger('checkout_error'); |
|
236
|
|
|
}, |
|
237
|
|
|
scroll_to_notices: function() { |
|
238
|
|
|
var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout' ); |
|
239
|
|
|
|
|
240
|
|
|
if ( ! scrollElement.length ) { |
|
241
|
|
|
scrollElement = $( '.form.checkout' ); |
|
242
|
|
|
} |
|
243
|
|
|
$.scroll_to_notices( scrollElement ); |
|
244
|
|
|
} |
|
245
|
|
|
}; |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
ppm_checkout.init(); |
|
|
|
|
|
|
249
|
|
|
}); |
|
250
|
|
|
|