-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
card.coffee
307 lines (259 loc) · 10.2 KB
/
card.coffee
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
require '../scss/card.scss'
QJ = require 'qj'
payment = require 'payment'
extend = require 'node.extend'
class Card
initializedDataAttr: "data-jp-card-initialized"
cardTemplate: '' +
'<div class="jp-card-container">' +
'<div class="jp-card">' +
'<div class="jp-card-front">' +
'<div class="jp-card-logo jp-card-elo">' +
'<div class="e">e</div>' +
'<div class="l">l</div>' +
'<div class="o">o</div>' +
'</div>' +
'<div class="jp-card-logo jp-card-visa">Visa</div>' +
'<div class="jp-card-logo jp-card-visaelectron">Visa<div class="elec">Electron</div></div>' +
'<div class="jp-card-logo jp-card-mastercard">Mastercard</div>' +
'<div class="jp-card-logo jp-card-maestro">Maestro</div>' +
'<div class="jp-card-logo jp-card-amex"></div>' +
'<div class="jp-card-logo jp-card-discover">discover</div>' +
'<div class="jp-card-logo jp-card-unionpay">UnionPay</div>' +
'<div class="jp-card-logo jp-card-dinersclub"></div>' +
'<div class="jp-card-logo jp-card-hipercard">Hipercard</div>' +
'<div class="jp-card-logo jp-card-troy">troy</div>' +
'<div class="jp-card-logo jp-card-dankort"><div class="dk"><div class="d"></div><div class="k"></div></div></div>' +
'<div class="jp-card-logo jp-card-jcb">' +
'<div class="j">J</div>' +
'<div class="c">C</div>' +
'<div class="b">B</div>' +
'</div>' +
'<div class="jp-card-lower">' +
'<div class="jp-card-shiny"></div>' +
'<div class="jp-card-cvc jp-card-display">{{cvc}}</div>' +
'<div class="jp-card-number jp-card-display">{{number}}</div>' +
'<div class="jp-card-name jp-card-display">{{name}}</div>' +
'<div class="jp-card-expiry jp-card-display" data-before="{{monthYear}}" data-after="{{validDate}}">{{expiry}}</div>' +
'</div>' +
'</div>' +
'<div class="jp-card-back">' +
'<div class="jp-card-bar"></div>' +
'<div class="jp-card-cvc jp-card-display">{{cvc}}</div>' +
'<div class="jp-card-shiny"></div>' +
'</div>' +
'</div>' +
'</div>'
template: (tpl, data) ->
tpl.replace /\{\{(.*?)\}\}/g, (match, key, str) ->
data[key]
cardTypes: [
'jp-card-amex',
'jp-card-dankort',
'jp-card-dinersclub',
'jp-card-discover',
'jp-card-unionpay',
'jp-card-jcb',
'jp-card-laser',
'jp-card-maestro',
'jp-card-mastercard',
'jp-card-troy',
'jp-card-unionpay',
'jp-card-visa',
'jp-card-visaelectron',
'jp-card-elo',
'jp-card-hipercard'
]
defaults:
formatting: true
formSelectors:
numberInput: 'input[name="number"]'
expiryInput: 'input[name="expiry"]'
cvcInput: 'input[name="cvc"]'
nameInput: 'input[name="name"]'
cardSelectors:
cardContainer: '.jp-card-container'
card: '.jp-card'
numberDisplay: '.jp-card-number'
expiryDisplay: '.jp-card-expiry'
cvcDisplay: '.jp-card-cvc'
nameDisplay: '.jp-card-name'
messages:
validDate: 'valid\nthru'
monthYear: 'month/year'
placeholders:
number: '•••• •••• •••• ••••'
cvc: '•••'
expiry: '••/••'
name: 'Full Name'
masks:
cardNumber: false
classes:
valid: 'jp-card-valid'
invalid: 'jp-card-invalid'
debug: false
constructor: (opts) ->
@options = extend(true, @defaults, opts)
unless @options.form
console.log "Please provide a form"
return
@$el = QJ(@options.form)
unless @options.container
console.log "Please provide a container"
return
@$container = QJ(@options.container)
# set a data attribute to ensure that card is only ever initialized
# once on a given container
toInitialize = if QJ.isDOMElement(@$container) then @$container else @$container[0]
return if toInitialize.getAttribute(@initializedDataAttr)
toInitialize.setAttribute(@initializedDataAttr, true)
@render()
@attachHandlers()
@handleInitialPlaceholders()
render: ->
QJ.append(@$container, @template(
@cardTemplate,
extend({}, @options.messages, @options.placeholders)
))
for name, selector of @options.cardSelectors
this["$#{name}"] = QJ.find(@$container, selector)
for name, selector of @options.formSelectors
selector = if @options[name] then @options[name] else selector
obj = QJ.find(@$el, selector)
console.error "Card can't find a #{name} in your form." if !obj.length and @options.debug
this["$#{name}"] = obj
if @options.formatting
Payment.formatCardNumber(@$numberInput)
Payment.formatCardCVC(@$cvcInput)
Payment.formatCardExpiry(@$expiryInput)
if @options.width
$cardContainer = QJ(@options.cardSelectors.cardContainer)[0]
baseWidth = parseInt($cardContainer.clientWidth || window.getComputedStyle($cardContainer).width)
$cardContainer.style.transform = "scale(#{@options.width / baseWidth})"
# safari can't handle transparent radial gradient right now
if navigator?.userAgent
ua = navigator.userAgent.toLowerCase()
if ua.indexOf('safari') != -1 and ua.indexOf('chrome') == -1
QJ.addClass @$card, 'jp-card-safari'
if (/MSIE 10\./i.test(navigator.userAgent))
QJ.addClass @$card, 'jp-card-ie-10'
# ie 11 does not support conditional compilation, use user agent instead
if (/rv:11.0/i.test(navigator.userAgent))
QJ.addClass @$card, 'jp-card-ie-11'
attachHandlers: ->
numberInputFilters = [@validToggler('cardNumber')]
numberInputFilters.push(@maskCardNumber) if @options.masks.cardNumber
bindVal @$numberInput, @$numberDisplay,
fill: false,
filters: numberInputFilters
QJ.on @$numberInput, 'payment.cardType', @handle('setCardType')
expiryFilters = [(val) ->
if val.length == 1 and val[0] == '0' then '' else val.replace /(\s+)/g, ''
]
expiryFilters.push @validToggler('cardExpiry')
bindVal @$expiryInput, @$expiryDisplay,
join: (text) ->
if text[0].length == 2 or text[1] then "/" else ""
filters: expiryFilters
bindVal @$cvcInput, @$cvcDisplay, filters: @validToggler('cardCVC')
QJ.on @$cvcInput, 'focus', @handle('flipCard')
QJ.on @$cvcInput, 'blur', @handle('unflipCard')
bindVal @$nameInput, @$nameDisplay,
fill: false
filters: @validToggler('cardHolderName')
join: ' '
handleInitialPlaceholders: ->
for name, selector of @options.formSelectors
el = this["$#{name}"]
el = el[0] if el instanceof NodeList
if QJ.val(el)
# if the input has a value, we want to trigger a refresh
QJ.trigger el, 'paste'
# set a timeout because `jquery.payment` does the reset of the val
# in a timeout
do (el) -> setTimeout -> QJ.trigger el, 'keyup'
handle: (fn) ->
(e) =>
args = Array.prototype.slice.call arguments
args.unshift e.target
@handlers[fn].apply this, args
validToggler: (validatorName) ->
if validatorName == "cardExpiry"
isValid = (val) ->
objVal = Payment.fns.cardExpiryVal val
Payment.fns.validateCardExpiry objVal.month, objVal.year
else if validatorName == "cardCVC"
isValid = (val) => Payment.fns.validateCardCVC val, @cardType
else if validatorName == "cardNumber"
isValid = (val) -> Payment.fns.validateCardNumber val
else if validatorName == "cardHolderName"
isValid = (val) -> val != ""
(val, $in, $out) =>
result = isValid val
@toggleValidClass $in, result
@toggleValidClass $out, result
val
toggleValidClass: (el, test) ->
QJ.toggleClass el, @options.classes.valid, test
QJ.toggleClass el, @options.classes.invalid, !test
maskCardNumber: (val, el, out) =>
mask = @options.masks.cardNumber
numbers = val.split(' ')
if numbers.length >= 3
numbers.forEach (item, idx) ->
numbers[idx] = numbers[idx].replace(/\d/g, mask) unless idx == numbers.length - 1
numbers.join(' ')
else
val.replace /\d/g, mask
handlers:
setCardType: ($el, e) ->
cardType = e.data
event = new CustomEvent('card-type-changed', 'detail': e.data)
document.dispatchEvent event
unless QJ.hasClass @$card, cardType
QJ.removeClass @$card, 'jp-card-unknown'
QJ.removeClass @$card, @cardTypes.join(' ')
QJ.addClass @$card, "jp-card-#{cardType}"
QJ.toggleClass @$card, 'jp-card-identified', (cardType isnt 'unknown')
@cardType = cardType
flipCard: ->
QJ.addClass @$card, 'jp-card-flipped'
unflipCard: ->
QJ.removeClass @$card, 'jp-card-flipped'
bindVal = (el, out, opts={}) ->
opts.fill = opts.fill || false
opts.filters = opts.filters || []
opts.filters = [opts.filters] unless opts.filters instanceof Array
opts.join = opts.join || ""
if !(typeof(opts.join) == "function")
joiner = opts.join
opts.join = () -> joiner
outDefaults = (o.textContent for o in out)
# Set the value on initiation
setVal(el, out, outDefaults, opts)
# Update on Events
QJ.on el, 'focus', ->
QJ.addClass out, 'jp-card-focused'
QJ.on el, 'blur', ->
QJ.removeClass out, 'jp-card-focused'
QJ.on el, 'keyup change paste', (e) ->
setVal(el, out, outDefaults, opts)
el
setVal = (el, out, outDefaults, opts) ->
val = (QJ.val(elem) for elem in el)
join = opts.join(val)
val = val.join(join)
val = "" if val == join
for filter in opts.filters
val = filter(val, el, out)
for outEl, i in out
if opts.fill
outVal = val + outDefaults[i].substring(val.length)
else
outVal = val or outDefaults[i]
outEl.textContent = outVal
getCardType: ->
ccType = Payment.fns.cardType(@$numberInput[0].value)
if ccType then ccType else 'unknown'
module.exports = Card
global.Card = Card