33require 'easypost/constants'
44
55class EasyPost ::Services ::Billing < EasyPost ::Services ::Service
6- # Get payment method info (type of the payment method and ID of the payment method)
7- def self . get_payment_method_info ( priority )
8- payment_methods = EasyPost ::Services ::Billing . retrieve_payment_methods
9- payment_method_map = {
10- 'primary' => 'primary_payment_method' ,
11- 'secondary' => 'secondary_payment_method' ,
12- }
13-
14- payment_method_to_use = payment_method_map [ priority ]
15-
16- error_string = EasyPost ::Constants ::INVALID_PAYMENT_METHOD
17- suggestion = "Please use a valid payment method: #{ payment_method_map . keys . join ( ', ' ) } "
18- if payment_methods [ payment_method_to_use ] . nil?
19- raise EasyPost ::Errors ::InvalidParameterError . new (
20- error_string ,
21- suggestion ,
22- )
23- end
24-
25- payment_method_id = payment_methods [ payment_method_to_use ] [ 'id' ]
26-
27- unless payment_method_id . nil?
28- if payment_method_id . start_with? ( 'card_' )
29- endpoint = '/v2/credit_cards'
30- elsif payment_method_id . start_with? ( 'bank_' )
31- endpoint = '/v2/bank_accounts'
32- else
33- raise EasyPost ::Errors ::InvalidObjectError . new ( error_string )
34- end
35- end
36-
37- [ endpoint , payment_method_id ]
38- end
39-
406 # Fund your EasyPost wallet by charging your primary or secondary card on file.
417 def fund_wallet ( amount , priority = 'primary' )
42- payment_info = EasyPost :: Services :: Billing . get_payment_method_info ( priority . downcase )
8+ payment_info = get_payment_method_info ( priority . downcase )
439 endpoint = payment_info [ 0 ]
4410 payment_id = payment_info [ 1 ]
4511
@@ -52,7 +18,7 @@ def fund_wallet(amount, priority = 'primary')
5218
5319 # Delete a payment method.
5420 def delete_payment_method ( priority )
55- payment_info = EasyPost :: Services :: Billing . get_payment_method_info ( priority . downcase )
21+ payment_info = get_payment_method_info ( priority . downcase )
5622 endpoint = payment_info [ 0 ]
5723 payment_id = payment_info [ 1 ]
5824
@@ -64,7 +30,7 @@ def delete_payment_method(priority)
6430
6531 # Retrieve all payment methods.
6632 def retrieve_payment_methods
67- response = @client . make_request ( :get , '/v2/ payment_methods' )
33+ response = @client . make_request ( :get , '/payment_methods' )
6834 payment_methods = EasyPost ::InternalUtilities ::Json . convert_json_to_object ( response )
6935
7036 if payment_methods [ 'id' ] . nil?
@@ -73,4 +39,40 @@ def retrieve_payment_methods
7339
7440 payment_methods
7541 end
42+
43+ private
44+
45+ # Get payment method info (type of the payment method and ID of the payment method)
46+ def get_payment_method_info ( priority )
47+ payment_methods = retrieve_payment_methods
48+ payment_method_map = {
49+ 'primary' => 'primary_payment_method' ,
50+ 'secondary' => 'secondary_payment_method' ,
51+ }
52+
53+ payment_method_to_use = payment_method_map [ priority ]
54+
55+ error_string = EasyPost ::Constants ::INVALID_PAYMENT_METHOD
56+ suggestion = "Please use a valid payment method: #{ payment_method_map . keys . join ( ', ' ) } "
57+ if payment_methods [ payment_method_to_use ] . nil?
58+ raise EasyPost ::Errors ::InvalidParameterError . new (
59+ error_string ,
60+ suggestion ,
61+ )
62+ end
63+
64+ payment_method_id = payment_methods [ payment_method_to_use ] [ 'id' ]
65+
66+ unless payment_method_id . nil?
67+ if payment_method_id . start_with? ( 'card_' )
68+ endpoint = '/credit_cards'
69+ elsif payment_method_id . start_with? ( 'bank_' )
70+ endpoint = '/bank_accounts'
71+ else
72+ raise EasyPost ::Errors ::InvalidObjectError . new ( error_string )
73+ end
74+ end
75+
76+ [ endpoint , payment_method_id ]
77+ end
7678end
0 commit comments