-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.js
72 lines (64 loc) · 2.32 KB
/
callback.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
// If successful, Square Point of Sale returns the following parameters.
const clientTransactionId = "client_transaction_id";
const transactionId = "transaction_id";
// const responseUrl = new URL(
// '/create?data={' +
// '"transaction_id":"transaction123",' +
// '"client_transaction_id":"40",' +
// '"status":"ok"' +
// '}',
// 'https://squareup.com'
// );
//If there's an error, Square Point of Sale returns the following parameters.
const errorField = "error_code";
//get the data URL and encode in JSON
function getTransactionInfo(URL) {
var data = decodeURI(URL.searchParams.get("data"));
console.log("data: " + data);
var transactionInfo = JSON.parse(data);
return transactionInfo;
}
// Makes a result string for success situation
function handleSuccess(transactionInfo){
var resultString ="";
if (clientTransactionId in transactionInfo) {
resultString += "Client Transaction ID: " + transactionInfo[clientTransactionId] + "<br>";
}
if (transactionId in transactionInfo) {
resultString += "Transaction ID: " + transactionInfo[transactionId] + "<br>";
}
else {
resultString += "Transaction ID: NO CARD USED<br>";
}
return resultString;
}
// Makes an error string for error situation
function handleError(transactionInfo){
var resultString ="";
if (errorField in transactionInfo) {
resultString += "Client Transaction ID: " + transactionInfo[clientTransactionId] + "<br>";
}
if (transactionId in transactionInfo) {
resultString += "Transaction ID: " + transactionInfo[transactionId] + "<br>";
}
else {
resultString += "Transaction ID: PROCESSED OFFLINE OR NO CARD USED<br>";
}
return resultString;
}
// Determines whether error or success based on urlParams, then prints the string
function printResponse() {
var responseUrl = window.location.href;//orginal line is var responseUrl = window.location.href;
var transactionInfo = getTransactionInfo(responseUrl);
var resultString = "";
if (errorField in transactionInfo) {
resultString = handleError(transactionInfo);
} else {
resultString = handleSuccess(transactionInfo);
}
document.getElementById('url').innerHTML = resultString;
// document.getElementById('url').innerHTML = "resultString";
}
function testString(){
document.getElementById('url').innerHTML = "test string in url div";
}