Revision 5a9c1d6873bebdf3231a839eb634b1d7cb26e9b7 authored by Luke Bjerring on 04 April 2018, 15:18:10 UTC, committed by Luke Bjerring on 04 April 2018, 19:25:25 UTC
1 parent 53404f2
Raw File
show-method-postmessage-iframe.html
<h1>This iframe calls shows() via postMessage()</h1>
<script>
"use strict";
const defaultMethods = Object.freeze([
  { supportedMethods: "basic-card" },
  { supportedMethods: "https://apple.com/pay" },
]);

const defaultDetails = Object.freeze({
  id: "fail",
  total: {
    label: "Total",
    amount: {
      currency: "USD",
      value: "1.00",
    },
  },
});

// We are going to use the id to prove that this works
// which we will pass back to the caller
window.onmessage = async event => {
  const { source, data: { id, request } } = event;
  switch (request) {
    case "show-payment-request": {
      const details = Object.assign({}, defaultDetails, { id });
      const request = new PaymentRequest(defaultMethods, details);
      try {
        const response = await request.show();
        source.postMessage(response.toJSON(), window.location.origin);
        await response.complete();
      } catch (err) {
        source.postMessage({ requestId: "fail" }, window.location.origin);
        await request.abort();
      }
    }
  }
};

</script>
back to top