Class: CreditCard

Inherits:
Object
  • Object
show all
Defined in:
lib/order_payment.rb

Constant Summary collapse

VALUES =
{
  visa: "00200",
  mastercard: "00300",
  jcb: "00500",
  amex: "00400",
  diners: "00100",
  nicos: "00600"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ CreditCard

Returns a new instance of CreditCard.



127
128
129
130
131
132
133
134
# File 'lib/order_payment.rb', line 127

def initialize(config = {})
  info = config.map { |key, value| [(key.to_sym rescue key), value.to_s] }.to_h

  self.number = info[:number] || ""
  self.cvv = info[:cvv]
  self.expiration_date = info[:expiration_date]
  self.name_on_card = info[:name]
end

Instance Attribute Details

#cvvObject

Returns the value of attribute cvv.



114
115
116
# File 'lib/order_payment.rb', line 114

def cvv
  @cvv
end

#expiration_dateObject

Returns the value of attribute expiration_date.



115
116
117
# File 'lib/order_payment.rb', line 115

def expiration_date
  @expiration_date
end

#name_on_cardObject

Returns the value of attribute name_on_card.



116
117
118
# File 'lib/order_payment.rb', line 116

def name_on_card
  @name_on_card
end

#numberObject

Returns the value of attribute number.



114
115
116
# File 'lib/order_payment.rb', line 114

def number
  @number
end

Instance Method Details

#input(default_name = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/order_payment.rb', line 136

def input(default_name = nil)
  loop do
    until number.valid_credit_card_brand?(:visa, :mastercard, :jcb, :amex, :diners)
      puts "Invalid card number" unless number == ""
      self.number = Ask.input "Credit Card Number"
    end

    unless number.credit_card_brand == :diners
      self.cvv ||= HighLine.new.ask("CVV: ") { |q| q.echo = "*" }
    end

    expiration_month, expiration_year = (expiration_date || "").split("/")
    while !(1..12).cover?(expiration_month.to_i) || !(17..31).cover?(expiration_year.to_i)
      self.expiration_date = Ask.input "Expiration Date (mm/yy)"
      expiration_month, expiration_year = expiration_date.split("/")
    end

    self.name_on_card ||= Ask.input "Name on Card", default: default_name

    break if valid?
  end
end

#paramsObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/order_payment.rb', line 159

def params
  {
    "existCreditCardF" => "",
    "reuseCredit_check" => "1", # Seems to be 1 but it doesn't save the CC info
    "cardComCd" => VALUES[number.credit_card_brand],
    "creditCardNo" => number,
    "creditCardSecurityCode" => cvv,
    "creditCardSignature" => name_on_card,
    "goodThruMonth" => expiration_date.split("/").first.rjust(2, "0"),
    "goodThruYear" => expiration_date.split("/").last
  }
end

#valid?Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/order_payment.rb', line 172

def valid?
  response = Request.post(
    "https://order.dominos.jp/eng/webapi/regi/validate/creditCard/", params,
    expect: :ok, failure: "Couldn't validate credit card info"
  )

  result = JSON.parse(response.body)
  if result["errorDetails"]
    puts result
    return false
  end

  true
end