show-method-postmessage-iframe.html (1266B)
1 <h1>This iframe calls shows() via postMessage()</h1> 2 <script> 3 "use strict"; 4 const defaultMethods = Object.freeze([ 5 { supportedMethods: "basic-card" }, 6 { 7 supportedMethods: "https://apple.com/apple-pay", 8 data: { 9 version: 3, 10 merchantIdentifier: "merchant.com.example", 11 countryCode: "US", 12 merchantCapabilities: ["supports3DS"], 13 supportedNetworks: ["visa"], 14 } 15 }, 16 ]); 17 18 const defaultDetails = Object.freeze({ 19 id: "fail", 20 total: { 21 label: "Total", 22 amount: { 23 currency: "USD", 24 value: "1.00", 25 }, 26 }, 27 }); 28 29 // We are going to use the id to prove that this works 30 // which we will pass back to the caller 31 window.onmessage = async event => { 32 const { source, data: { id, request } } = event; 33 switch (request) { 34 case "show-payment-request": { 35 const details = Object.assign({}, defaultDetails, { id }); 36 const request = new PaymentRequest(defaultMethods, details); 37 try { 38 const response = await request.show(); 39 source.postMessage(response.toJSON(), window.location.origin); 40 await response.complete(); 41 } catch (err) { 42 source.postMessage({ requestId: "fail" }, window.location.origin); 43 await request.abort(); 44 } 45 } 46 } 47 }; 48 49 </script>