forked from jbillimoria/JavaScriptButtons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaypal-button.js
More file actions
226 lines (175 loc) · 4.65 KB
/
Copy pathpaypal-button.js
File metadata and controls
226 lines (175 loc) · 4.65 KB
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
216
217
218
219
220
221
222
223
224
225
226
if (typeof PAYPAL === 'undefined' || !PAYPAL) {
var PAYPAL = {};
}
PAYPAL.apps = PAYPAL.apps || {};
(function () {
'use strict';
var app = {},
paypalURL = 'https://www.paypal.com/cgi-bin/webscr',
prettyParams = {
id: 'hosted_button_id',
name: 'item_name',
number: 'item_number'
},
buttonImgs = {
buynow: '//www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif',
cart: '//www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif',
basic: '//www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif'
};
if (!PAYPAL.apps.ButtonFactory) {
/**
* A count of each type of button on the page
*/
app.buttons = {
buy: 0,
cart: 0,
qr: 0,
api: 0
};
/**
* Renders a button in place of the given element
*
* @param data {Object} An object of key/value data to set as button params
* @param type (String) The type of the button to render
* @param parent {HTMLElement} The element to add the button to (Optional)
* @return {HTMLElement}
*/
app.create = function (data, type, parent) {
var normalized = {}, button, key;
// Don't render without the correct data
if (!data || !data.business) {
return false;
}
// Normalize the data's keys
for (key in data) {
normalized[prettyParams[key] || key] = data[key];
}
// Hosted buttons
if (normalized.hosted_button_id) {
normalized.cmd = '_s-xclick';
// Cart buttons
} else if (type === 'cart') {
normalized.cmd = '_cart';
normalized.add = true;
// Plain text buttons
} else {
normalized.cmd = '_xclick';
}
// Build the UI components
if (type === 'qr') {
button = buildQR(normalized);
} else {
button = buildForm(type, normalized);
}
// Register it
this.buttons[type] += 1;
// Add it to the DOM
if (parent) {
parent.appendChild(button);
}
return button;
};
PAYPAL.apps.ButtonFactory = app;
}
/**
* Builds the form DOM structure for a button
*
* @param type (String) The type of the button to render
* @param data {Object} An object of key/value data to set as button params
* @return {HTMLElement}
*/
function buildForm(type, data) {
var form = document.createElement('form'),
btn = document.createElement('input'),
hidden = document.createElement('input'),
input, key;
btn.type = 'image';
hidden.type = 'hidden';
form.method = 'post';
form.action = paypalURL;
form.appendChild(btn);
btn.src = getButtonImg(type);
for (key in data) {
input = hidden.cloneNode(true);
input.name = key;
input.value = data[key];
form.appendChild(input);
}
// If the Mini Cart is present then register the form
if (PAYPAL.apps.MiniCart && data.cmd === '_cart') {
var MiniCart = PAYPAL.apps.MiniCart;
if (!MiniCart.UI.itemList) {
MiniCart.render();
}
MiniCart.bindForm(form);
}
return form;
}
/**
* Builds the image for a QR code
*
* @param data {Object} An object of key/value data to set as button params
* @return {HTMLElement}
*/
function buildQR(data) {
var img = document.createElement('img'),
size = data.size || 250,
url = paypalURL + '?',
pattern = 13,
key;
for (key in data) {
url += key + '=' + data[key] + '&';
}
url = encodeURIComponent(url);
img.src = 'https://www.paypal.com/webapps/ppint/qrcode?data=' + url + '&pattern=' + pattern + '&' + url + '&height=' + size;
return img;
}
/**
* Utility function to return the rendered button image URL
*
* @param type {String} The type of button to render
* @return {String}
*/
function getButtonImg(type) {
return buttonImgs[type] || buttonImgs.basic;
}
/**
* Utility function to polyfill dataset functionality for browsers
*
* @param el {HTMLElement} The element to check
* @return {Object}
*/
function getDataSet(el) {
var dataset = el.dataset,
attrs, attr, matches, len, i;
if (!dataset) {
dataset = {};
if ((attrs = el.attributes)) {
for (i = 0, len = attrs.length; i < len; i++) {
attr = attrs[i];
if ((matches = /^data-(.+)/.exec(attr.name))) {
dataset[matches[1]] = attr.value;
}
}
}
}
return dataset;
}
// Init the buttons
if (typeof document !== 'undefined') {
var ButtonFactory = PAYPAL.apps.ButtonFactory,
nodes = document.getElementsByTagName('script'),
node, data, button, business, i, len;
for (i = 0, len = nodes.length; i < len; i++) {
node = nodes[i];
data = node && getDataSet(node);
button = data && data.button;
business = data.business = node.src.split('?merchant=')[1];
if (button && business) {
ButtonFactory.create(data, button, node.parentNode);
// Clean up
node.parentNode.removeChild(node);
}
}
}
}());