Re: [w3c/webpayments-payment-apps-api] Add explicit permission call to allow payment app to handle payments (#94)

Here is a solution using Permission API that work nicely with multiple tabs - so if one tab disables the permission, the other tab picks it up. The solution assumes a there is a `HTMLButtonElement` element with id "request-permission" on the page. 

```JS
(async () => {
  let permissionObj;
  try {
    permissionObj = await navigator.permissions.query({ name: "payments" });
  } catch (err) {
    return; // not supported, bail out!
  }
  const button = document.getElementById("request-permission");
  const clickListener = () => {
    window.PaymentManager.requestPermission();
  }
  const changeListener = () => {
    // Monitor other windows/in case the permission changes
    if (permissionObj.state === "prompt") {
      button.disabled = false;
      button.addEventListener("click", clickListener, { once: true });
      return;
    }
    button.disabled = true;
    button.removeEventListener("click", clickListener, { once: true });
  };
  changeListener();
  permissionObj.addEventListener("change", changeListener);
})();
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webpayments-payment-apps-api/issues/94#issuecomment-275618212

Received on Friday, 27 January 2017 09:00:47 UTC