This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
checkout.php
405 lines (343 loc) · 13.4 KB
/
checkout.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
require_once ("../include/incl.header.php");
require_once ("../include/incl.conf.php");
$item_price = (isset($_POST["item-price"])) ? $_POST["item-price"] : "";
$currency = (isset($_POST["currency"])) ? $_POST["currency"] : "USD";
$locale = (isset($_POST["locale"])) ? $_POST["locale"] : "en_US";
$loc = explode('/', $_SERVER['REQUEST_URI'])[1];
?>
<!-- Reference the required client SDKs -->
<script src="<?php echo BT_JAVASCRIPT_CLIENT ?>"></script>
<script src="<?php echo BT_JAVASCRIPT_PAYPAL ?>"></script>
<script src="<?php echo PP_CHECKOUT ?>"></script>
<div class="container-fluid">
<div class="row">
<div class="col-sm-5">
<div class="divBorder" style="min-height: 450px;">
<h3>Checkout</h3>
<div style="text-align: center;">
<h4>Ship to</h4>
</div>
<form class="form-horizontal" id="bt-payPalButton-form"
action="bt-transaction.php" method="post">
<input type="hidden" name="checkout" value="pay-now" /> <input
type="hidden" name="payment-method-nonce"
class="payment-method-nonce" /> <input type="hidden"
name="item-description" value="DSLR Camera, Auto-focus, A0123" />
<input type="hidden" name="currency" value="<?php echo $currency ?>"/>
<input type="hidden" name="locale" value="<?php echo $locale ?>"/>
<input
type="hidden" id="item-price-original" name="item-price-original"
value="<?php echo $item_price ?>" /> <input type="hidden"
id="total-amount" name="total-amount"
value="<?php echo $item_price ?>" /> <input type="hidden"
id="recipient" name="recipient" value="" />
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">First Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="first-name"
name="first-name" value="Jane">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Last Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="last-name"
name="last-name" value="Doe">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Address 1</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="line1" name="line1"
value="55 East 52nd Street">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Address 2</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="line2" name="line2"
value="21st Floor">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">City</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="city" name="city"
value="New York">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">State</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="state" name="state"
value="NY">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Postal Code</label>
<div class="col-sm-9">
<input class="form-control" type="text" id="postal-code"
name="postal-code" value="10022">
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Country</label>
<div class="col-sm-9">
<select class="form-control input-sm" id="country-code"
name="country-code">
<option value="AU">Australia</option>
<option value="BR">Brazil</option>
<option value="CA">Canada</option>
<option value="CZ">Czech Republic</option>
<option value="DK">Denmark</option>
<option value="HK">Hong Kong</option>
<option value="HU">Hungary</option>
<option value="IL">Israel</option>
<option value="JP">Japan</option>
<option value="MY">Malaysia</option>
<option value="MX">Mexico</option>
<option value="NO">Norway</option>
<option value="NZ">New Zealand</option>
<option value="PH">Philippines</option>
<option value="PL">Poland</option>
<option value="GB">United Kingdom</option>
<option value="RU">Russia</option>
<option value="SG">Singapore</option>
<option value="SE">Sweden</option>
<option value="CH">Switzerland</option>
<option value="TH">Thailand</option>
<option value="TW">Taiwan</option>
<option selected value="US">United States</option>
</select>
</div>
</div>
<div class="form-group form-group-sm" style="margin-bottom: 4px;">
<label class="col-sm-3 control-label">Shipping Type</label>
<div class="col-sm-9">
<select class="form-control input-sm" id="shipping-method"
name="shipping-method" onChange="updateTotalAmount(this)">
<optgroup label="United Parcel Service"
style="font-style: normal;">
<option value="15.00">Worldwide Expedited - 15.00</option>
<option value="10.00">Worldwide Express Saver - 10.00</option>
</optgroup>
<optgroup label="Ground" style="font-style: normal;">
<option selected="selected" value="5.00">Ground - 5.00</option>
</optgroup>
</select>
</div>
</div>
</form>
<hr />
<div>
<h4 style="text-align: center;">Payment methods</h4>
<div style="text-align: center;">
<span style="font-size: 12pt;">Total amount: </span> <span
id="total-amount-display" style="font-size: 12pt;"></span> <span
<span style="font-size: 12pt;"><?php echo $currency ?></span>
</div>
<div id="payment-methods" style="width: 280px; margin: 0 auto;">
<!-- Render the radio fields and button containers -->
<label> <input name="payment-option" type="radio"
onclick="toggle('continue-div', 'papbutton-div');" checked
value="paypal"> <img class="mark" alt="Pay with Paypal"
src="../img/paypal-mark.jpg">
</label> <br /> <label> <input name="payment-option" type="radio"
onclick="toggle('papbutton-div', 'continue-div');" value="card">
<img class="mark"
alt="Accepting Visa, Mastercard, Discover and American Express"
src="../img/card-mark.png">
</label>
<script>
// radio button toggle
function toggle (hide, show) {
$('.' + hide).css('display', 'none');
$('.' + show).css('display', 'inline-block');
}
</script>
<br /> <br />
<!-- -----------------------
PayPal Checkout button
-->
<div class="papbutton-div">
<div id="papbutton" style="padding-left: 10px;"></div>
</div>
<div class="continue-div" id="card-button-container"
style="display: none;">
<button>Continue</button>
<i>Please select the PayPal Mark.</i>
</div>
</div>
<!-- loader icon while server is processing transaction -->
<div style="width: 100%; text-align: center;">
<div class="loader" style="display: none;"></div>
</div>
</div>
</div>
</div>
<!-- --------- Payment flow diagram section --------- -->
<div class="col-sm-7">
<div class="divBorder" style="min-height: 450px;">
<h3>Payment Flow</h3>
<img class="img-responsive" id="flow-step"
src="../img/pay-now-flow-2.png">
</div>
</div>
<div class="col-xs-12">
<div style="text-align: center;">
<h3>
Return to <a href='../index.php'>Home Page</a>
</h3>
</div>
</div>
</div>
</div>
<script>
// authorization from server
var authorization = '<?php
require_once ("../include/incl.conf.php");
require_once (BRAINTREE_PHP_SDK1);
$gateway = new Braintree_Gateway(array(
'accessToken' => ACCESS_TOKEN
));
echo ($clientToken = $gateway->clientToken()->generate());
?>'
// Checkout with PayPal
var ppForm = document.getElementById('bt-payPalButton-form');
var btPayPalButton = document.getElementById('papbutton');
braintree.client.create({
authorization: authorization
}, function(clientErr, clientInstance) {
// Handle error in client creation
if (clientErr) {
console.log('Error creating client instance: ' + clientErr);
return;
}
/*
Braintree - PayPal button component
*/
braintree.paypalCheckout.create({
client: clientInstance
}, function (createErr, paypalCheckoutInstance) {
if (createErr) {
if (createErr.code === 'PAYPAL_BROWSER_NOT_SUPPORTED') {
console.error('This browser is not supported.');
}
else {
console.error('Error!', createErr);
}
return;
}
paypal.Button.render({
// sandbox or production
env: '<?php echo ENVIRONMENT ?>',
locale: getLocale(),
style: {
size: 'medium',
color: 'gold',
shape: 'pill',
label: 'checkout'
},
payment: function () {
// paypal min-browser opens
$("#flow-step").attr("src","../img/pay-now-flow-3.png");
return paypalCheckoutInstance.createPayment({
flow: 'checkout',
intent: 'sale',
amount: getAmount(),
currency: getCurrency(),
locale: getLocale(),
displayName: 'Demo Portal Test Store',
enableShippingAddress: true,
shippingAddressEditable: false,
shippingAddressOverride: {
recipientName: getRecipientName(),
line1: getLine1(),
line2: getLine2(),
city: getCity(),
countryCode: getCountryCode(),
postalCode: getPostalCode(),
state: getState()
}
});
},
commit: true, // Show a 'Pay Now' button in the checkout flow
onAuthorize: function (data, actions) {
return paypalCheckoutInstance.tokenizePayment(data, function (err1, payload) {
// Pay Now clicked in PayPal mini-browser
if (err1) {
console.error('onAuthorize: ', err1);
}
else {
console.log('Paypal nonce: ', payload.nonce );
// hide payment methods
$('#payment-methods').css('display', 'none');
// show spinner
$('.loader').css('display', 'inline-block');
// Put `payload.nonce` into the 'payment-method-nonce' input, then submit the form
$('.payment-method-nonce').val(payload.nonce);
ppForm.submit();
}
});
},
onCancel: function (data) {
//console.log('checkout.js payment cancelled', JSON.stringify(data, 0, 2));
var currentUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
var cancelUrl = currentUrl.substring(0, (currentUrl.lastIndexOf('/') - 3)) + '/include/cancel.php';
window.location.href = cancelUrl;
},
onError: function (err) {
console.error('checkout.js error', err.toString());
var currentUrl = window.location.href = window.location.protocol + '//' + window.location.host + window.location.pathname;
var errorUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + '/error.php?message='+err.toString();
window.location.href = errorUrl;
}
}, '#papbutton'); // the PayPal button will be rendered in an html element with the id `papbutton`
});
});
</script>
<script>
updateTotalAmount(document.getElementById("shipping-method"));
// adjust total amount based on shipping
function updateTotalAmount(that) {
var totalAmount = (Number(document.getElementById("item-price-original").value) + Number(that.value)).toFixed(2);
document.getElementById("total-amount").value = totalAmount;
document.getElementById("total-amount-display").innerHTML = totalAmount;
}
// update "paypalCheckoutInstance.createPayment" with current "Ship to" data prior to payment
function getRecipientName() {
document.getElementById("recipient").value = document.getElementById("first-name").value + ' ' + document.getElementById("last-name").value;
return (document.getElementById("first-name").value + ' ' + document.getElementById("last-name").value);
}
function getLine1() {
return (document.getElementById("line1").value);
}
function getLine2() {
return (document.getElementById("line2").value);
}
function getCity() {
return (document.getElementById("city").value);
}
function getState() {
return (document.getElementById("state").value);
}
function getPostalCode() {
return (document.getElementById("postal-code").value);
}
function getCountryCode() {
return (document.getElementById("country-code").value);
}
function getAmount() {
return (document.getElementById("total-amount").value);
}
function getCurrency() {
var currency ="<?php echo $currency; ?>";
return (currency);
}
function getLocale() {
var locale ="<?php echo $locale; ?>";
return (locale);
}
</script>
<?php require_once("../include/incl.footer.php"); ?>