Re: [w3c/webpayments-payment-apps-api] Example 3 does not work (#82)

I don't know how crazy we want to get with example code, but I imagine people may just copy whatever is in the spec and actually use it in their app directly.

With that in mind, you may be able to shorten the example a little bit by reusing the Promise that is a result of calling fetch and its subsequent `then` calls and just letting any errors propagate through. I also added a call to remove the event listener to avoid leakage/reuse. Probably need to remove the listener if the call to open the window fails as well, but if we're not trying to make this example super robust, it's not worth bothering. So, just some tweaks -- take them or leave them.

```js
let contentType;                                                                                                                                                                                                                               
/* Handle payment request from a payee */                                                                                                                                                                                                      
self.addEventListener('paymentrequest', function(e) {                                                                                                                                                                                          
  e.respondWith(
    fetch('https://www.example.com/bobpay/process', {                                                                                                                                                                                          
        method: 'POST',                                                                                                                                                                                                                        
        body: JSON.stringify(e.data),                                                                                                                                                                                                          
      })
      .then(function(response) {                                                                                                                                                                                                               
        contentType = response.headers.get('content-type');                                                                                                                                                                                    
        if (contentType) {                                                                                                                                                                                                                    
          return response.text();                                                                                                                                                                                                              
        }
        throw new Error('No content type header.');                                                                                                                                                                                                   
      })
      .then(function(body) {                                                                                                                                                                                                                   
        if (contentType.indexOf('application/json') !== -1) {                                                                                                                                                                                  
          /* Respond to the payment request with the received body */                                                                                                                                                                          
          return JSON.parse(body);                                                                                                                                                                                                           
        }
        if (contentType.indexOf('text/html') !== -1) {                                                                                                                                                                                  
          /* Handle payment response from payment app window about to open. */                                                                                                                                                                 
          let listener;
          let promise = new Promise(function(resolve, reject) {
            self.addEventListener('message', listener = function(e) {
              self.removeEventListener('message', listener);
              if (e.data.hasOwnProperty('name')) {
                reject(e.data);
              } else {
                resolve(e.data);
              }
            });
          });
          /* Open a new payment window and populate it with the                                                                                                                                                                                
             document returned from the response */                                                                                                                                                                                            
          const url = 'data:text/html;base64,' + btoa(body);
          return clients.openWindow(url).then(function(windowClient) {                                                                                                                                                                                
              windowClient.postMessage(e.data);
              return promise;
            }).catch(function(err) {
              self.removeListener('message', listener);
              throw err;
            });
        }
        throw new Error('Unexpected value in content type header.');
      });
  }));
});
```


-- 
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/82#issuecomment-271610683

Received on Tuesday, 10 January 2017 15:47:49 UTC