-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitpay-invoice-frame.html
123 lines (112 loc) · 3.18 KB
/
bitpay-invoice-frame.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="bitpay-invoice-frame">
<template>
<style>
#iframe {
width: 100%;
max-width: 500px;
height: 150px;
}
</style>
<iframe
id="iframe"
src="[[ _invoiceFrameUrl ]]"
hidden$="[[ !invoice ]]"
frameBorder="0"
scrolling="no"
allowtransparency="true"
></iframe>
</template>
<script>
/**
* Show bitpay invoice in iframe
* @customElement
* @polymer
* @extends {Polymer.Element}
*/
class BitpayInvoiceFrame extends Polymer.Element {
static get is() { return 'bitpay-invoice-frame'; }
static get properties() {
return {
// invoice to show (expects at least {url} as bitpay returns)
invoice: {
type: Object,
},
// invoice url for iframe
_invoiceFrameUrl: {
type: String,
computed: "__invoiceFrameUrl(invoice.url, dark)",
},
// whether to use dark theme (transparent background)
dark: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
// invoice status
status: {
type: String,
notify: true,
},
// whether events bubble
bubbles: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
// postMessage handler
_handler: {
type: Object,
},
// event domain for the handler
_eventOrigin: {
type: String,
computed: "__originFromUrl(invoice.url)",
},
};
}
connectedCallback() {
super.connectedCallback();
this._handler = this.__handler.bind(this);
window.addEventListener("message", this._handler);
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener("message", this._handler);
this._handler = null;
}
__handler(event) {
// bail for wrong origin
if (event.origin !== this._eventOrigin) { return; }
if (event.data && event.data.status) {
this.status = event.data.status;
this.invoice.status = event.data.status;
if (event.data.status === "paid") {
/**
* @event bitpay-invoice-paid Fired on successful payment
*/
this.dispatchEvent(new CustomEvent('bitpay-invoice-paid', {
detail: this.invoice,
bubbles: this.bubbles,
composed: true,
}));
}
}
}
__invoiceFrameUrl(url, dark) {
if (url) {
return `${url}&view=iframe${dark ? '&theme=dark' : ''}`;
} else {
return 'about:blank';
}
}
__originFromUrl(url) {
if (!url) { return null; };
let link = document.createElement('a');
link.setAttribute('href', url);
return `${link.protocol}//${link.hostname}`
}
}
customElements.define(BitpayInvoiceFrame.is, BitpayInvoiceFrame);
</script>
</dom-module>