index.coffee 18.1 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 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
assert = require('assert')
window = require('jsdom').jsdom().defaultView

global.window = window
global.document = window.document

Payment = require('../src/payment')
QJ = require('qj')

describe 'jquery.payment', ->
  describe 'Validating a card number', ->
    it 'should fail if empty', ->
      topic = Payment.fns.validateCardNumber ''
      assert.equal topic, false

    it 'should fail if is a bunch of spaces', ->
      topic = Payment.fns.validateCardNumber '                 '
      assert.equal topic, false

    it 'should success if is valid', ->
      topic = Payment.fns.validateCardNumber '4242424242424242'
      assert.equal topic, true

    it 'that has dashes in it but is valid', ->
      topic = Payment.fns.validateCardNumber '4242-4242-4242-4242'
      assert.equal topic, true

    it 'should succeed if it has spaces in it but is valid', ->
      topic = Payment.fns.validateCardNumber '4242 4242 4242 4242'
      assert.equal topic, true

    it 'that does not pass the luhn checker', ->
      topic = Payment.fns.validateCardNumber '4242424242424241'
      assert.equal topic, false

    it 'should fail if is more than 16 digits', ->
      topic = Payment.fns.validateCardNumber '42424242424242424'
      assert.equal topic, false

    it 'should fail if is less than 10 digits', ->
      topic = Payment.fns.validateCardNumber '424242424'
      assert.equal topic, false

    it 'should fail with non-digits', ->
      topic = Payment.fns.validateCardNumber '4242424e42424241'
      assert.equal topic, false

    it 'should validate for all card types', ->
      assert(Payment.fns.validateCardNumber('378282246310005'), 'amex')
      assert(Payment.fns.validateCardNumber('371449635398431'), 'amex')
      assert(Payment.fns.validateCardNumber('378734493671000'), 'amex')

      assert(Payment.fns.validateCardNumber('5019123456789013'), 'dankort')

      assert(Payment.fns.validateCardNumber('30569309025904'), 'dinersclub')
      assert(Payment.fns.validateCardNumber('38520000023237'), 'dinersclub')

      assert(Payment.fns.validateCardNumber('6011111111111117'), 'discover')
      assert(Payment.fns.validateCardNumber('6011000990139424'), 'discover')

      assert(Payment.fns.validateCardNumber('3530111333300000'), 'jcb')
      assert(Payment.fns.validateCardNumber('3566002020360505'), 'jcb')

      assert(Payment.fns.validateCardNumber('5555555555554444'), 'mastercard')

      assert(Payment.fns.validateCardNumber('4111111111111111'), 'visa')
      assert(Payment.fns.validateCardNumber('4012888888881881'), 'visa')
      assert(Payment.fns.validateCardNumber('4222222222222'), 'visa')

      assert(Payment.fns.validateCardNumber('4917300800000000'), 'visaelectron')

      assert(Payment.fns.validateCardNumber('6759649826438453'), 'maestro')

      assert(Payment.fns.validateCardNumber('6271136264806203568'), 'unionpay')
      assert(Payment.fns.validateCardNumber('6236265930072952775'), 'unionpay')
      assert(Payment.fns.validateCardNumber('6204679475679144515'), 'unionpay')
      assert(Payment.fns.validateCardNumber('6216657720782466507'), 'unionpay')

  describe 'Validating a CVC', ->
    it 'should fail if is empty', ->
      topic = Payment.fns.validateCardCVC ''
      assert.equal topic, false

    it 'should pass if is valid', ->
      topic = Payment.fns.validateCardCVC '123'
      assert.equal topic, true

    it 'should fail with non-digits', ->
      topic = Payment.fns.validateCardNumber '12e'
      assert.equal topic, false

    it 'should fail with less than 3 digits', ->
      topic = Payment.fns.validateCardNumber '12'
      assert.equal topic, false

    it 'should fail with more than 4 digits', ->
      topic = Payment.fns.validateCardNumber '12345'
      assert.equal topic, false

  describe 'Validating an expiration date', ->
    it 'should fail expires is before the current year', ->
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() - 1
      assert.equal topic, false

    it 'that expires in the current year but before current month', ->
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry currentTime.getMonth(), currentTime.getFullYear()
      assert.equal topic, false

    it 'that has an invalid month', ->
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry 13, currentTime.getFullYear()
      assert.equal topic, false

    it 'that is this year and month', ->
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear()
      assert.equal topic, true

    it 'that is just after this month', ->
      # Remember - months start with 0 in JavaScript!
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear()
      assert.equal topic, true

    it 'that is after this year', ->
      currentTime = new Date()
      topic = Payment.fns.validateCardExpiry currentTime.getMonth() + 1, currentTime.getFullYear() + 1
      assert.equal topic, true

    it 'that has string numbers', ->
      currentTime = new Date()
      currentTime.setFullYear(currentTime.getFullYear() + 1, currentTime.getMonth() + 2)
      topic = Payment.fns.validateCardExpiry currentTime.getMonth() + '', currentTime.getFullYear() + ''
      assert.equal topic, true

    it 'that has non-numbers', ->
      topic = Payment.fns.validateCardExpiry 'h12', '3300'
      assert.equal topic, false

    it 'should fail if year or month is NaN', ->
      topic = Payment.fns.validateCardExpiry '12', NaN
      assert.equal topic, false

    it 'should support year shorthand', ->
      assert.equal Payment.fns.validateCardExpiry('05', '20'), true

  describe 'Validating a CVC number', ->
    it 'should validate a three digit number with no card type', ->
      topic = Payment.fns.validateCardCVC('123')
      assert.equal topic, true

    it 'should not validate a three digit number with card type amex', ->
      topic = Payment.fns.validateCardCVC('123', 'amex')
      assert.equal topic, false

    it 'should validate a three digit number with card type other than amex', ->
      topic = Payment.fns.validateCardCVC('123', 'visa')
      assert.equal topic, true

    it 'should not validate a four digit number with a card type other than amex', ->
      topic = Payment.fns.validateCardCVC('1234', 'visa')
      assert.equal topic, false

    it 'should validate a four digit number with card type amex', ->
      topic = Payment.fns.validateCardCVC('1234', 'amex')
      assert.equal topic, true

    it 'should not validate a number larger than 4 digits', ->
      topic = Payment.fns.validateCardCVC('12344')
      assert.equal topic, false

  describe 'Parsing an expiry value', ->
    it 'should parse string expiry', ->
      topic = Payment.fns.cardExpiryVal('03 / 2025')
      assert.deepEqual topic, month: 3, year: 2025

    it 'should support shorthand year', ->
      topic = Payment.fns.cardExpiryVal('05/04')
      assert.deepEqual topic, month: 5, year: 2004

    it 'should return NaN when it cannot parse', ->
      topic = Payment.fns.cardExpiryVal('05/dd')
      assert isNaN(topic.year)

  describe 'Getting a card type', ->
    it 'should return Visa that begins with 40', ->
      topic = Payment.fns.cardType '4012121212121212'
      assert.equal topic, 'visa'

    it 'that begins with 5 should return MasterCard', ->
      topic = Payment.fns.cardType '5555555555554444'
      assert.equal topic, 'mastercard'

    it 'that begins with 34 should return American Express', ->
      topic = Payment.fns.cardType '3412121212121212'
      assert.equal topic, 'amex'

    it 'that is not numbers should return null', ->
      topic = Payment.fns.cardType 'aoeu'
      assert.equal topic, null

    it 'that has unrecognized beginning numbers should return null', ->
      topic = Payment.fns.cardType 'aoeu'
      assert.equal topic, null

    it 'should return correct type for all test numbers', ->
      assert.equal(Payment.fns.cardType('378282246310005'), 'amex')
      assert.equal(Payment.fns.cardType('371449635398431'), 'amex')
      assert.equal(Payment.fns.cardType('378734493671000'), 'amex')

      assert.equal(Payment.fns.cardType('30569309025904'), 'dinersclub')
      assert.equal(Payment.fns.cardType('38520000023237'), 'dinersclub')

      assert.equal(Payment.fns.cardType('6011111111111117'), 'discover')
      assert.equal(Payment.fns.cardType('6011000990139424'), 'discover')

      assert.equal(Payment.fns.cardType('3530111333300000'), 'jcb')
      assert.equal(Payment.fns.cardType('3566002020360505'), 'jcb')

      assert.equal(Payment.fns.cardType('5555555555554444'), 'mastercard')

      assert.equal(Payment.fns.cardType('4111111111111111'), 'visa')
      assert.equal(Payment.fns.cardType('4012888888881881'), 'visa')
      assert.equal(Payment.fns.cardType('4222222222222'), 'visa')

      assert.equal(Payment.fns.cardType('6759649826438453'), 'maestro')

      assert.equal(Payment.fns.cardType('6271136264806203568'), 'unionpay')
      assert.equal(Payment.fns.cardType('6236265930072952775'), 'unionpay')
      assert.equal(Payment.fns.cardType('6204679475679144515'), 'unionpay')
      assert.equal(Payment.fns.cardType('6216657720782466507'), 'unionpay')

  describe 'formatCardNumber', ->
    it 'should restrict non-number characters', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val number, '4242'

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "a".charCodeAt(0)

      number.dispatchEvent(ev)

      assert.equal QJ.val(number), '4242'
    it 'should restrict characters when the card is number 16 characters', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val(number, '4242 4242 4242 4242')

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "4".charCodeAt(0)

      number.dispatchEvent(ev)

      assert.equal QJ.val(number), '4242 4242 4242 4242'
    it 'should format cc number correctly', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val(number, '4242')

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = 52

      number.dispatchEvent(ev)

      assert.equal QJ.val(number), '4242 4'

    it 'should remove a trailing space before a number on backspace ', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val(number, '4242 ')
      number.selectionStart = 5

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keydown", true, true
      ev.eventName = "keydown"
      ev.which = 8

      number.dispatchEvent(ev)

      assert.equal QJ.val(number), '424'
    it 'should remove the space after a number being deleted on a backspace', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val(number, '4242 5')
      number.selectionStart = 6

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keydown", true, true
      ev.eventName = "keydown"
      ev.which = 8

      number.dispatchEvent(ev)

      assert.equal QJ.val(number), '4242'
    it 'should set the card type correctly', ->
      number = document.createElement('input')
      number.type = 'text'
      QJ.val(number, '4')

      Payment.formatCardNumber(number)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keyup", true, true
      ev.eventName = "keyup"
      number.dispatchEvent(ev)

      assert QJ.hasClass(number, 'visa')
      assert QJ.hasClass(number, 'identified')

      QJ.val(number, '')

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keyup", true, true
      ev.eventName = "keyup"
      number.dispatchEvent(ev)

      # JSDom doesn't support custom events right now, so this testing
      # doesn't work :(
      #
      # eventCalled = false
      # number.addEventListener 'payment.cardType', ->
      #   console.log 'hiya'
      #   eventCalled = true
      # assert eventCalled

      assert QJ.hasClass(number, 'unknown')
      assert !QJ.hasClass(number, 'identified')
    it 'should format correctly on paste', ->
      number = document.createElement('input')
      number.type = 'text'
      Payment.formatCardNumber(number)

      QJ.val(number, '42424')

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "paste", true, true
      ev.eventName = "paste"

      number.dispatchEvent(ev)

      # must setTimeout because paste event handling is
      # done in a setTimeout
      setTimeout ->
        assert.equal QJ.val(number), '4242 4'


  describe 'formatCardExpiry', ->
    it 'should add a slash after two numbers', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '1')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "1".charCodeAt(0)

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '11 / '
    it 'should format add a 0 and slash to a number > 1 correctly', ->
      expiry = document.createElement('input')
      expiry.type = "text"

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "4".charCodeAt(0)

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '04 / '
    it 'should format add a 0 to a number > 1 in the month input correctly', ->
      month = document.createElement('input')
      month.type = "text"
      year = document.createElement('input')
      year.type = "text"

      Payment.formatCardExpiry([month, year])

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "4".charCodeAt(0)

      month.dispatchEvent(ev)

      assert.equal QJ.val(month), '04'
    it 'should format forward slash shorthand correctly', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '1')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "/".charCodeAt(0)

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '01 / '

    it 'should only allow numbers', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '1')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "d".charCodeAt(0)

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '1'
    it 'should remove spaces trailing space and / after removing a number', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '12 / 1')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keydown", true, true
      ev.eventName = "keydown"
      ev.which = 8 # backspace

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '12'

    it 'should a number after removing a space and a /', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '12 / ')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keydown", true, true
      ev.eventName = "keydown"
      ev.which = 8 # backspace

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '1'
    it 'should restrict combined expiry fields to length <= 6 digits', ->
      expiry = document.createElement('input')
      expiry.type = "text"
      QJ.val(expiry, '12 / 2018')

      Payment.formatCardExpiry(expiry)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "1".charCodeAt(0)

      expiry.dispatchEvent(ev)

      assert.equal QJ.val(expiry), '12 / 2018'
    it 'should restrict month expiry fields to length <= 2 digits', ->
      month = document.createElement('input')
      month.type = "text"
      QJ.val(month, '12')

      year = document.createElement('input')
      year.type = "text"
      QJ.val(year, '2018')

      Payment.formatCardExpiryMultiple(month, year)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "1".charCodeAt(0)

      month.dispatchEvent(ev)

      assert.equal QJ.val(month), '12'
    it 'should restrict year expiry fields to length <= 4 digits', ->
      month = document.createElement('input')
      month.type = "text"
      QJ.val(month, '12')

      year = document.createElement('input')
      year.type = "text"
      QJ.val(year, '2018')

      Payment.formatCardExpiryMultiple(month, year)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "1".charCodeAt(0)

      year.dispatchEvent(ev)

      assert.equal QJ.val(year), '2018'
  describe 'formatCVC', ->
    it 'should allow only numbers', ->
      cvc = document.createElement('input')
      cvc.type = "text"
      QJ.val(cvc, '1')

      Payment.formatCardCVC(cvc)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "d".charCodeAt(0)

      cvc.dispatchEvent(ev)

      assert.equal QJ.val(cvc), '1'
    it 'should restrict to length <= 4', ->
      cvc = document.createElement('input')
      cvc.type = "text"
      QJ.val(cvc, '1234')

      Payment.formatCardCVC(cvc)

      ev = document.createEvent "HTMLEvents"
      ev.initEvent "keypress", true, true
      ev.eventName = "keypress"
      ev.which = "1".charCodeAt(0)

      cvc.dispatchEvent(ev)

      assert.equal QJ.val(cvc), '1234'