Class: MpApi::Token

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:, id: nil, errors: nil) ⇒ Token

Returns a new instance of Token.



4
5
6
7
8
9
10
11
12
# File 'lib/mp_api/token.rb', line 4

def initialize(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:, id: nil, errors: nil)
  @card_number = card_number
  @expiration_year = expiration_year
  @expiration_month = expiration_month
  @security_code = security_code
  @cardholder_name = cardholder_name
  @id = id
  @errors = errors
end

Instance Attribute Details

#card_numberObject (readonly)

Returns the value of attribute card_number.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def card_number
  @card_number
end

#cardholder_nameObject (readonly)

Returns the value of attribute cardholder_name.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def cardholder_name
  @cardholder_name
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def errors
  @errors
end

#expiration_monthObject (readonly)

Returns the value of attribute expiration_month.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def expiration_month
  @expiration_month
end

#expiration_yearObject (readonly)

Returns the value of attribute expiration_year.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def expiration_year
  @expiration_year
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def id
  @id
end

#security_codeObject (readonly)

Returns the value of attribute security_code.



3
4
5
# File 'lib/mp_api/token.rb', line 3

def security_code
  @security_code
end

Class Method Details

.build_hash(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mp_api/token.rb', line 31

def self.build_hash(response)
  {
    id: response["id"],
    expiration_year: response["expiration_year"],
    expiration_month: response["expiration_month"],
    cardholder_name: response.dig("cardholder", "name"),
    card_number: response["card_number"],
    security_code: response["security_code"],
    errors: response["error"]
  }
end

Instance Method Details

#build_jsonObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mp_api/token.rb', line 14

def build_json
  {
    card_number: card_number,
    expiration_year: expiration_year,
    expiration_month: expiration_month,
    security_code: security_code,
    cardholder: {
      name: cardholder_name
    }
  }
end

#createObject



26
27
28
29
# File 'lib/mp_api/token.rb', line 26

def create
  response = Client.new.create_token(JSON.dump(build_json))
  self.class.new(**self.class.build_hash(response.json))
end