This repository has been archived by the owner on May 26, 2024. It is now read-only.
forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.rb
71 lines (61 loc) · 3.5 KB
/
errors.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module StripeMock
def self.prepare_error(stripe_error, *handler_names)
handler_names.push(:all) if handler_names.count == 0
if @state == 'local'
instance
elsif @state == 'remote'
client
else
raise UnstartedStateError
end.error_queue.queue stripe_error, handler_names
end
def self.prepare_card_error(code, *handler_names)
handler_names.push(:new_charge) if handler_names.count == 0
error = CardErrors.build_error_for(code)
if error.nil?
raise StripeMockError, "Unrecognized stripe card error code: #{code}"
end
prepare_error error, *handler_names
end
module CardErrors
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
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
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
end
end