-
Notifications
You must be signed in to change notification settings - Fork 18
/
script.js
95 lines (89 loc) · 3.96 KB
/
script.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
// Helper / Utility functions
let url_to_head = (url) => {
return new Promise(function(resolve, reject) {
var script = document.createElement('script');
script.src = url;
script.onload = function() {
resolve();
};
script.onerror = function() {
reject('Error loading script.');
};
document.head.appendChild(script);
});
}
let handle_close = (event) => {
event.target.closest(".ms-alert").remove();
}
let handle_click = (event) => {
if (event.target.classList.contains("ms-close")) {
handle_close(event);
}
}
document.addEventListener("click", handle_click);
const paypal_sdk_url = "https://www.paypal.com/sdk/js";
const client_id = "REPLACE_WITH_YOUR_CLIENT_ID";
const currency = "USD";
const intent = "capture";
let alerts = document.getElementById("alerts");
//PayPal Code
//https://developer.paypal.com/sdk/js/configuration/#link-queryparameters
url_to_head(paypal_sdk_url + "?client-id=" + client_id + "&enable-funding=venmo¤cy=" + currency + "&intent=" + intent)
.then(() => {
//Handle loading spinner
document.getElementById("loading").classList.add("hide");
document.getElementById("content").classList.remove("hide");
let alerts = document.getElementById("alerts");
let paypal_buttons = paypal.Buttons({ // https://developer.paypal.com/sdk/js/reference
onClick: (data) => { // https://developer.paypal.com/sdk/js/reference/#link-oninitonclick
//Custom JS here
},
style: { //https://developer.paypal.com/sdk/js/reference/#link-style
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal'
},
createOrder: function(data, actions) { //https://developer.paypal.com/docs/api/orders/v2/#orders_create
return fetch("http://localhost:3000/create_order", {
method: "post", headers: { "Content-Type": "application/json; charset=utf-8" },
body: JSON.stringify({ "intent": intent })
})
.then((response) => response.json())
.then((order) => { return order.id; });
},
onApprove: function(data, actions) {
let order_id = data.orderID;
return fetch("http://localhost:3000/complete_order", {
method: "post", headers: { "Content-Type": "application/json; charset=utf-8" },
body: JSON.stringify({
"intent": intent,
"order_id": order_id
})
})
.then((response) => response.json())
.then((order_details) => {
console.log(order_details); //https://developer.paypal.com/docs/api/orders/v2/#orders_capture!c=201&path=create_time&t=response
let intent_object = intent === "authorize" ? "authorizations" : "captures";
//Custom Successful Message
alerts.innerHTML = `<div class=\'ms-alert ms-action\'>Thank you ` + order_details.payer.name.given_name + ` ` + order_details.payer.name.surname + ` for your payment of ` + order_details.purchase_units[0].payments[intent_object][0].amount.value + ` ` + order_details.purchase_units[0].payments[intent_object][0].amount.currency_code + `!</div>`;
//Close out the PayPal buttons that were rendered
paypal_buttons.close();
})
.catch((error) => {
console.log(error);
alerts.innerHTML = `<div class="ms-alert ms-action2 ms-small"><span class="ms-close"></span><p>An Error Ocurred!</p> </div>`;
});
},
onCancel: function (data) {
alerts.innerHTML = `<div class="ms-alert ms-action2 ms-small"><span class="ms-close"></span><p>Order cancelled!</p> </div>`;
},
onError: function(err) {
console.log(err);
}
});
paypal_buttons.render('#payment_options');
})
.catch((error) => {
console.error(error);
});