-
Notifications
You must be signed in to change notification settings - Fork 0
/
payone.js
88 lines (76 loc) · 2.95 KB
/
payone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(function ($) {
'use strict';
Drupal.behaviors.payone_payment = {
attach: function (context, settings) {
if (!Drupal.payment_handler) {
Drupal.payment_handler = {};
}
var self = this;
for (var pmid in settings.payone_payment) {
Drupal.payment_handler[pmid] = function (pmid, $method, submitter) {
self.validateHandler(pmid, $method, submitter, settings.payone_payment[pmid]);
};
}
},
validateHandler: function (pmid, $method, submitter, params) {
var form_id = $method.closest('form').attr('id');
$('.mo-dialog-wrapper').addClass('visible');
if (typeof Drupal.clientsideValidation !== 'undefined') {
$('#clientsidevalidation-' + form_id + '-errors ul').empty();
}
var getField = function (name) {
if (name instanceof Array) { name = name.join(']['); }
return $method.find('[name$="[' + name + ']"]');
};
params['cardholder'] = getField('holder').val();
params['cardpan'] = getField('credit_card_number').val();
params['cardtype'] = getField('issuer').val();
params['cardexpiremonth'] = getField(['expiry_date', 'month']).val();
params['cardexpireyear'] = getField(['expiry_date', 'year']).val();
params['cardcvc2'] = getField('secure_code').val();
// We have to register a global callback function.
var self = this;
window['payone_payment_callback'] = function (response) {
if (response.get('status') === 'VALID') {
getField('credit_card_number').val('');
getField('secure_code').val('');
$method.find('.payone-pseudocardpan').val(response.get('pseudocardpan'));
submitter.ready();
}
else {
self.errorHandler(response.get('customermessage'), form_id);
submitter.error();
}
};
var request = new PayoneRequest(params, {
return_type: 'object',
callback_function_name: 'payone_payment_callback'
});
request.checkAndStore();
},
errorHandler: function (error, form_id) {
var settings;
var wrapper;
var child;
if (typeof Drupal.clientsideValidation !== 'undefined') {
settings = Drupal.settings.clientsideValidation['forms'][form_id];
wrapper = document.createElement(settings.general.wrapper);
child = document.createElement(settings.general.errorElement);
child.className = settings.general.errorClass;
child.innerHTML = error;
wrapper.appendChild(child);
$('#clientsidevalidation-' + form_id + '-errors ul')
.append(wrapper).show()
.parent().show();
}
else {
if ($('#messages').length === 0) {
$('<div id="messages"><div class="section clearfix">' +
'</div></div>').insertAfter('#header');
}
$('<div class="messages error">' + error + '</div>')
.appendTo('#messages .clearfix');
}
}
};
}(jQuery));