Skip to content

Commit a704c2b

Browse files
committed
Adding in a BN code and refactoring a bit
1 parent 758f51b commit a704c2b

1 file changed

Lines changed: 43 additions & 30 deletions

File tree

src/paypal-button.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ PAYPAL.apps = PAYPAL.apps || {};
1212

1313
var app = {},
1414
paypalURL = 'https://www.paypal.com/cgi-bin/webscr',
15+
bnCode = 'JavaScriptButton_{type}',
1516
prettyParams = {
1617
id: 'hosted_button_id',
1718
name: 'item_name',
1819
number: 'item_number',
1920
lang: 'lc'
2021
},
2122
buttonImgs = {
22-
buynow: '//www.paypalobjects.com/{lang}/i/btn/btn_buynow_{size}.gif',
23-
cart: '//www.paypalobjects.com/{lang}/i/btn/btn_cart_{size}.gif',
24-
basic: '//www.paypalobjects.com/{lang}/i/btn/btn_buynow_{size}.gif'
23+
buynow: '//www.paypalobjects.com/{locale}/i/btn/btn_buynow_{size}.gif',
24+
cart: '//www.paypalobjects.com/{locale}/i/btn/btn_cart_{size}.gif',
25+
basic: '//www.paypalobjects.com/{locale}/i/btn/btn_buynow_{size}.gif'
2526
};
2627

2728
if (!PAYPAL.apps.ButtonFactory) {
@@ -39,41 +40,50 @@ PAYPAL.apps = PAYPAL.apps || {};
3940
/**
4041
* Renders a button in place of the given element
4142
*
42-
* @param data {Object} An object of key/value data to set as button params
43+
* @param raw {Object} An object of key/value data to set as button params
4344
* @param type (String) The type of the button to render
4445
* @param parent {HTMLElement} The element to add the button to (Optional)
4546
* @return {HTMLElement}
4647
*/
47-
app.create = function (data, type, parent) {
48-
var normalized = {}, button, key;
48+
app.create = function (raw, type, parent) {
49+
var data = {}, button, key, size;
4950

5051
// Don't render without the correct data
51-
if (!data || !data.business) {
52+
if (!raw || !raw.business) {
5253
return false;
5354
}
5455

5556
// Normalize the data's keys
56-
for (key in data) {
57-
normalized[prettyParams[key] || key] = data[key];
57+
for (key in raw) {
58+
data[prettyParams[key] || key] = raw[key];
5859
}
5960

61+
// Setup defaults
62+
type = type || 'basic';
63+
6064
// Hosted buttons
61-
if (normalized.hosted_button_id) {
62-
normalized.cmd = '_s-xclick';
65+
if (data.hosted_button_id) {
66+
data.cmd = '_s-xclick';
6367
// Cart buttons
6468
} else if (type === 'cart') {
65-
normalized.cmd = '_cart';
66-
normalized.add = true;
69+
data.cmd = '_cart';
70+
data.add = true;
6771
// Plain text buttons
6872
} else {
69-
normalized.cmd = '_xclick';
73+
data.cmd = '_xclick';
7074
}
7175

76+
// Create the button name
77+
data.bn = bnCode.replace(/\{type\}/, type);
78+
7279
// Build the UI components
7380
if (type === 'qr') {
74-
button = buildQR(normalized);
81+
size = data.size;
82+
delete data.size;
83+
84+
button = buildQR(data, size);
7585
} else {
76-
button = buildForm(type, normalized);
86+
button = buildForm(type, data);
7787
}
7888

7989
// Register it
@@ -111,7 +121,7 @@ PAYPAL.apps = PAYPAL.apps || {};
111121
form.action = paypalURL;
112122
form.appendChild(btn);
113123

114-
btn.src = getButtonImg(type, data);
124+
btn.src = getButtonImg(type, data.size, data.lc);
115125

116126
for (key in data) {
117127
input = hidden.cloneNode(true);
@@ -140,15 +150,19 @@ PAYPAL.apps = PAYPAL.apps || {};
140150
* Builds the image for a QR code
141151
*
142152
* @param data {Object} An object of key/value data to set as button params
153+
* @param size {String} The size of QR code's longest side
154+
* @param locale {String} The locale
143155
* @return {HTMLElement}
144156
*/
145-
function buildQR(data) {
157+
function buildQR(data, size, locale) {
146158
var img = document.createElement('img'),
147-
size = data.size || 250,
148159
url = paypalURL + '?',
149160
pattern = 13,
150161
key;
151162

163+
// QR defaults
164+
size = size || 250;
165+
152166
for (key in data) {
153167
url += key + '=' + encodeURIComponent(data[key]) + '&';
154168
}
@@ -165,19 +179,18 @@ PAYPAL.apps = PAYPAL.apps || {};
165179
* Utility function to return the rendered button image URL
166180
*
167181
* @param type {String} The type of button to render
182+
* @param size {String} The size of button (small/large)
183+
* @param locale {String} The locale
168184
* @return {String}
169185
*/
170-
function getButtonImg(type, data) {
171-
var img = buttonImgs[type] || buttonImgs.basic,
172-
lang = data.lc || 'en_US',
173-
size = 'LG';
174-
175-
// Convert "pretty sizes" to expected sizes
176-
if (data.size === 'small') {
177-
size = 'SM';
178-
}
179-
180-
return img.replace(/\{lang\}/, lang).replace(/\{size\}/, size);
186+
function getButtonImg(type, size, locale) {
187+
var img = buttonImgs[type] || buttonImgs.basic;
188+
189+
// Image defaults
190+
locale = locale || 'en_US';
191+
size = (size === 'small') ? 'SM' : 'LG';
192+
193+
return img.replace(/\{locale\}/, locale).replace(/\{size\}/, size);
181194
}
182195

183196

0 commit comments

Comments
 (0)