Skip to content

Commit fa446a9

Browse files
author
Jeff Harrell
committed
Late night coding can be bad
1 parent 7c12f8a commit fa446a9

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

src/paypal-button.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PAYPAL.apps = PAYPAL.apps || {};
1212

1313
var app = {},
1414
paypalURL = 'https://www.paypal.com/cgi-bin/webscr',
15+
qrCodeURL = 'https://www.paypal.com/webapps/ppint/qrcode?data={url}&pattern={pattern}&height={size}',
16+
scriptURL = 'paypal-button.min.js',
1517
bnCode = 'JavaScriptButton_{type}',
1618
prettyParams = {
1719
id: 'hosted_button_id',
@@ -27,7 +29,7 @@ PAYPAL.apps = PAYPAL.apps || {};
2729
if (!PAYPAL.apps.ButtonFactory) {
2830

2931
/**
30-
* Initial config for the app
32+
* Initial config for the app. These values can be overridden by the page.
3133
*/
3234
app.config = {
3335
labels: {
@@ -45,35 +47,34 @@ PAYPAL.apps = PAYPAL.apps || {};
4547
buynow: 0,
4648
cart: 0,
4749
hosted: 0,
48-
qr: 0
50+
qr: 0,
51+
script: 0
4952
};
5053

5154
/**
5255
* Renders a button in place of the given element
5356
*
57+
* @param business {Object} The ID or email address of the merchant to create the button for
5458
* @param raw {Object} An object of key/value data to set as button params
5559
* @param type (String) The type of the button to render
5660
* @param parent {HTMLElement} The element to add the button to (Optional)
5761
* @return {HTMLElement}
5862
*/
59-
app.create = function (raw, type, parent) {
63+
app.create = function (business, raw, type, parent) {
6064
var data = new DataStore(), button, key;
6165

62-
// Don't render without the correct data
63-
if (!raw || !raw.business) {
64-
return false;
65-
}
66+
if (!business) { return false; }
6667

6768
// Normalize the data's keys and add to a data store
6869
for (key in raw) {
6970
data.add(prettyParams[key] || key, raw[key].value, raw[key].isEditable);
7071
}
7172

72-
// Setup defaults
73+
// Defaults
7374
type = type || 'buynow';
7475

7576
// Hosted buttons
76-
if (data.hosted_button_id) {
77+
if (data.items.hosted_button_id) {
7778
type = 'hosted';
7879
data.add('cmd', '_s-xclick');
7980
// Cart buttons
@@ -85,15 +86,18 @@ PAYPAL.apps = PAYPAL.apps || {};
8586
data.add('cmd', '_xclick');
8687
}
8788

88-
// Create the button name
89+
// Add common data
90+
data.add('business', business);
8991
data.add('bn', bnCode.replace(/\{type\}/, type));
9092

9193
// Build the UI components
9294
if (type === 'qr') {
9395
button = buildQR(data, data.items.size);
9496
data.remove('size');
97+
} else if (type === 'script') {
98+
button = buildScript(data);
9599
} else {
96-
button = buildForm(type, data);
100+
button = buildForm(data, type);
97101
}
98102

99103
// Register it
@@ -115,11 +119,11 @@ PAYPAL.apps = PAYPAL.apps || {};
115119
/**
116120
* Builds the form DOM structure for a button
117121
*
118-
* @param type (String) The type of the button to render
119122
* @param data {Object} An object of key/value data to set as button params
123+
* @param type (String) The type of the button to render
120124
* @return {HTMLElement}
121125
*/
122-
function buildForm(type, data) {
126+
function buildForm(data, type) {
123127
var form = document.createElement('form'),
124128
btn = document.createElement('input'),
125129
hidden = document.createElement('input'),
@@ -198,13 +202,11 @@ PAYPAL.apps = PAYPAL.apps || {};
198202

199203
for (key in items) {
200204
item = items[key];
201-
202205
url += item.key + '=' + encodeURIComponent(item.value) + '&';
203206
}
204207

205208
url = encodeURIComponent(url);
206-
207-
img.src = 'https://www.paypal.com/webapps/ppint/qrcode?data=' + url + '&pattern=' + pattern + '&' + url + '&height=' + size;
209+
img.src = qrCodeURL.replace('{url}', url).replace('{pattern}', pattern).replace('{size}', size);
208210

209211
return img;
210212
}
@@ -279,19 +281,19 @@ PAYPAL.apps = PAYPAL.apps || {};
279281
if (typeof document !== 'undefined') {
280282
var ButtonFactory = PAYPAL.apps.ButtonFactory,
281283
nodes = document.getElementsByTagName('script'),
282-
node, data, button, business, i, len;
284+
node, data, type, business, i, len;
283285

284286
for (i = 0, len = nodes.length; i < len; i++) {
285287
node = nodes[i];
286288

287289
if (!node || !node.src) { continue; }
288290

289291
data = node && getDataSet(node);
290-
button = data && data.button;
291-
business = data.business = node.src.split('?merchant=')[1];
292+
type = data && data.button && data.button.value;
293+
business = node.src.split('?merchant=')[1];
292294

293-
if (button && business) {
294-
ButtonFactory.create(data, button, node.parentNode);
295+
if (business) {
296+
ButtonFactory.create(business, data, type, node.parentNode);
295297

296298
// Clean up
297299
node.parentNode.removeChild(node);

test/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h2>Buy Now (Spanish)</h2>
3333
data-button="buynow"
3434
data-name="Buy now!"
3535
data-amount="1.00"
36-
data-lang="es_ES"
36+
data-lang="en_US"
3737
></script>
3838
</div>
3939

@@ -42,7 +42,8 @@ <h2>Buy Now (Editable)</h2>
4242
<script src="../src/paypal-button.js?merchant=6XF3MPZBZV6HU"
4343
data-button="buynow"
4444
data-name-editable="Buy now!"
45-
data-amount-editable="1.00"
45+
data-amount="1.00"
46+
data-quantity-editable="1"
4647
data-lang="es_ES"
4748
></script>
4849
</div>

0 commit comments

Comments
 (0)