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

I went through the code visually and fixed up a few mistakes. I have not tested it yet, so not sure whether it actually works.

```javascript
let contentType;                                                                                                                                                                                                                               
/* Handle payment request from a payee */                                                                                                                                                                                                      
self.addEventListener('paymentrequest', function(e) {                                                                                                                                                                                          
  e.respondWith(new Promise(function(resolve, reject) {                                                                                                                                                                                        
    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) {                                                                                                                                                                                                                    
          reject('No content type header.');                                                                                                                                                                                                   
        } else {                                                                                                                                                                                                                               
          return response.text();                                                                                                                                                                                                              
        }                                                                                                                                                                                                                                      
      })                                                                                                                                                                                                                                       
      .then(function(body) {                                                                                                                                                                                                                   
        if (contentType.indexOf('application/json') !== -1) {                                                                                                                                                                                  
          /* Respond to the payment request with the received body */                                                                                                                                                                          
          resolve(JSON.parse(body));                                                                                                                                                                                                           
        } else if (contentType.indexOf('text/html') !== -1) {                                                                                                                                                                                  
          /* Handle payment response from payment app window about to open. */                                                                                                                                                                 
          self.addEventListener('message', function(e) {                                                                                                                                                                                       
            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);                                                                                                                                                                                   
          clients.openWindow(url).then(function(windowClient) {                                                                                                                                                                                
              windowClient.postMessage(e.data);                                                                                                                                                                                                
            })                                                                                                                                                                                                                                 
            .catch(function(error) {                                                                                                                                                                                                           
              reject('Unable to open window.');                                                                                                                                                                                                
            });                                                                                                                                                                                                                                
        } else {                                                                                                                                                                                                                               
          reject('Unexpected value in content type header.');                                                                                                                                                                                  
        }                                                                                                                                                                                                                                      
      })                                                                                                                                                                                                                                       
      .catch(function(err) {                                                                                                                                                                                                                   
        reject(err);                                                                                                                                                                                                                           
      });                                                                                                                                                                                                                                      
  }));                                                                                                                                                                                                                                         
});                                                                                                                                                                                                                                            
```

-- 
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-271599438

Received on Tuesday, 10 January 2017 15:09:06 UTC