Sha256: 261dc2f5670a96faf4c3a4e4a713961eef2f4d6445d1e7de3d7005efc3c450d6

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

'use strict'

class Sprangular.CreditCard
  this.TYPE_NAMES =
    master: 'MasterCard'
    visa: 'Visa'
    amex: 'American Express'
    discover: 'Discover'
    dinersclub: 'Diners Club'
    jcb: 'JCB'

  Validity.define @,
    number: ['required', '_validateCardFormat']
    month: 'required'
    year: 'required'
    cvc: [length: {greaterThan: 2, lessThan: 5}]

  constructor: ->
    @number = ''
    @name = null
    @month = null
    @year = null
    @cvc = null
    @type = null
    @token = null
    @lastDigits = null

  init: ->
    @id = @id
    @name = @name
    @lastDigits = @last_digits
    @token = @gateway_payment_profile_id
    @type = @cc_type

  isNew: ->
    not (@token and @token.length > 0)

  label: ->
    "#{@constructor.TYPE_NAMES[@type]} XXXX-XXXX-XXXX-#{@lastDigits}"

  determineType: ->
    @type = if @number.match /^3[47]/
              'amex'
            else if @number.match /^4/
              'visa'
            else if @number.match /^5[1-5]/
              'master'
            else if @number.match /^6(5|011)/
              'discover'
            else if @number.match /^3(0[0-5]|36|38)/
              'dinersclub'
            else if @number.match /^(2131|1800|35)/
              'jcb'

  same: (other) ->
    @id == other.id

  _validateCardFormat: ->
    'invalid card number' unless Sprangular.Luhn.isValid(@number)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sprangular-0.1.0 app/assets/javascripts/sprangular/models/creditCard.coffee