| 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 items: [ |
| 18 { |
| 19 id: 'sub', |
| 20 label: 'Subtotal', |
| 21 amount: {currency: 'USD', value: '5.00'} |
| 22 }, |
| 23 { |
| 24 id: 'total', |
| 25 label: 'Total', |
| 26 amount: {currency: 'USD', value: '5.00'} |
| 27 } |
| 28 ] |
| 29 }; |
| 30 |
| 31 var request = |
| 32 new PaymentRequest(['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(true) |
| 43 .then(function() { |
| 44 print(request.shippingOption + '<br>' + |
| 45 JSON.stringify( |
| 46 toDictionary(request.shippingAddress), undefined, 2) + |
| 47 '<br>' + resp.methodName + '<br>' + |
| 48 JSON.stringify(resp.details, undefined, 2)); |
| 49 }) |
| 50 .catch(function(error) { |
| 51 print(error.message); |
| 52 }); |
| 53 }) |
| 54 .catch(function(error) { |
| 55 print(error.message); |
| 56 }); |
| 57 } catch (error) { |
| 58 print(error.message); |
| 59 } |
| 60 } |
| 61 |
| 62 /** |
| 63 * Updates the shopping cart with the appropriate shipping prices according to |
| 64 * the shipping address. |
| 65 * @param {object} details - The shopping cart. |
| 66 * @param {ShippingAddress} addr - The shipping address. |
| 67 * @return {object} The updated shopping cart. |
| 68 */ |
| 69 function updateDetails(details, addr) { |
| 70 if (addr.regionCode === 'US') { |
| 71 var shippingOption = { |
| 72 id: '', |
| 73 label: '', |
| 74 amount: {currency: 'USD', value: '0.00'} |
| 75 }; |
| 76 if (addr.administrativeArea === 'CA') { |
| 77 shippingOption.id = 'ca'; |
| 78 shippingOption.label = 'Free shipping in California'; |
| 79 details.items[details.items.length - 1].amount.value = '5.00'; |
| 80 } else { |
| 81 shippingOption.id = 'us'; |
| 82 shippingOption.label = 'Standard shipping in US'; |
| 83 shippingOption.amount.value = '5.00'; |
| 84 details.items[details.items.length - 1].amount.value = '10.00'; |
| 85 } |
| 86 if (details.items.length === 3) { |
| 87 details.items.splice(-1, 0, shippingOption); |
| 88 } else { |
| 89 details.items.splice(-2, 1, shippingOption); |
| 90 } |
| 91 details.shippingOptions = [shippingOption]; |
| 92 } else { |
| 93 delete details.shippingOptions; |
| 94 } |
| 95 return details; |
| 96 } |
| OLD | NEW |