-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaymentfree.js
129 lines (123 loc) · 4.18 KB
/
paymentfree.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var classArray = classArray;
// var stripe = Stripe("pk_test_txUHW0roiaw7rDEneBF5IgCB");
// var stripe = Stripe('pk_live_IiyzcOmj7fIv5anZ0W1Ukyie');
// var elements = stripe.elements();
var id;
var data;
document.addEventListener("DOMContentLoaded", function (event) {
// createElements();
formHandler();
id = getParam("id")
getData(id)
});
function getParam(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
console.log("payment.getParam: results="+results);
if (results==null){
return null;
}
else{
final_result = decodeURI(results[1]) || 0;
console.log("payment.getParam: final_result="+final_result);
return final_result;
}
}
// Custom styling can be passed to options when creating an Element.
var style = {
base: {
// Add your base input styles here. For example:
fontSize: '16px',
color: "#32325d",
}
};
function getData(id) {
$.ajax({
type: "GET",
contentType: 'application/json',
url : "https://siliconvalleyyouth-current.herokuapp.com/class2025spring?id="+id,
// Don't forget to change url to match teaching semester ^
// url : "http://localhost:3000/class2020spring?id="+id,
dataType: "json",
success: function(res) {
console.log("success")
createForm(res);
},
error: function(err) {
console.log(err);
}
})
//sendEmail()
}
function createForm(res) {
var raw_data = res["data"]
// var data = raw_data[0]
var data = JSON.parse(raw_data);
console.log(data)
var className = data["classname"]
var numClasses = data["numberclasses"]
console.log("createForm:+"+className+",numClasses="+numClasses)
$("#costDisplay").text("$0")
$("#classTitle").text("Registration for "+ className + " at " + data["location"] + " on " + data["time"])
$("#className").setAttribute('value', className);
//sendEmail()
}
// var card = elements.create('card', { style: style });
// card.addEventListener('change', function (event) {
// var displayError = document.getElementById('card-errors');
// if (event.error) {
// displayError.textContent = event.error.message;
// } else {
// displayError.textContent = '';
// }
// });
function formHandler() {
var form = document.getElementById('payment-form');
form.addEventListener('submit', function (event) {
event.preventDefault();
// stripe.createToken(card).then(function (result) {
// if (result.error) {
// var errorElement = document.getElementById('card-errors');
// errorElement.textContent = result.error.message;
// } else {
stripeTokenHandler();
// }
// });
});
//sendEmail()
}
function stripeTokenHandler() {
// console.log("--- in github stripe");
// console.log(token);
// var form = document.getElementById('payment-form');
// var hiddenInput = document.createElement('input');
// hiddenInput.setAttribute('type', 'hidden');
// hiddenInput.setAttribute('name', 'stripeToken');
// hiddenInput.setAttribute('value', token.id);
// form.appendChild(hiddenInput);
$.ajax({
url: $('#payment-form').attr('action')+ "?id="+id,
// url: "http://localhost:3000/payment?id="+id,
type: 'POST',
data: $('#payment-form').serialize(),
success: function (response) {
console.log("got response from server")
console.log(response);
if (response === "Success") {
writeThankYou()
// sendEmail()
}
if (response == "Failed") {
alert("Payment failed, please check your credit card credentials or try again later.")
}
}
});
return false;
}
// function createElements() {
// card.mount('#card-element');
// }
function writeThankYou() {
$("#thankyoubody").fadeIn();
$("#paymentbody").hide();
$("#classname2").html(data["classname"]);
}