-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcredit-card.html
267 lines (252 loc) · 11.5 KB
/
credit-card.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" xmlns="http://www.w3.org/1999/html"
xmlns:sec="http://www.w3.org/1999/xhtml">
<head th:replace="~{layout :: common_header}">
<title>Midtrans Snap JAVA Springboot Demo</title>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/picomodal/3.0.0/picoModal.js"></script>
<div th:replace="~{fragments/header :: header}"></div>
<!-- Begin page content -->
<main role="main" class="container" style="padding-top: 30px">
<h2 class="mt-5">Midtrans CreditCard CoreAPI Demo</h2>
<div class="row">
<div class="col-md-4 order-md-2 mb-4">
<h4 class="d-flex justify-content-between align-items-center mb-3">
<span class="text-muted">Your cart</span>
<span class="badge badge-secondary badge-pill">3</span>
</h4>
<ul class="list-group mb-3">
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Sendal Karet Rumahan</h6>
<small class="text-muted">Ukuran 7</small>
</div>
<span class="text-muted">Rp.15.0000</span>
</li>
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Mantel Hujan</h6>
<small class="text-muted">Ukuran XL</small>
</div>
<span class="text-muted">Rp.200.000</span>
</li>
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Sarung Tangan Karet</h6>
<small class="text-muted">1Pack Isi 17Pcs</small>
</div>
<span class="text-muted">Rp.50.000</span>
</li>
<li class="list-group-item d-flex justify-content-between">
<span>Total (Rp)</span>
<strong>Rp.265.000</strong>
</li>
</ul>
</div>
<div class="col-md-8 order-md-1">
<h4 class="mb-3">Billing & Shipping Address</h4>
<form class="needs-validation" method="post" novalidate>
<div class="row">
<div class="col-md-3">
<label for="firstName">First name</label></br>
<h5><label id="firstName" th:text="${result.customer_details.first_name}"></label></h5>
</div>
<div class="col-md-3">
<label for="lastName">Last name</label></br>
<h5><label id="lastName" th:text="${result.customer_details.last_name}"></label></h5>
</div>
</div>
<div class="mb-3">
<label for="email">Email</label></br>
<h5><label id="email" th:text="${result.customer_details.billing_address.email}"></label></h5>
</div>
<div class="mb-3">
<label for="address">Address</label></br>
<h5><label id="address" th:text="${result.customer_details.billing_address.address}"></label></h5>
</div>
<hr class="mb-4">
<div class="row">
<div class="col-md-5">
<div class="row">
<div class="col-md-8">
<label for="card-number">Credit card number</label>
<input type="text" class="card-number form-control" id="card-number"
value="4811 1111 1111 1114" placeholder="" required>
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="card-expiry-month">Month</label>
<input type="text" class="card-expiry-month form-control" value="12"
id="card-expiry-month" placeholder="" required>
<div class="invalid-feedback">
Expiration date required
</div>
</div>
/
<div class="col-md-3 mb-3">
<label for="card-expiry-year">Year</label>
<input type="text" class="card-expiry-year form-control" value="22"
id="card-expiry-year" placeholder="" required>
<div class="invalid-feedback">
Security code required
</div>
</div>
</div>
<div>
<div class="col-md-4 mb-3">
<label for="card-cvv">CVV</label>
<input type="text" class="card-cvv form-control" value="123" id="card-cvv"
placeholder="" required>
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
</div>
</div>
<div class="col-md-7">
<code>
<pre>
<b>Testing cards:</b>
<b>For 3D Secure:</b>
Visa success 4811 1111 1111 1114
Visa deny by bank 4711 1111 1111 1115
Visa deny by FDS 4611 1111 1111 1116
MasterCard success 5211 1111 1111 1117
MasterCard deny by bank 5111 1111 1111 1118
MasterCard deny by FDS 5411 1111 1111 1115
Challenge by FDS 4511 1111 1111 1117
</pre>
</code>
</div>
</div>
</form>
<hr class="mb-4">
<button id="pay-button" class="btn btn-primary btn-lg btn-block">Payment with CreditCard!</button>
<hr class="mb-4">
<code>
<b>Transaction Result:</b>
<pre id="result"> Awaiting transactions... </pre>
<b>Transaction verified status result:</b>
<pre id="status-result"> Awaiting transactions... </pre>
</code>
<hr class="mb-4">
</div>
</div>
</main>
<div th:replace="~{layout :: body_bottom_scripts}"/>
<script id="midtrans-script" src="https://api.midtrans.com/v2/assets/js/midtrans-new-3ds.min.js"
data-environment="sandbox" th:attr="data-client-key=${clientKey}" type="text/javascript"></script>
<script th:inline="javascript">
var payButton = document.getElementById('pay-button');
payButton.onclick = function (event) {
console.log("PAY button clicked");
var cardData = {
"card_number": document.querySelector(".card-number").value,
"card_exp_month": document.querySelector(".card-expiry-month").value,
"card_exp_year": document.querySelector(".card-expiry-year").value,
"card_cvv": document.querySelector(".card-cvv").value,
};
MidtransNew3ds.getCardToken(cardData, options);
event.preventDefault();
return false;
};
var options = {
onSuccess: function(response) {
// Implement success handling here
console.log(response);
fetch("/cards/charge", {
method : "POST",
body: JSON.stringify({
"token_id" : response.token_id
}),
headers: {'Content-Type': 'application/json'},
})
.then(function(response) { return response.json(); })
.then(function(responseObj) {
console.log("Charge response:",responseObj);
if (responseObj.status_code == "201"){
// [3.1] Transaction need 3DS authentication
MidtransNew3ds.authenticate(responseObj.redirect_url, callback3dsAuthentication);
} else {
// Transaction do not need 3DS Authentication, transaction is complete with result
transactionComplete(responseObj);
}
})
},
onFailure: function(response){
// fail to get card token
transactionComplete(response);
}
};
var callback3dsAuthentication = {
performAuthentication: function(redirect_url){
// [3.2] 3ds authentication redirect_url received, open iframe to display to customer
popupModal.openPopup(redirect_url);
}
,
// [3.3] When 3DS authentication result received, which contains transaction result
// it will trigger one of function below, according to status: success/failure/pending
onSuccess: function(response){
transactionComplete(response);
},
onFailure: function(response){
transactionComplete(response);
},
onPending: function(response){
transactionComplete(response);
}
};
function transactionComplete(responseObj){
// Close 3DS popup, then display the result (for example)
console.log("transactionComplete with status: ",responseObj);
popupModal.closePopup();
document.querySelector("#result").innerText = JSON.stringify(responseObj, null, 2);
document.querySelector("#result").scrollIntoView();
if (responseObj.transaction_id){
// [4] Inform the result to backend update DB status and verify to Midtrans
// Check backend implementation on `app.js` file, section `[4]`
fetch('/transactions/status', {
method : "POST",
body: JSON.stringify({ "transaction_id" : responseObj.transaction_id }),
headers: {'Content-Type': 'application/json'},
})
.then(function(statusResponse) { return statusResponse.json(); } )
.then(function(statusResponseObj) {
// Transaction status received after being verified
console.log("Check transaction response:",statusResponseObj);
// transactionComplete(statusResponseObj);
document.querySelector("#status-result").innerText =
JSON.stringify(statusResponseObj, null, 2);
})
}
}
// helper functions below
let popupModal = (function(){
let modal = null;
return {
openPopup(url){
modal = picoModal({
content:'<iframe frameborder="0" style="height:90vh; width:100%;" src="'+url+'"></iframe>',
width: "75%",
height: "75% !important",
closeButton: false,
overlayClose: false,
escCloses: false
}).show();
},
closePopup(){
try{
modal.close();
} catch(e) {}
}
}
}());
</script>
</body>
</html>