summaryrefslogtreecommitdiffstats
path: root/dom/payments/test/simple_payment_request.html
blob: b532ba6101b0323a4becec5977ad74c5b52b9d0e (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!doctype html>
<meta charset="utf-8">
<title>Payment Request Testing</title>
<script>
const methods = [
  {
    supportedMethods: "basic-card",
  },
];
const details = {
  id: "simple details",
  total: {
    label: "Donation",
    amount: { currency: "USD", value: "55.00" },
  },
};
const updatedDetails = {
  id: "simple details",
  total: {
    label: "Donation",
    amount: { currency: "USD", value: "55.00" },
  },
  error: "",
};

let request;
let shippingChangedEvent;

let msg = "successful";
try {
  request = new PaymentRequest(methods, details);
  request.onshippingoptionchange = (event) => {
    shippingChangedEvent = event;
    window.parent.postMessage("successful", "*");
  };
  request.onshippingaddresschange = (event) => {
    shippingChangedEvent = event;
    window.parent.postMessage("successful", "*");
  };

} catch (err) {
  msg = err.name;
}
window.parent.postMessage(msg, "*");


if (request) {
  window.onmessage = async ({ data: action }) => {
    msg = "successful";
    switch (action) {
      case "show PaymentRequest":
        const responsePromise = request.show();
        window.parent.postMessage(msg, "*");
        try {
          await responsePromise;
        } catch (err) {
          if (err.name !== "AbortError") {
            msg = err.name;
          }
        }
        window.parent.postMessage(msg, "*")
        break;
      case "updateWith PaymentRequest":
        if (shippingChangedEvent) {
          try {
            shippingChangedEvent.updateWith(updatedDetails);
          } catch(err) {
            if (err.name !== "InvalidStateError") {
              msg = err.name;
            }
          }
          window.parent.postMessage(msg, "*");
          shippingChangedEvent = undefined;
        }
        break;
      default:
        window.parent.postMessage(`fail - unknown postmessage action: ${action}`, "*");
    }
  };
}
</script>