forked from jbillimoria/JavaScriptButtons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (63 loc) · 1.75 KB
/
index.js
File metadata and controls
83 lines (63 loc) · 1.75 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
'use strict';
var DataStore = require('./util/datastore'),
factory = require('./factory'),
app = {};
app.counter = {
buynow: 0,
cart: 0,
donate: 0,
subscribe: 0
};
app.create = function (business, data, config, parent) {
var result = factory(business, data, config);
if (result) {
// Log how many buttons were created
app.counter[result.label] += 1;
// Add it to the page
if (parent) {
parent.appendChild(result.el);
}
}
return result;
};
app.process = function (el) {
var nodes = el.getElementsByTagName('script'),
node, data, business, i, len;
for (i = 0, len = nodes.length; i < len; i++) {
node = nodes[i];
if (!node || !node.src) {
continue;
}
data = new DataStore();
data.parse(node);
// If there's a merchant ID attached then it's a button of interest
if ((business = node.src.split('?merchant=')[1])) {
app.create(
business,
data,
{
type: data.pluck('type'),
label: data.pluck('button'),
size: data.pluck('size'),
style: data.pluck('style'),
host: data.pluck('host')
},
node.parentNode
);
// Clean up
node.parentNode.removeChild(node);
}
}
};
// Support node and the browser
if (typeof window === 'undefined') {
module.exports = app;
} else {
// Make the API available
if (!window.paypal) {
window.paypal = {};
window.paypal.button = app;
}
// Bind to existing scripts
window.paypal.button.process(document);
}