summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/nav/CreditCardView.js
blob: a942446d6fd3bc78489a9457ee94a0f5aae5f9a4 (plain)
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
var CreditCardView = SerializableView.extend({
  
  template: $("#creditcard_template").html(),
  
  cardOptions: {
    accept: ['visa', 'mastercard', 'amex'],
  },
  
  events: {
  },
  
  initialize: function(opt){
    this.parent = opt.parent
    this.setElement( this.parent.$(".cc") )
    this.$el.html(this.template)
    
    this.$number = this.$("[name=Number]")
    this.$number.validateCreditCard(this.updateCard.bind(this), this.cardOptions)
  },
  
  populate: function(data){
    this.parent.$(".cc input").val("")
    this.$(".cc input").val("")
    this.load_data(data)
  },
  
  updateCard: function(card){
    // console.log(card)
    // card.card_type.name
    // card.card_type.valid
  },
  
  validate_presence: {
    'Number': 'Please enter your credit card number.',
    'Cvv': 'Please enter your security code.',
  },
  
  validate_fields: function(data, errors){
    var card = this.$number.validateCreditCard(this.cardOptions)
    if (! card.valid) { errors.push([ "Number", "Your card number is invalid." ]) }
    if (! data.ExpirationMonth || data.ExpirationMonth == "NONE") { errors.push([ "ExpirationMonth", "Please enter the expiration month." ]) }
    if (! data.ExpirationYear || data.ExpirationYear == "NONE") { errors.push([ "ExpirationYear", "Please select the expiration month." ]) }
    data.UserId = auth.user_id
    if (card.valid) {
      data.Type = card.card_type.name
    }
  },

})