FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptButtons/src/paypal-button.js at master · ahmadshhadah/JavaScriptButtons · GitHub
ahmadshhadah
/
JavaScriptButtons
Public
forked from
jbillimoria/JavaScriptButtons
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptButtons
/
src
/
paypal-button.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
322 lines (256 loc) · 7.47 KB
Breadcrumbs
JavaScriptButtons
/
src
/
paypal-button.js
Copy path
File metadata and controls
322 lines (256 loc) · 7.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
if
(
typeof
PAYPAL
===
'undefined'
||
!
PAYPAL
)
{
var
PAYPAL
=
{
}
;
}
PAYPAL
.
apps
=
PAYPAL
.
apps
||
{
}
;
(
function
(
)
{
'use strict'
;
var
app
=
{
}
,
paypalURL
=
'https://www.paypal.com/cgi-bin/webscr'
,
qrCodeURL
=
'https://www.paypal.com/webapps/ppint/qrcode?data={url}&pattern={pattern}&height={size}'
,
bnCode
=
'JavaScriptButton_{type}'
,
prettyParams
=
{
name
:
'item_name'
,
number
:
'item_number'
,
locale
:
'lc'
,
currency
:
'currency_code'
,
recurrence
:
'p3'
,
period
:
't3'
}
,
buttonImgs
=
{
buynow
:
'//www.paypalobjects.com/{locale}/i/btn/btn_buynow_{size}.gif'
,
cart
:
'//www.paypalobjects.com/{locale}/i/btn/btn_cart_{size}.gif'
,
donate
:
'//www.paypalobjects.com/{locale}/i/btn/btn_donate_{size}.gif'
,
subscribe
:
'//www.paypalobjects.com/{locale}/i/btn/btn_subscribe_{size}.gif'
}
;
if
(
!
PAYPAL
.
apps
.
ButtonFactory
)
{
/**
* Initial config for the app. These values can be overridden by the page.
*/
app
.
config
=
{
labels
:
{
item_name
:
'Item'
,
item_number
:
'Number'
,
amount
:
'Amount'
,
quantity
:
'Quantity'
}
}
;
/**
* A count of each type of button on the page
*/
app
.
buttons
=
{
buynow
:
0
,
cart
:
0
,
donate
:
0
,
qr
:
0
,
subscribe
:
0
}
;
/**
* Renders a button in place of the given element
*
*
@param
business {Object} The ID or email address of the merchant to create the button for
*
@param
raw {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
(
business
,
raw
,
type
,
parent
)
{
var
data
=
new
DataStore
(
)
,
button
,
key
;
if
(
!
business
)
{
return
false
;
}
// Normalize the data's keys and add to a data store
for
(
key
in
raw
)
{
data
.
add
(
prettyParams
[
key
]
||
key
,
raw
[
key
]
.
value
,
raw
[
key
]
.
isEditable
)
;
}
// Defaults
type
=
type
||
'buynow'
;
// Cart buttons
if
(
type
===
'cart'
)
{
data
.
add
(
'cmd'
,
'_cart'
)
;
data
.
add
(
'add'
,
true
)
;
// Donation buttons
}
else
if
(
type
===
'donate'
)
{
data
.
add
(
'cmd'
,
'_donations'
)
;
// Subscribe buttons
}
else
if
(
type
===
'subscribe'
)
{
data
.
add
(
'cmd'
,
'_xclick-subscriptions'
)
;
// TODO: "amount" cannot be used in prettyParams since it's overloaded
// Find a better way to do this
if
(
data
.
items
.
amount
&&
!
data
.
items
.
a3
)
{
data
.
add
(
'a3'
,
data
.
items
.
amount
.
value
)
;
}
// Buy Now buttons
}
else
{
data
.
add
(
'cmd'
,
'_xclick'
)
;
}
// Add common data
data
.
add
(
'business'
,
business
)
;
data
.
add
(
'bn'
,
bnCode
.
replace
(
/
\{
t
y
p
e
\}
/
,
type
)
)
;
// Build the UI components
if
(
type
===
'qr'
)
{
button
=
buildQR
(
data
,
data
.
items
.
size
)
;
data
.
remove
(
'size'
)
;
}
else
{
button
=
buildForm
(
data
,
type
)
;
}
// 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
data {Object} An object of key/value data to set as button params
*
@param
type (String) The type of the button to render
*
@return
{
HTMLElement
}
*/
function
buildForm
(
data
,
type
)
{
var
form
=
document
.
createElement
(
'form'
)
,
btn
=
document
.
createElement
(
'input'
)
,
hidden
=
document
.
createElement
(
'input'
)
,
items
=
data
.
items
,
item
,
child
,
label
,
input
,
key
,
size
,
locale
;
btn
.
type
=
'image'
;
hidden
.
type
=
'hidden'
;
form
.
method
=
'post'
;
form
.
action
=
paypalURL
;
form
.
className
=
'paypal-button'
;
form
.
target
=
'_top'
;
for
(
key
in
items
)
{
item
=
items
[
key
]
;
if
(
item
.
isEditable
)
{
input
=
document
.
createElement
(
'input'
)
;
input
.
type
=
'text'
;
input
.
className
=
'paypal-input'
;
input
.
name
=
item
.
key
;
input
.
value
=
item
.
value
;
label
=
document
.
createElement
(
'label'
)
;
label
.
className
=
'paypal-label'
;
label
.
appendChild
(
document
.
createTextNode
(
app
.
config
.
labels
[
item
.
key
]
+
' '
||
''
)
)
;
label
.
appendChild
(
input
)
;
child
=
document
.
createElement
(
'p'
)
;
child
.
className
=
'paypal-group'
;
child
.
appendChild
(
label
)
;
}
else
{
input
=
child
=
hidden
.
cloneNode
(
true
)
;
input
.
name
=
item
.
key
;
input
.
value
=
item
.
value
;
}
form
.
appendChild
(
child
)
;
}
size
=
items
.
size
&&
items
.
size
.
value
;
locale
=
items
.
lc
&&
items
.
lc
.
value
;
form
.
appendChild
(
btn
)
;
btn
.
src
=
getButtonImg
(
type
,
size
,
locale
)
;
// If the Mini Cart is present then register the form
if
(
PAYPAL
.
apps
.
MiniCart
&&
data
.
items
.
cmd
.
value
===
'_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
*
@param
size {String} The size of QR code's longest side
*
@return
{
HTMLElement
}
*/
function
buildQR
(
data
,
size
)
{
var
img
=
document
.
createElement
(
'img'
)
,
url
=
paypalURL
+
'?'
,
pattern
=
13
,
items
=
data
.
items
,
item
,
key
;
// QR defaults
size
=
size
&&
size
.
value
||
250
;
for
(
key
in
items
)
{
item
=
items
[
key
]
;
url
+=
item
.
key
+
'='
+
encodeURIComponent
(
item
.
value
)
+
'&'
;
}
url
=
encodeURIComponent
(
url
)
;
img
.
src
=
qrCodeURL
.
replace
(
'{url}'
,
url
)
.
replace
(
'{pattern}'
,
pattern
)
.
replace
(
'{size}'
,
size
)
;
return
img
;
}
/**
* Utility function to return the rendered button image URL
*
*
@param
type {String} The type of button to render
*
@param
size {String} The size of button (small/large)
*
@param
locale {String} The locale
*
@return
{
String
}
*/
function
getButtonImg
(
type
,
size
,
locale
)
{
var
img
=
buttonImgs
[
type
]
||
buttonImgs
.
buynow
;
// Image defaults
locale
=
locale
||
'en_US'
;
size
=
(
size
===
'small'
)
?
'SM'
:
'LG'
;
return
img
.
replace
(
/
\{
l
o
c
a
l
e
\}
/
,
locale
)
.
replace
(
/
\{
s
i
z
e
\}
/
,
size
)
;
}
/**
* Utility function to polyfill dataset functionality with a bit of a spin
*
*
@param
el {HTMLElement} The element to check
*
@return
{
Object
}
*/
function
getDataSet
(
el
)
{
var
dataset
=
{
}
,
attrs
,
attr
,
matches
,
len
,
i
;
if
(
(
attrs
=
el
.
attributes
)
)
{
for
(
i
=
0
,
len
=
attrs
.
length
;
i
<
len
;
i
++
)
{
attr
=
attrs
[
i
]
;
if
(
(
matches
=
/
^
d
a
t
a
-
(
[
a
-
z
0
-
9
_
]
+
)
(
-
e
d
i
t
a
b
l
e
)
?
/
i
.
exec
(
attr
.
name
)
)
)
{
dataset
[
matches
[
1
]
]
=
{
value
:
attr
.
value
,
isEditable
:
!
!
matches
[
2
]
}
;
}
}
}
return
dataset
;
}
/**
* A storage object to create structured methods around a button's data
*/
function
DataStore
(
)
{
this
.
items
=
{
}
;
this
.
add
=
function
(
key
,
value
,
isEditable
)
{
this
.
items
[
key
]
=
{
key
:
key
,
value
:
value
,
isEditable
:
isEditable
}
;
}
;
this
.
remove
=
function
(
key
)
{
delete
this
.
items
[
key
]
;
}
;
}
// Init the buttons
if
(
typeof
document
!==
'undefined'
)
{
var
ButtonFactory
=
PAYPAL
.
apps
.
ButtonFactory
,
nodes
=
document
.
getElementsByTagName
(
'script'
)
,
node
,
data
,
type
,
business
,
i
,
len
;
for
(
i
=
0
,
len
=
nodes
.
length
;
i
<
len
;
i
++
)
{
node
=
nodes
[
i
]
;
if
(
!
node
||
!
node
.
src
)
{
continue
;
}
data
=
node
&&
getDataSet
(
node
)
;
type
=
data
&&
data
.
button
&&
data
.
button
.
value
;
business
=
node
.
src
.
split
(
'?merchant='
)
[
1
]
;
if
(
business
)
{
ButtonFactory
.
create
(
business
,
data
,
type
,
node
.
parentNode
)
;
// Clean up
node
.
parentNode
.
removeChild
(
node
)
;
}
}
}
}
(
)
)
;
// Export for CommonJS environments
if
(
typeof
module
===
'object'
&&
typeof
module
.
exports
===
'object'
)
{
module
.
exports
=
PAYPAL
;
}
Back
|
FazBrowse Home
|
New Git URL