-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_3ds_2_javascript_v3_sdk.php
209 lines (174 loc) · 5.73 KB
/
index_3ds_2_javascript_v3_sdk.php
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
<?php
include('config.php');
$amount = 100; // fixed amount 100
if (isset($_POST['nonce'])) {
$result = Braintree_Transaction::sale([
'amount' => $amount,
'paymentMethodNonce' => $_POST['nonce'],
'merchantAccountId' => $btSettings['merchant_account_id'], // from config
'customer' => ['email' => 'customer@example.com',
'company' => 'Company name'
],
/**
* if needed:
*/
'options' => ['skipAdvancedFraudChecking' => true],
]);
if ($result->success) {
echo "Success";
/**
* submit for settlement after success:
*/
Braintree_Transaction::submitForSettlement($result->transaction->id);
} else {
echo "Fail: " . $result->message;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Braintree card payment implementation 3D secure 2.0 Javascript v3 SDK</title>
<meta name="description" content="Braintree card payment implementation 3D secure 2.0 Javascript v3 SDK">
<meta name="author" content="Jozef Vacval">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>3D Secure 2.0 implementation</h1>
<!-- form for submit -->
<form action="" method="post" id="frm">
<input type="hidden" id="nonce" name="nonce" value="">
</form>
<form action="javascript:void(0)" class="container">
<div class="row">
<div class="col-xs-12">
<table class="table">
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<tr>
<td>Number (successful with no challenge)</td>
<td>4000000000001000</td>
</tr>
<tr>
<td>Number (successful with challenge)</td>
<td>4000000000001091</td>
</tr>
<tr>
<td>Number (unsuccessful with challenge)</td>
<td>4000000000001109</td>
</tr>
<tr>
<td>Expiration Date (for sandbox testing, year must be exactly 3 years in the future)</td>
<td>12/22</td>
</tr>
<tr>
<td>CVV</td>
<td>123</td>
</tr>
</table>
</div>
</div>
<div id="hosted-fields">
Card number:
<div id="hf-number" class="form-control"></div>
Expiration date:
<div id="hf-date" class="form-control"></div>
CVV:
<div id="hf-cvv" class="form-control"></div>
<input disabled="disabled" id="pay-btn" class="btn btn-success" type="submit" value="Loading...">
</div>
</form>
<script src="assets/jquery.js"></script>
<script src="https://js.braintreegateway.com/web/3.52.0/js/client.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.52.0/js/hosted-fields.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.52.0/js/three-d-secure.min.js"></script>
<script>
var hf, threeDS;
function start() {
getClientToken();
}
function getClientToken() {
$.getJSON("generateToken.php", function (data) {
onFetchClientToken(data.client_token);
});
}
function setupComponents(clientToken) {
return Promise.all([
braintree.hostedFields.create({
authorization: clientToken,
styles: {
input: {
'font-size': '14px',
'font-family': 'monospace'
}
},
fields: {
number: {
selector: '#hf-number',
placeholder: '4111 1111 1111 1111'
},
cvv: {
selector: '#hf-cvv',
placeholder: '123'
},
expirationDate: {
selector: '#hf-date',
placeholder: '12 / 2020'
}
}
}),
braintree.threeDSecure.create({
authorization: clientToken,
version: 2
})
]);
}
function onFetchClientToken(clientToken) {
return setupComponents(clientToken).then(function (instances) {
hf = instances[0];
threeDS = instances[1];
setupForm();
}).catch(function (err) {
alert(err.message)
});
}
function setupForm() {
enablePayNow();
}
function enablePayNow() {
$('#pay-btn').val('Pay Now');
$('#pay-btn').prop("disabled", false);
}
$(document).on('click', '#pay-btn', function (event) {
$(this).prop("disabled", true);
$(this).val('Processing...');
hf.tokenize().then(function (payload) {
return threeDS.verifyCard({
onLookupComplete: function (data, next) {
next();
},
amount: <?= $amount ?>,
nonce: payload.nonce,
bin: payload.details.bin,
})
}).then(function (payload) {
if (!payload.liabilityShifted) {
console.log('Liability did not shift', payload);
$('#frm').submit();
return;
}
console.log('verification success:', payload);
$('#nonce').val(payload.nonce);
$('#frm').submit();
}).catch(function (err) {
alert(err.message)
enablePayNow();
});
});
start();
</script>
</body>
</html>