|
| 1 | +/** |
| 2 | + * The callback for successful creation of PaymentRequest. |
| 3 | + * |
| 4 | + * @callback successCallback |
| 5 | + * @param {PaymentRequest} request The newly created instance of PaymentRequest |
| 6 | + * object. |
| 7 | + * @param {string} optionalWarning The optional warning message to be used, for |
| 8 | + * example, when unable to determine whether the browser can make payments. |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * The callback for failed creation of PaymentRequest. |
| 13 | + * |
| 14 | + * @callback failureCallback |
| 15 | + * @param {string} error The message indicating the reason why an instance of |
| 16 | + * PaymentRequest was not created. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Asynchronously builds PaymentRequest for both Android Pay and credit card |
| 21 | + * payments, but does not show any UI yet. Succeeds only if can make payments. |
| 22 | + * If you encounter issues when running your own copy of this sample, run 'adb |
| 23 | + * logcat | grep Wallet' to see detailed error messages. |
| 24 | + * |
| 25 | + * @param {successCallback} onSuccess The callback to invoke when this function |
| 26 | + * is finished successfully. |
| 27 | + * @param {failureCallback} onFailure The callback to invoke when this function |
| 28 | + * is finished with failure. |
| 29 | + */ |
| 30 | +function initPaymentRequest(onSuccess, onFailure) { |
| 31 | + if (!navigator.userAgent.match(/Android/i)) { |
| 32 | + onFailure('Supported only on Android for now.'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (!('PaymentRequest' in window)) { |
| 37 | + onFailure('This browser does not support web payments.'); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + let networks = ['amex', 'diners', 'discover', 'jcb', 'mastercard', 'unionpay', |
| 42 | + 'visa', 'mir']; |
| 43 | + let types = ['debit', 'credit', 'prepaid']; |
| 44 | + let supportedInstruments = [{ |
| 45 | + supportedMethods: ['https://android.com/pay'], |
| 46 | + data: { |
| 47 | + merchantName: 'Android Pay Demo', |
| 48 | + // Place your own Android Pay merchant ID here. The merchant ID is tied to |
| 49 | + // the origin of the website. |
| 50 | + merchantId: '00184145120947117657', |
| 51 | + // If you do not yet have a merchant ID, uncomment the following line. |
| 52 | + // environment: 'TEST', |
| 53 | + allowedCardNetworks: ['AMEX', 'DISCOVER', 'MASTERCARD', 'VISA'], |
| 54 | + paymentMethodTokenizationParameters: { |
| 55 | + tokenizationType: 'GATEWAY_TOKEN', |
| 56 | + parameters: { |
| 57 | + 'gateway': 'stripe', |
| 58 | + // Place your own Stripe publishable key here. Use a matching Stripe |
| 59 | + // secret key on the server to initiate a transaction. |
| 60 | + 'stripe:publishableKey': 'pk_live_lNk21zqKM2BENZENh3rzCUgo', |
| 61 | + 'stripe:version': '2016-07-06', |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, { |
| 66 | + supportedMethods: networks, |
| 67 | + }, { |
| 68 | + supportedMethods: ['basic-card'], |
| 69 | + data: {supportedNetworks: networks, supportedTypes: types}, |
| 70 | + }]; |
| 71 | + |
| 72 | + let details = { |
| 73 | + total: {label: 'Donation', amount: {currency: 'USD', value: '55.00'}}, |
| 74 | + displayItems: [ |
| 75 | + { |
| 76 | + label: 'Original donation amount', |
| 77 | + amount: {currency: 'USD', value: '65.00'}, |
| 78 | + }, |
| 79 | + { |
| 80 | + label: 'Friends and family discount', |
| 81 | + amount: {currency: 'USD', value: '-10.00'}, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }; |
| 85 | + |
| 86 | + let request = new PaymentRequest(supportedInstruments, details); |
| 87 | + |
| 88 | + if (request.canMakePayment) { |
| 89 | + request.canMakePayment().then(function(result) { |
| 90 | + if (result) { |
| 91 | + onSuccess(request); |
| 92 | + } else { |
| 93 | + onFailure('Cannot make payment'); |
| 94 | + } |
| 95 | + }).catch(function(err) { |
| 96 | + onSuccess(request, err); |
| 97 | + }); |
| 98 | + } else { |
| 99 | + onSuccess( |
| 100 | + request, 'This browser does not support "can make payment" query'); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Invokes PaymentRequest for Android Pay. |
| 106 | + * |
| 107 | + * @param {PaymentRequest} request The PaymentRequest object. |
| 108 | + */ |
| 109 | +function onBuyClicked(request) { |
| 110 | + request.show().then(function(instrumentResponse) { |
| 111 | + sendPaymentToServer(instrumentResponse); |
| 112 | + }) |
| 113 | + .catch(function(err) { |
| 114 | + ChromeSamples.setStatus(err); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Simulates processing the payment data on the server. |
| 120 | + * |
| 121 | + * @param {PaymentResponse} instrumentResponse The payment information to |
| 122 | + * process. |
| 123 | + */ |
| 124 | +function sendPaymentToServer(instrumentResponse) { |
| 125 | + // There's no server-side component of these samples. No transactions are |
| 126 | + // processed and no money exchanged hands. Instantaneous transactions are not |
| 127 | + // realistic. Add a 2 second delay to make it seem more real. |
| 128 | + window.setTimeout(function() { |
| 129 | + instrumentResponse.complete('success') |
| 130 | + .then(function() { |
| 131 | + document.getElementById('result').innerHTML = |
| 132 | + instrumentToJsonString(instrumentResponse); |
| 133 | + }) |
| 134 | + .catch(function(err) { |
| 135 | + ChromeSamples.setStatus(err); |
| 136 | + }); |
| 137 | + }, 2000); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Converts the payment instrument into a JSON string. |
| 142 | + * |
| 143 | + * @param {PaymentResponse} instrument The instrument to convert. |
| 144 | + * @return {string} The JSON string representation of the instrument. |
| 145 | + */ |
| 146 | +function instrumentToJsonString(instrument) { |
| 147 | + if (instrument.toJSON) { |
| 148 | + return JSON.stringify(instrument, undefined, 2); |
| 149 | + } else { |
| 150 | + return JSON.stringify({ |
| 151 | + methodName: instrument.methodName, |
| 152 | + details: instrument.details, |
| 153 | + }, undefined, 2); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Initializes the buy button. |
| 159 | + * |
| 160 | + * @param {HTMLElement} buyButton The "Buy" button to initialize. |
| 161 | + */ |
| 162 | +function initBuyButton(buyButton) { |
| 163 | + initPaymentRequest(function(request, optionalWarning) { |
| 164 | + buyButton.setAttribute('style', 'display: inline;'); |
| 165 | + ChromeSamples.setStatus(optionalWarning ? optionalWarning : ''); |
| 166 | + buyButton.addEventListener('click', function handleClick() { |
| 167 | + buyButton.removeEventListener('click', handleClick); |
| 168 | + onBuyClicked(request); |
| 169 | + initBuyButton(buyButton); |
| 170 | + }); |
| 171 | + }, function(error) { |
| 172 | + buyButton.setAttribute('style', 'display: none;'); |
| 173 | + ChromeSamples.setStatus(error); |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +const buyButton = document.getElementById('buyButton'); |
| 178 | +buyButton.setAttribute('style', 'display: none;'); |
| 179 | +ChromeSamples.setStatus('Checking...'); |
| 180 | +initBuyButton(buyButton); |
0 commit comments