-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
160 lines (145 loc) · 5.43 KB
/
index.html
File metadata and controls
160 lines (145 loc) · 5.43 KB
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
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<!-- Load the required components. -->
<script src="https://js.braintreegateway.com/web/3.60.0/js/client.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.60.0/js/paypal-checkout.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.60.0/js/data-collector.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="paypal-button" style="display: none;"></div><br>
<div id="result"></div><br>
<div id="sale_result"></div><br>
<script type="text/javascript">
$.get( "getToken.php", function( data ) {
console.log(data);
var clientToken = data;
braintree.client.create(
{
authorization: clientToken
//authorization: 'authorization'
},
function (clientErr, clientInstance) {
//console.log(clientInstance);
braintree.dataCollector.create(
{
client: clientInstance,
paypal: true
},
function (err, dataCollectorInstance) {
if (err) {
// Handle error
return;
}
// At this point, you should access the dataCollectorInstance.deviceData value and provide it
// to your server, e.g. by injecting it into your form as a hidden input.
myDeviceData = dataCollectorInstance.deviceData;
}
);
// Stop if there was a problem creating the client.
// This could happen if there is a network error or if the authorization
// is invalid.
if (clientErr) {
console.error('Error creating client:', clientErr);
return;
}
// Create a PayPal Checkout component.
braintree.paypalCheckout.create(
{
client: clientInstance
},
function (paypalCheckoutErr, paypalCheckoutInstance) {
// Stop if there was a problem creating PayPal Checkout.
// This could happen if there was a network error or if it's incorrectly
// configured.
if (paypalCheckoutErr) {
console.error('Error creating PayPal Checkout:', paypalCheckoutErr);
return;
}
// Set up PayPal with the checkout.js library
paypal.Button.render({
locale: 'en_US',//PayPal button language, he_IL for Hebrew
env: 'sandbox', // or 'production'
commit: true, //"Pay now" label
payment: function () {
return paypalCheckoutInstance.createPayment({
flow: 'checkout', // Required
intent: 'sale',
locale: 'en_US', //popup language, he_IL for Hebrew
amount: 14.44, // Required
currency: 'USD', // Required
landingPageType: 'login',//billing is for guest, login is for PayPal
enableShippingAddress: true, //get the shipping address from the PayPal account
shippingAddressEditable: true,
shippingAddressOverride: {
recipientName: 'Tarun Agarwal',
line1: '1101 S Main St',
line2: '#310',
city: 'Milpitas',
countryCode: 'US',
postalCode: '95035',
state: 'CA',
}
});
},
validate: function(actions) {
console.log("validate called");
document.getElementById("paypal-button").style.display='';
//actions.disable(); // Allow for validation in onClick()
paypalActions = actions; // Save for later enable()/disable() calls
},
onClick:function (){
console.log('onClick called')
//paypalActions.enable();
},
onAuthorize: function (data, actions) {
console.log(data);
return paypalCheckoutInstance.tokenizePayment(data)
.then(function (payload) {
// Submit `payload.nonce` to your server
//this $.ajaxSetup step is optional, but will save you a bunch of caching and asynchrony problems.
console.log(payload);
$.ajaxSetup({
cache: false,
async: false
});
var orderID = Math.floor(Math.random() * 10000) + 1;
console.log('order ID='+orderID);
$.ajax({
type: "POST",
url: 'transaction.php',
data: {"payment_method_nonce": payload.nonce, "amount": 14.44, "orderId": orderID, "customField": "PayPal custom", "description": "test buyer"},
success: function(response){
console.log('Data submit worked. Response was:\n' + response);
$('#sale_result').html(response);
}
});
}).catch(function(error){
if (error === 'INSTRUMENT_DECLINED') {
actions.restart();
}
});
},
onCancel: function (data) {
console.log('checkout.js payment cancelled', JSON.stringify(data, 0, 2));
window.location = "http://www.example.com";
},
onError: function (err) {
console.error('checkout.js error', err);
}
}, '#paypal-button').then(function () {
//console.log('finish rendering');
//document.getElementById("paypal-button").style.display='';
//console.log($('#paypal-button'));
// The PayPal button will be rendered in an html element with the id
// `paypal-button`. This function will be called when the PayPal button
// is set up and ready to be used.
});
});
});
});
</script>
</body>