OLD | NEW |
1 <button onclick="buy()" | 1 <button onclick="buy()" |
2 id="buy" | 2 id="buy" |
3 style="width: 50%; | 3 style="width: 50%; |
4 height: 50%; | 4 height: 50%; |
5 font-size: 3em;">Buy</button> | 5 font-size: 3em;">Buy</button> |
6 <script> | 6 <script> |
7 function print(msg) { | 7 function print(msg) { |
8 document.open(); | 8 document.open(); |
9 document.write('<pre id="result" style="font-size: 3em;">' + msg + '</pre>'); | 9 document.write('<pre id="result" style="font-size: 3em;">' + msg + '</pre>'); |
10 document.close(); | 10 document.close(); |
11 } | 11 } |
12 function toDictionary(addr) { | 12 function toDictionary(addr) { |
13 var dict = {}; | 13 var dict = {}; |
14 dict.regionCode = addr.regionCode; | 14 dict.regionCode = addr.regionCode; |
15 dict.administrativeArea = addr.administrativeArea; | 15 dict.administrativeArea = addr.administrativeArea; |
16 dict.locality = addr.locality; | 16 dict.locality = addr.locality; |
17 dict.dependentLocality = addr.dependentLocality; | 17 dict.dependentLocality = addr.dependentLocality; |
18 dict.addressLine = addr.addressLine; | 18 dict.addressLine = addr.addressLine; |
19 dict.postalCode = addr.postalCode; | 19 dict.postalCode = addr.postalCode; |
20 dict.sortingCode = addr.sortingCode; | 20 dict.sortingCode = addr.sortingCode; |
21 dict.languageCode = addr.languageCode; | 21 dict.languageCode = addr.languageCode; |
22 dict.organization = addr.organization; | 22 dict.organization = addr.organization; |
23 dict.recipient = addr.recipient; | 23 dict.recipient = addr.recipient; |
24 return dict; | 24 return dict; |
25 } | 25 } |
26 function buy() { | 26 function buy() { |
27 try { | 27 try { |
28 var request = new PaymentRequest(['visa'], { | 28 var details = { |
29 'items' : [ { | 29 'items' : [ { |
30 'id' : 'total', | 30 'id' : 'total', |
31 'label' : 'Total', | 31 'label' : 'Total', |
32 'amount' : {'currencyCode' : 'USD', 'value' : '5.00'} | 32 'amount' : {'currencyCode' : 'USD', 'value' : '5.00'} |
33 } ], | |
34 'shippingOptions' : [ { | |
35 'id' : 'freeShippingOption', | |
36 'label' : 'Free global shipping', | |
37 'amount' : {'currencyCode' : 'USD', 'value' : '0'} | |
38 } ] | 33 } ] |
39 }, | 34 }; |
40 {'requestShipping' : true}); | 35 var request = |
| 36 new PaymentRequest(['visa'], details, {'requestShipping' : true}); |
| 37 request.addEventListener("shippingaddresschange", function(e) { |
| 38 e.updateWith(new Promise(function(resolve, reject) { |
| 39 details['shippingOptions'] = [ { |
| 40 'id' : 'calculatedShippingOption', |
| 41 'label' : 'Standard shipping', |
| 42 'amount' : {'currencyCode' : 'USD', 'value' : '1.00'} |
| 43 } ]; |
| 44 resolve(details); |
| 45 })); |
| 46 }); |
41 request.show() | 47 request.show() |
42 .then(function(resp) { | 48 .then(function(resp) { |
43 resp.complete(true) | 49 resp.complete(true) |
44 .then(function() { | 50 .then(function() { |
45 print(request.shippingOption + '<br>' + | 51 print(request.shippingOption + '<br>' + |
46 JSON.stringify(toDictionary(request.shippingAddress), | 52 JSON.stringify(toDictionary(request.shippingAddress), |
47 undefined, 2) + | 53 undefined, 2) + |
48 '<br>' + resp.methodName + '<br>' + | 54 '<br>' + resp.methodName + '<br>' + |
49 JSON.stringify(resp.details, undefined, 2)); | 55 JSON.stringify(resp.details, undefined, 2)); |
50 }) | 56 }) |
51 .catch(function(error) { print(error.message); }); | 57 .catch(function(error) { print(error.message); }); |
52 }) | 58 }) |
53 .catch(function(error) { print(error.message); }); | 59 .catch(function(error) { print(error.message); }); |
54 } catch (error) { | 60 } catch (error) { |
55 print(error.message); | 61 print(error.message); |
56 } | 62 } |
57 } | 63 } |
58 </script> | 64 </script> |
OLD | NEW |