-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPayment.js
More file actions
82 lines (74 loc) · 2.08 KB
/
Copy pathgetPayment.js
File metadata and controls
82 lines (74 loc) · 2.08 KB
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
const baseAppUrl = `${document.location.protocol}//${document.location.host}`;
console.log(baseAppUrl);
const emsApiUrl = "https://api.online.emspay.eu/v1/orders/";
/**
* Read the order data from query string.
* @returns {Object} the order containing the order id and project id
*/
function getOrder() {
let queryStrValues = document.location.href
.substring(
document.location.href.indexOf("?") + 1,
document.location.href.length
)
.split("&");
let order = {};
queryStrValues.forEach((parameter) => {
var paramterArray = parameter.split("=");
Object.defineProperty(order, paramterArray[0], {
value: paramterArray[1],
});
});
return order;
}
/**
* Fill the HTML element with the data
* @param {Object} data JSON data returned by API
*/
function fillHtml(data) {}
/**
* Build the ajax data and request, send it and listen for the response.
*
* @param {Object} requestData The object containing the data to send.
*/
function getDataFromApi(requestData) {
const order = getOrder();
if (!order.order_id) return;
const getUrl = `${emsApiUrl}${order.order_id}`;
let customHeaders = new Headers();
customHeaders.append(
"Authorization",
"Basic NWExNzhmZGUyNDUwNGE5MDk3YTY1NTJhZGExMWEwY2Y6"
);
var params = {
method: "GET",
headers: customHeaders,
};
const request = new Request(getUrl, params);
fetch(request, params)
.then((response) => {
console.log("Fetch response", response);
if (response.redirected) {
alert("what is the redirection?");
window.location = response.url;
}
if (response.ok) {
const jsonData = response.json();
console.log(jsonData);
return jsonData;
}
console.log("Fetch failed response", response);
})
.then((data) => {
console.log(data);
let paymentDataElem = document.querySelector(".payment-data");
paymentDataElem.innerHTML = JSON.stringify(data);
})
.catch((err) => {
alert("Check console");
console.error("Error", err);
});
}
window.onload = function () {
getDataFromApi();
};