-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitpay-invoice-create.html
177 lines (166 loc) · 4.88 KB
/
bitpay-invoice-create.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="bitpay-invoice-create">
<template>
<style>
.error {
font-size: 0.8em;
color: var(--error-color, red);
}
</style>
<div class="error">[[ _error ]]</div>
</template>
<script>
/**
* Create Bitpay Invoices https://test.bitpay.com/api/#resource-Invoices
* @customElement
* @polymer
* @extends {Polymer.Element}
*/
class BitpayInvoiceCreate extends Polymer.Element {
static get is() { return 'bitpay-invoice-create'; }
static get properties() {
return {
// whether to automaticly create invoice on change
auto: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
// Bitpay POS auth token, signing disabled (https://bitpay.com/dashboard/merchant/api-tokens)
token: {
type: String,
},
// the full invoice object
invoice: {
type: Object,
notify: true,
},
// whether to use live or test api (https://bitpay.com/docs/testing)
testnet: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
// amount to charge
amount: {
type: Number,
},
// ISO 4217 3-character currency code
currency: {
type: String,
value: 'EUR',
},
// buyer object for refund, when email is set its not asked by bitpay (optional)
buyer: {
type: Object,
},
// your reference to the payment (transmitted via posData field)
reference: {
type: String,
},
// Indicates whether items are physical goods.
physical: {
type: Boolean,
value: false,
},
// sets item code to donation
donation: {
type: Boolean,
value: false,
},
// Webhook URL for instant payment notification (IPN)
notify: {
type: String,
},
// computed test or live url
_apiUrl: {
type: String,
computed: '__apiUrl(testnet)',
},
// api headers Object (https://test.bitpay.com/api/#making-requests)
_apiHeaders: {
type: Object,
value: () => {
return new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'x-accept-version': '2.0.0',
});
},
},
// last encountered error
_error: {
type: String,
},
// Is true when the request is processing
loading: {
type: Boolean,
value: false,
notify: true,
},
};
}
static get observers() {
return [
'_update(token, amount, currency)',
];
}
_update() {
// bail if auto is disabled
if (!this.auto) { return false; };
this.reset();
this.create().catch((error) => { this._error = error });
}
// request invoice, returns a promise
async create() {
if (!(this.amount && parseFloat(this.amount) > 0)) { throw new Error('amount is required'); };
if (!this.token) { throw new Error('token is required'); };
this.loading = true;
let payload = {
'token': this.token,
'price': this.amount,
'currency': this.currency,
'buyer': this.buyer,
'posData': this.reference,
'physical': this.physical,
'notificationURL': this.notify,
'itemCode': this.donation? 'bitcoindonation' : null,
'redirectURL': document.location.href,
};
let body;
try {
let response = await fetch(`${this._apiUrl}/invoices`, {
method: 'POST',
body: JSON.stringify(payload),
headers: this._apiHeaders,
});
body = await response.json();
} catch(error) {
throw new Error('Connection to Bitpay api failed.');
} finally {
this.loading = false;
}
if (body.data && body.data.id) {
this.invoice = body.data;
return body.data;
} else if (body.error) {
throw new Error(body.error);
} else {
throw new Error('unknow bitpay response');
}
}
__apiUrl(test) {
if (test) {
return 'https://test.bitpay.com';
} else {
return 'https://bitpay.com';
}
}
reset() {
this._error = null;
this.invoice = null;
}
}
customElements.define(BitpayInvoiceCreate.is, BitpayInvoiceCreate);
</script>
</dom-module>