This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
67 lines (59 loc) · 1.98 KB
/
app.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
// You will need to make many changes here
// for production
// Do Note that reportErrorToBackend and verifyTransactionOnBackend
// are called in main.js
// Be careful changing their name or the data they accept
$("#checkout-form").submit(function(e){
e.preventDefault();
var email = $('#email').val();
var amount = $('#amount').val();
// then we initialize the transaction on the backend
startTransactionOnBackend(email, amount);
// clean up
$("#checkout-form").hide();
$("#checkout-error").html('');
$("#processing").show();
$('#email').val('');
$('#amount').val('');
});
function reportErrorToBackend(error){
// we are reporting only the error here. in real life
// you will want to collect a little more information
$.ajax({
type: "POST",
url: 'report',
data: {error: error}
});
}
function verifyTransactionOnBackend(reference){
$.ajax({
type: "GET",
url: 'verify/' + reference,
success: function (gateway_response) {
$("#success-gateway-response").html(gateway_response);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("There was an error verifying "+reference, xhr.responseText);
reportErrorToBackend(xhr.responseText);
$("#verify-error").html(xhr.responseText);
}
});
}
function startTransactionOnBackend(email, amount){
$.ajax({
type: "POST",
url: 'new-access-code',
data: {email: email, amount: amount},
success: function (access_code) {
startPaystack(access_code);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("There was an error getting an accesscode", xhr.responseText);
reportErrorToBackend(xhr.responseText);
// clean up
$("#processing").hide();
$("#checkout-form").show();
$("#checkout-error").html(xhr.responseText);
}
});
}