-
Notifications
You must be signed in to change notification settings - Fork 1
/
pay_with_google.html
139 lines (128 loc) · 4.19 KB
/
pay_with_google.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
<div id="checkout">
<button>Checkout</button>
</div>
<script>
if (window.PaymentRequest) {
var request = createPaymentRequest();
request.canMakePayment()
.then(function(result) {
if (result) {
addPayWithGoogleButton();
}
})
.catch(function(err) {
showErrorForDebugging(
'canMakePayment() error! ' + err.name + ' error: ' + err.message);
});
} else {
showErrorForDebugging('PaymentRequest API not available.');
}
/**
* Add a Pay with Google button alongside the existing checkout option
*/
function addPayWithGoogleButton() {
var payWithGoogleButton = document.createElement('button');
payWithGoogleButton.appendChild(document.createTextNode('Pay with Google'));
payWithGoogleButton.addEventListener('click', onPayWithGoogleClicked);
document.getElementById('checkout').appendChild(payWithGoogleButton);
}
/**
* Show Pay with Google chooser when buy button clicked
*/
function onPayWithGoogleClicked() {
createPaymentRequest()
.show()
.then(function(response) {
// Dismiss payment dialog
response.complete('success');
handlePaymentResponse(response);
})
.catch(function(err) {
showErrorForDebugging('show() error! ' + err.name + ' error: ' + err.message);
});
}
/**
* Configure support for the Google Payments API
*
* @returns {object} data attribute suitable for PaymentMethodData
*/
function getGooglePaymentsConfiguration() {
return {
// Merchant ID available after approval by Google.
//'merchantId':'<ENTER_MERCHANTID>', //only needed in prod env
'environment': 'TEST',
'apiVersion': 1,
'allowedPaymentMethods': ['CARD', 'TOKENIZED_CARD'],
'paymentMethodTokenizationParameters': {
'tokenizationType': 'PAYMENT_GATEWAY',
// Check with your payment gateway on the parameters to pass.
'parameters': {
'gateway': 'vantiv',
'vantiv:merchantPayPageId': '<ENTER_EPROTECT_PAYPAGEID>',
'vantiv:merchantOrderId': 'orderId',
'vantiv:merchantTransactionId': 'tranId',
'vantiv:merchantReportGroup': 'reportGroup'
}
},
'cardRequirements': {
'allowedCardNetworks': ['AMEX', 'DISCOVER', 'MASTERCARD', 'VISA'],
'billingAddressRequired': true,
'billingAddressFormat': 'MIN'
},
'phoneNumberRequired': true,
'emailRequired': true,
'shippingAddressRequired': true
};
}
/**
* Create a PaymentRequest
*
* @returns {PaymentRequest}
*/
function createPaymentRequest() {
// Support Google Payment API.
var methodData = [{
'supportedMethods': ['https://google.com/pay'],
'data': getGooglePaymentsConfiguration()
}];
var details = {
total: {label: 'Donation', amount: {currency: 'USD', value: '55.00'}},
displayItems: [
{
label: 'Original donation amount',
amount: {currency: 'USD', value: '65.00'}
},
{
label: 'Friends and family discount',
amount: {currency: 'USD', value: '-10.00'}
}
]
};
return new PaymentRequest(methodData, details);
}
/**
* Process a PaymentResponse
*
* @param {PaymentResponse} response returned when user approves the Pay with Google request
*/
function handlePaymentResponse(response) {
var formattedResponse = document.createElement('pre');
formattedResponse.appendChild(
document.createTextNode(JSON.stringify(response.toJSON(), null, 2)));
document.getElementById('checkout')
.insertAdjacentElement('afterend', formattedResponse);
}
/**
* Display an error message
*
* @param {string} text message to display
*/
function showErrorForDebugging(text) {
var errorDisplay = document.createElement('code');
errorDisplay.style.color = 'red';
errorDisplay.appendChild(document.createTextNode(text));
var p = document.createElement('p');
p.appendChild(errorDisplay);
document.getElementById('checkout').insertAdjacentElement('afterend', p);
}
</script>