summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/payment-handler/untrusted-event.js
blob: e067952cc3b7dd0eb74909001e49615e20a8f8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
let sender = null;

self.addEventListener('message', e => {
  sender = e.source;

  if (e.data == 'paymentrequest') {
    self.dispatchEvent(new PaymentRequestEvent('paymentrequest', {
      methodData: [{
        supportedMethods: 'https://example.com/pay'
      }],
      total: {
        currency: 'USD',
        value: '100'
      },
      modifiers: [{
        supportedMethods: 'https://example.com/pay'
      }]
    }));
  } else if (e.data == 'canmakepayment') {
    self.dispatchEvent(new CanMakePaymentEvent('canmakepayment', {
      methodData: [{
        supportedMethods: 'https://example.com/pay'
      }],
      modifiers: [{
        supportedMethods: 'https://example.com/pay'
      }]
    }));
  }
});

self.addEventListener('paymentrequest', async e => {
  const result = [];

  try {
    e.respondWith({});
  } catch (exception) {
    result.push(exception.name);
  }

  try {
    await e.openWindow('payment-app/payment.html');
  } catch (exception) {
    result.push(exception.name);
  }

  sender.postMessage(result);
});

self.addEventListener('canmakepayment', async e => {
  const result = [];

  try {
    e.respondWith({});
  } catch (exception) {
    result.push(exception.name);
  }

  sender.postMessage(result);
});