self.importScripts('data.js');

// TODO: Keep state in the window instead, because a single service worker
// can talk to multiple windows.

self.changePaymentMethod = null;
self.resolver = null;
self.client = null;
self.response = response1;

self.addEventListener('message', (evt) => {
  if (evt.data === 'confirm' && self.resolver !== null) {
    self.resolver(response);
    self.resolver = null;
  } else if (evt.data === 'change-method-1' && self.changePaymentMethod !== null) {
    response = response1;
    self.changePaymentMethod(response.methodName, redact(response)).then((paymentMethodChangeResponse) => {
      self.client.postMessage(paymentMethodChangeResponse);
    }).catch((error) => {
      self.client.postMessage({error: error.message});
    });
  } else if (evt.data === 'change-method-2' && self.changePaymentMethod !== null) {
    response = response2;
    self.changePaymentMethod(response.methodName, redact(response)).then((paymentMethodChangeResponse) => {
      self.client.postMessage(paymentMethodChangeResponse);
    }).catch((error) => {
      self.client.postMessage({error: error.message});
    });
  } else {
    console.log('Unrecognized message: ' + evt.data);
  }
});

self.addEventListener('paymentrequest', (evt) => {
  if (evt.delegateToWindow) {
    evt.delegateToWindow('confirm.html');
    return;
  }

  self.changePaymentMethod = evt.changePaymentMethod;
  evt.respondWith(new Promise((resolve) => {
    self.resolver = resolve;
    self.client = evt.openWindow('confirm.html');
  }));
});

@@ -139,35 +139,35 @@
 .%0A%0Aself.
-changePaymentMethod
+paymentRequestEvent
  = null;
@@ -438,32 +438,89 @@
 thod-1' && self.
+paymentRequestEvent !== null && self.paymentRequestEvent.
 changePaymentMet
@@ -518,33 +518,24 @@
 aymentMethod
- !== null
 ) %7B%0A    resp
@@ -553,32 +553,52 @@
 onse1;%0A    self.
+paymentRequestEvent.
 changePaymentMet
@@ -877,24 +877,81 @@
 -2' && self.
+paymentRequestEvent !== null && self.paymentRequestEvent.
 changePaymen
@@ -957,25 +957,16 @@
 ntMethod
- !== null
 ) %7B%0A    
@@ -992,24 +992,44 @@
 2;%0A    self.
+paymentRequestEvent.
 changePaymen
@@ -1283,23 +1283,64 @@
     
-console.log('Un
+self.client.postMessage(%7Berror: 'Service worker did not 
 reco
@@ -1348,17 +1348,20 @@
 nize
-d
+ the
  message
 : ' 
@@ -1356,18 +1356,18 @@
  message
-:
  
+%22
 ' + evt.
@@ -1370,16 +1370,24 @@
 evt.data
+ + '%22.'%7D
 );%0A  %7D%0A%7D
@@ -1541,59 +1541,60 @@
 elf.
-changePaymentMethod = evt.changePaymentMethod;%0A  ev
+paymentRequestEvent = evt;%0A  self.paymentRequestEven
 t.re

Save event instead of function