-
Notifications
You must be signed in to change notification settings - Fork 3
/
payum.js
103 lines (93 loc) · 3.3 KB
/
payum.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
if("undefined"==typeof jQuery) {
throw new Error("Payum's JavaScript requires jQuery");
}
Payum = function(serverUrl) {
var payum = this;
payum.serverUrl = serverUrl;
payum.payment = {
create: function(payment, callback) {
$.ajax(payum.serverUrl + '/payments', {
'data': JSON.stringify(payment),
'type': 'POST',
'processData': false,
'contentType': 'application/json',
success: function(data) {
callback(data.payment);
}
});
},
get: function(id, callback) {
$.ajax(payum.serverUrl + '/payments/'+id, {
'type': 'GET',
success: function(data) {
callback(data.payment);
}
});
}
};
payum.token = {
create: function(token, callback) {
$.ajax(payum.serverUrl + '/tokens', {
'data': JSON.stringify(token),
'type': 'POST',
'processData': false,
'contentType': 'application/json',
success: function(data) {
callback(data.token);
}
});
}
};
payum.execute = function(url, container) {
$.ajax(url, {
type: "GET",
async: true,
headers: {
Accept: 'application/vnd.payum+json'
},
complete: function(data) {
payum.updateContainer(data, container);
$(container + ' form').on('submit', function (e) {
e.preventDefault();
var form = $(this);
var values = {};
$.each(form.serializeArray(), function (i, field) {
values[field.name] = field.value;
});
jQuery.ajax(form.attr('action'), {
type: "POST",
headers: {
Accept: 'application/vnd.payum+json'
},
data: values,
success: function(data) {
},
complete: function(data) {
payum.updateContainer(data, container);
},
error: function(data) {
payum.updateContainer(data, container);
}
});
});
},
error: function(data) {
payum.updateContainer(data, container);
}
});
};
payum.updateContainer = function updateContainer(data, container) {
if (data.status == 302) {
window.location.replace(data.responseJSON.headers.Location);
}
if (data.status >= 200 && data.status < 300) {
$(container).html(data.responseJSON.content);
}
if (data.status >= 400 && data.status < 500) {
$(container).html('<div class="alert alert-warning">Bad request error</div>');
}
if (data.status >= 500 && data.status < 600) {
$(container).html('<div class="alert alert-warning">Internal server error</div>');
}
};
};