Class: PaymentGateway::Mundipagg::Card

Inherits:
Base
  • Object
show all
Defined in:
lib/payment_gateway/mundipagg/card.rb

Defined Under Namespace

Classes: InvalidCardNumberError, MissingCardDataAttributeError

Constant Summary

Constants inherited from Base

Base::API_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_id) ⇒ Card

Returns a new instance of Card.



11
12
13
14
15
# File 'lib/payment_gateway/mundipagg/card.rb', line 11

def initialize(customer_id)
  super()
  raise "User customer_id cannot be blank" if customer_id.blank?
  @customer_id = customer_id
end

Instance Attribute Details

#customer_idObject

Returns the value of attribute customer_id.



9
10
11
# File 'lib/payment_gateway/mundipagg/card.rb', line 9

def customer_id
  @customer_id
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/payment_gateway/mundipagg/card.rb', line 25

def any?
  list&.any?
end

#create(card_data) ⇒ Object

Card data ###

{

"number": "5425019448107793",
"holder_name": "Tony Stark",

“holder_document”: “93095135270”,

"exp_month": 1,
"exp_year": 18,
"cvv": "351",
"billing_address": {
  street: "Malibu Point",
  "number": "10880",
  "ZIP code": "90265",
  "neighborhood": "Central Malibu",
  "city": Malibu,
  "state": "HERE",
  "country": "US"
}

}



48
49
50
51
# File 'lib/payment_gateway/mundipagg/card.rb', line 48

def create(card_data)
  validate_card_data(card_data)
  request(:post, mount_url, body: card_request_body(card_data))
end

#destroy(credit_card_id) ⇒ Object



53
54
55
56
57
58
# File 'lib/payment_gateway/mundipagg/card.rb', line 53

def destroy(credit_card_id)
  if credit_card_id.blank?
    raise "Credit card id cannot be blank"
  end
  response = request(:delete, mount_url("/#{credit_card_id}"))
end

#listObject



17
18
19
# File 'lib/payment_gateway/mundipagg/card.rb', line 17

def list
  request(:get, mount_url)
end

#show(card_id) ⇒ Object



21
22
23
# File 'lib/payment_gateway/mundipagg/card.rb', line 21

def show(card_id)
  request(:get, mount_url("/#{card_id}"))
end

#validate_card_cvv(cvv) ⇒ Object



84
85
86
# File 'lib/payment_gateway/mundipagg/card.rb', line 84

def validate_card_cvv(cvv)
  raise "Invalid cvv" unless /^[0-9]{3,4}$/.match(cvv.to_s)
end

#validate_card_data(card_data) ⇒ Object



60
61
62
63
64
65
# File 'lib/payment_gateway/mundipagg/card.rb', line 60

def validate_card_data(card_data)
  validate_card_data_attributes_presence(card_data)
  validate_card_number((card_data[:number] || card_data['number']))
  validate_card_exp_date(card_data)
  validate_card_cvv((card_data[:cvv] || card_data['cvv']))
end

#validate_card_exp_date(card_data) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/payment_gateway/mundipagg/card.rb', line 73

def validate_card_exp_date(card_data)
  exp_month = card_data[:exp_month] || card_data['exp_month']
  exp_year  = card_data[:exp_year] || card_data['exp_year']
  unless exp_month.is_a?(Integer) && exp_month.in?(1..12)
    raise "Invalid card exp_month, use integer from 1 to 12"
  end
  unless exp_year.is_a?(Integer) && exp_year.in?(0..50)
    raise "Invalid card exp_year, user integer from 0 to 50"
  end
end

#validate_card_number(card_number) ⇒ Object



67
68
69
70
71
# File 'lib/payment_gateway/mundipagg/card.rb', line 67

def validate_card_number(card_number)
  unless CreditCardValidations::Detector.new(card_number).valid?
    raise InvalidCardNumberError
  end
end