-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.html
168 lines (157 loc) · 5.45 KB
/
external.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">
<meta http-equiv="x-ua-compatible" content="IE=Edge">
<title>Verification Example</title>
<script type="text/javascript" src="./utilities.js"></script>
<style>
iframe.hidden {
display: none;
}
iframe.visible {
border: none;
height: 70vh;
width: 90vw;
max-width: 40em;
outline: 1px solid red;
}
input {
display: block;
}
</style>
<script>
const baseUrl = "https://merchant.intergiro.com/v1"
let authorization
let verification
function submit() {
authorization = {
"number": generateUnique(), // "Should be your unique authorization number"
"amount": Number(document.getElementById("amount").value),
"currency": document.getElementById("currency").value,
"card": {
"pan": document.getElementById("pan").value,
"expires": [
Number(document.getElementById("expires").value.split("/")[0]),
Number(document.getElementById("expires").value.split("/")[1])
],
"csc": document.getElementById("csc").value
},
browser: { parent: window.location.origin }
}
verification = {
"number": authorization.number,
"items": authorization.items,
"currency": authorization.currency,
"card": {
"pan": authorization.card.pan,
"expires": authorization.card.expires,
"csc": authorization.card.csc
},
browser: { parent: window.location.origin }
}
authorize(authorization)
}
async function authorize(creatable) {
const [status, body] = await post(baseUrl + "/authorization", document.getElementById("no3dKey").value.trim(), creatable)
const iframe = document.getElementById("verification")
switch (status) {
case 201:
alert("success: " + JSON.stringify(body))
break
case 400:
if (body.error == "verification required" && body.content.details?.url) {
iframe.src = body.content.details.url
authorization.id = body.id
} else if (body.error == "verification required")
await perform3d()
else
alert("failed: " + JSON.stringify(body))
break
default:
alert("failed: " + JSON.stringify(body))
break
}
}
window.addEventListener("message", async e => {
if (e.data.destination == "parent" && e.data.content.name == "card") {
const response = JSON.parse(atob(e.data.content.value.split(".")[1]))
if (response.ver.type == "guard")
await authorize({ ...authorization, card: e.data.content.value })
}
})
async function post(url, key, body) {
const response = await fetch(url, {
method: "POST",
headers: new Headers(
{
Authorization: "Bearer " + key,
"Content-Type": "application/json"
}),
body: JSON.stringify(body),
}
)
return [response.status, response.headers.get("Content-Type")?.startsWith("application/json") ? await response.json() : await response.text()]
}
// Function used in this example to perform 3D. A public api key with 3D configurations is needed to test this example.
async function perform3d() {
const [status, body] = await post(baseUrl + "/verification?method=GET", document.getElementById("key").value.trim(), verification)
const iframe = document.getElementById("verification")
switch (status) {
case 201:
authorization.card.verification = body
iframe.classList.remove("visible")
iframe.classList.add("hidden")
await authorize(authorization)
break
case 400:
if (body.error == "verification required") {
iframe.classList.replace("hidden", body.content.details.visible ? "visible" : "hidden")
iframe.src = body.content.details.url + "&parent=" + encodeURIComponent(window.location.origin)
} else
alert("failed: " + JSON.stringify(body))
break
default:
alert("failed: " + JSON.stringify(body))
break
}
}
// EventListener used to receive data from the 3D process used in this example.
window.addEventListener("message", async e => {
if (e.data.destination == "parent" && e.data.content.name == "card") {
const response = JSON.parse(atob(e.data.content.value.split(".")[1]))
if (response.ver.type != "guard") {
verification.card = e.data.content.value
await perform3d()
}
const iframe = document.getElementById("verification")
iframe.class = "hidden"
} else if (e.data.destination == "parent" && e.data.content.name == "verification") {
verification.card.verification = JSON.parse(e.data.content.value)
await perform3d()
}
})
</script>
</head>
<body style="width: 100%; max-width: 20em; margin-left: auto; margin-right: auto;">
<iframe id="verification" class="hidden"></iframe>
<form method="post">
<label for="pan">PAN</label>
<input type="text" id="pan" name="pan" value="4111111111111111" />
<label for="csc">CSC</label>
<input type="text" id="csc" name="csc" value="987" />
<label for="expires">Expires</label>
<input type="text" id="expires" name="expires" placeholder="MM/YY" value="3/33" />
<label for="currency">Currency</label>
<input type="text" id="currency" name="currency" value="EUR" />
<label for="amount">Amount</label>
<input type="text" id="amount" name="amount" value="250" />
<label for="no3dKey">No3d Key</label>
<input type="text" id="no3dKey" name="no3dKey" />
<label for="key">3d Key</label>
<input type="text" id="key" name="key" />
</form>
<button onclick="submit()">Submit</button>
</body>
</html>