Module: StripeMock::CardErrors

Defined in:
lib/stripe_mock/api/errors.rb

Class Method Summary collapse

Class Method Details

.build_card_error(message, param, **kwargs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stripe_mock/api/errors.rb', line 57

def self.build_card_error(message, param, **kwargs)
  json_hash = {
    message: message,
    param: param,
    code: kwargs[:code],
    type: 'card_error',
    decline_code: get_decline_code(kwargs[:code])
  }

  error_keyword_args = kwargs.merge(json_body: { error: json_hash }, http_body: { error: json_hash }.to_json)

  Stripe::CardError.new(message, param, **error_keyword_args)
end

.build_error_for(code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stripe_mock/api/errors.rb', line 26

def self.build_error_for(code)
  case code
  when :incorrect_number then build_card_error('The card number is incorrect', 'number', code: 'incorrect_number', http_status: 402)
  when :invalid_number then build_card_error('The card number is not a valid credit card number', 'number', code:  'invalid_number', http_status: 402)
  when :invalid_expiry_month then build_card_error("The card's expiration month is invalid", 'exp_month', code: 'invalid_expiry_month', http_status: 402)
  when :invalid_expiry_year then build_card_error("The card's expiration year is invalid", 'exp_year', code: 'invalid_expiry_year', http_status: 402)
  when :invalid_cvc then build_card_error("The card's security code is invalid", 'cvc', code: 'invalid_cvc', http_status: 402)
  when :expired_card then build_card_error('The card has expired', 'exp_month', code: 'expired_card', http_status: 402)
  when :incorrect_cvc then build_card_error("The card's security code is incorrect", 'cvc', code: 'incorrect_cvc', http_status: 402)
  when :card_declined then build_card_error('The card was declined', nil, code: 'card_declined', http_status: 402)
  when :missing then build_card_error('There is no card on a customer that is being charged.', nil, code: 'missing', http_status: 402)
  when :processing_error then build_card_error('An error occurred while processing the card', nil, code: 'processing_error', http_status: 402)
  when :card_error then build_card_error('The card number is not a valid credit card number.', 'number', code: 'invalid_number', http_status: 402)
  when :incorrect_zip then build_card_error('The zip code you supplied failed validation.', 'address_zip', code: 'incorrect_zip', http_status: 402)
  when :insufficient_funds then build_card_error('The card has insufficient funds to complete the purchase.', nil, code: 'insufficient_funds', http_status: 402)
  when :lost_card then build_card_error('The payment has been declined because the card is reported lost.', nil, code: 'lost_card', http_status: 402)
  when :stolen_card then build_card_error('The payment has been declined because the card is reported stolen.', nil, code: 'stolen_card', http_status: 402)
  end
end

.get_decline_code(code) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/stripe_mock/api/errors.rb', line 46

def self.get_decline_code(code)
  decline_code_map = {
    card_declined: 'do_not_honor',
    missing: nil
  }
  decline_code_map.default = code.to_s

  code_key = code.to_sym
  decline_code_map[code_key]
end