| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 /* global PaymentRequest:false */ | |
| 8 /* global toDictionary:false */ | |
| 9 | |
| 10 /** | |
| 11 * Launches the PaymentRequest UI that offers free shipping in California and | |
| 12 * $5.00 shipping in US. Does not allow shipping outside of US. | |
| 13 */ | |
| 14 function buy() { // eslint-disable-line no-unused-vars | |
| 15 try { | |
| 16 var details = { | |
| 17 total: {label: 'Total', amount: {currency: 'USD', value: '5.00'}}, | |
| 18 displayItems: [ | |
| 19 { | |
| 20 label: 'Pending shipping price', | |
| 21 amount: {currency: 'USD', value: '0.00'}, | |
| 22 pending: true | |
| 23 }, | |
| 24 { | |
| 25 label: 'Subtotal', | |
| 26 amount: {currency: 'USD', value: '5.00'} | |
| 27 } | |
| 28 ] | |
| 29 }; | |
| 30 | |
| 31 var request = new PaymentRequest( | |
| 32 [{supportedMethods: ['visa']}], details, {requestShipping: true}); | |
| 33 | |
| 34 request.addEventListener('shippingaddresschange', function(evt) { | |
| 35 evt.updateWith(new Promise(function(resolve) { | |
| 36 resolve(updateDetails(details, request.shippingAddress)); | |
| 37 })); | |
| 38 }); | |
| 39 | |
| 40 request.show() | |
| 41 .then(function(resp) { | |
| 42 resp.complete('success') | |
| 43 .then(function() { | |
| 44 print( | |
| 45 resp.shippingOption + '<br>' + | |
| 46 JSON.stringify( | |
| 47 toDictionary(resp.shippingAddress), undefined, 2) + | |
| 48 '<br>' + resp.methodName + '<br>' + | |
| 49 JSON.stringify(resp.details, undefined, 2)); | |
| 50 }) | |
| 51 .catch(function(error) { | |
| 52 print(error); | |
| 53 }); | |
| 54 }) | |
| 55 .catch(function(error) { | |
| 56 print(error); | |
| 57 }); | |
| 58 } catch (error) { | |
| 59 print(error.message); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Updates the shopping cart with the appropriate shipping prices according to | |
| 65 * the shipping address. | |
| 66 * @param {object} details - The shopping cart. | |
| 67 * @param {ShippingAddress} addr - The shipping address. | |
| 68 * @return {object} The updated shopping cart. | |
| 69 */ | |
| 70 function updateDetails(details, addr) { | |
| 71 if (addr.country === 'US') { | |
| 72 var shippingOption = { | |
| 73 id: '', | |
| 74 label: '', | |
| 75 amount: {currency: 'USD', value: '0.00'}, | |
| 76 selected: true | |
| 77 }; | |
| 78 if (addr.region === 'CA') { | |
| 79 shippingOption.id = 'californiaShippingOption'; | |
| 80 shippingOption.label = 'Free shipping in California'; | |
| 81 details.total.amount.value = '5.00'; | |
| 82 } else { | |
| 83 shippingOption.id = 'usShippingOption'; | |
| 84 shippingOption.label = 'Standard shipping in US'; | |
| 85 shippingOption.amount.value = '5.00'; | |
| 86 details.total.amount.value = '10.00'; | |
| 87 } | |
| 88 details.displayItems.splice(0, 1, shippingOption); | |
| 89 details.shippingOptions = [shippingOption]; | |
| 90 } else { | |
| 91 delete details.shippingOptions; | |
| 92 } | |
| 93 return details; | |
| 94 } | |
| OLD | NEW |