-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaypal-button-express.html
215 lines (193 loc) · 5.92 KB
/
paypal-button-express.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="paypal-button-express">
<template>
<style>
:host {
display: block;
}
</style>
<iframe id="frame" width="100%" height="55px" frameBorder="0"></iframe>
</template>
</template>
<script>
/**
* @customElement
* @polymer
* @extends {Polymer.Element}
*/
class PaypalButtonExpress extends Polymer.Element {
static get is() { return 'paypal-button-express'; }
static get properties() {
return {
// whether to use sandbox mode
sandbox: {
type: Boolean,
reflectToAttribute: true,
value: false,
},
// sandbox client id (https://developer.paypal.com/developer/applications/create)
sandboxId: {
type: String,
},
// production client id
productionId: {
type: String,
},
// amount to charge
amount: {
type: Number,
},
// amount currency
currency: {
type: String,
value: 'EUR',
},
// payment reference (optional)
reference: {
type: String,
},
// whether events bubble
bubbles: {
type: Boolean,
value: false,
},
// the paypal response data
response: {
type: Object,
notify: true,
readonly: true,
},
// the iframe to render the button
_frame: {
type: Object,
},
// postMessage listener and handler
_handler: {
type: Object,
},
};
}
static get observers() {
return [
'open(sandbox, sandboxId, productionId)',
'_updateFrame(amount, currency, reference)',
]
}
// update iframe url
open() {
// bail if frame isnt initialized yet
if (!this._frame) { return; }
// bail if no ids are set
if (!this.productionId && !(this.sandbox && this.sandboxId)) { return; }
let params = new URLSearchParams();
params.set('env', this._env());
params.set('sandboxId', this.sandboxId);
params.set('productionId', this.productionId);
params.set('amount', this.amount);
params.set('currency', this.currency);
params.set('reference', this.reference);
params.set('referer', document.location.href);
this._frame.src = `${this.resolveUrl("paypal.html")}?${params.toString()}`;
}
connectedCallback() {
super.connectedCallback();
this.handleParams();
this._frame = this.$.frame;
this._handler = this._eventHandler.bind(this);
window.addEventListener("message", this._handler);
this.open();
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener("message", this._handler);
this._frame = null;
}
// detect from url params if the page is a redirect from the payment flow
async handleParams() {
// queue as micro task
await 0;
let searchParams = new URLSearchParams(window.location.search);
if (searchParams.has("paypal-window-success")) {
let data = {
// map the callback url params to the api response
paymentID: searchParams.get("paymentId"),
paymentToken: searchParams.get("token"),
payerID: searchParams.get("PayerID"),
};
this._fireSuccess(data);
this.response = data;
}
if (searchParams.has("paypal-window-error")) {
this._fireError();
}
}
_updateFrame() {
this._sendMessage('paypal-window-update', {
amount: this.amount,
currency: this.currency,
reference: this.reference,
});
}
_sendMessage(type, data) {
if (this._frame) {
this._frame.contentWindow.postMessage({
type,
data
}, `${window.location.protocol}//${window.location.host}`);
}
}
_fireSuccess(data) {
/**
* @event paypal-success Fired on succesful checkout.
*/
this.dispatchEvent(new CustomEvent('paypal-success', {
detail: data,
bubbles: this.bubbles,
composed: true,
}));
}
_fireError(data) {
/**
* @event paypal-error Fired on paypal error or window close.
*/
this.dispatchEvent(new CustomEvent('paypal-error', {
detail: data,
bubbles: this.bubbles,
composed: true,
}));
}
// data bridge recieving
_eventHandler(event) {
// bail for wrong origin
if (event.origin !== `${window.location.protocol}//${window.location.host}`) { return; }
switch(event.data.type) {
case "paypal-window-init":
this._sendMessage("paypal-window-init-ack");
break;
case "paypal-window-rendered":
this._updateFrame();
break;
case "paypal-window-success":
this._sendMessage("paypal-window-success-ack");
this.response = event.data.data;
this._fireSuccess(event.data.data);
break;
case "paypal-window-error":
this._sendMessage("paypal-window-error-ack");
this._fireError(event.data.data);
break;
case "paypal-window-close":
this._sendMessage("paypal-window-close-ack");
this._fireError("user closed window");
break;
default:
// do nothing
}
}
_env() {
return this.sandbox ? 'sandbox' : 'production';
}
}
customElements.define(PaypalButtonExpress.is, PaypalButtonExpress);
</script>
</dom-module>