Class: EasyPost::Services::Billing

Inherits:
Service
  • Object
show all
Defined in:
lib/easypost/services/billing.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from EasyPost::Services::Service

Class Method Details

.get_payment_method_info(priority) ⇒ Object

Get payment method info (type of the payment method and ID of the payment method)



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
# File 'lib/easypost/services/billing.rb', line 7

def self.get_payment_method_info(priority)
  payment_methods = EasyPost::Services::Billing.retrieve_payment_methods
  payment_method_map = {
    'primary' => 'primary_payment_method',
    'secondary' => 'secondary_payment_method',
  }

  payment_method_to_use = payment_method_map[priority]

  error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
  suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
  if payment_methods[payment_method_to_use].nil?
    raise EasyPost::Errors::InvalidParameterError.new(
      error_string,
      suggestion,
    )
  end

  payment_method_id = payment_methods[payment_method_to_use]['id']

  unless payment_method_id.nil?
    if payment_method_id.start_with?('card_')
      endpoint = '/v2/credit_cards'
    elsif payment_method_id.start_with?('bank_')
      endpoint = '/v2/bank_accounts'
    else
      raise EasyPost::Errors::InvalidObjectError.new(error_string)
    end
  end

  [endpoint, payment_method_id]
end

Instance Method Details

#delete_payment_method(priority) ⇒ Object

Delete a payment method.



54
55
56
57
58
59
60
61
62
63
# File 'lib/easypost/services/billing.rb', line 54

def delete_payment_method(priority)
  payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
  endpoint = payment_info[0]
  payment_id = payment_info[1]

  @client.make_request(:delete, "#{endpoint}/#{payment_id}")

  # Return true if succeeds, an error will be thrown if it fails
  true
end

#fund_wallet(amount, priority = 'primary') ⇒ Object

Fund your EasyPost wallet by charging your primary or secondary card on file.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easypost/services/billing.rb', line 41

def fund_wallet(amount, priority = 'primary')
  payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
  endpoint = payment_info[0]
  payment_id = payment_info[1]

  wrapped_params = { amount: amount }
  @client.make_request(:post, "#{endpoint}/#{payment_id}/charges", EasyPost::Models::EasyPostObject, wrapped_params)

  # Return true if succeeds, an error will be thrown if it fails
  true
end

#retrieve_payment_methodsObject

Retrieve all payment methods.



66
67
68
69
70
71
72
73
74
# File 'lib/easypost/services/billing.rb', line 66

def retrieve_payment_methods
  response = @client.make_request(:get, '/v2/payment_methods')

  if response['id'].nil?
    raise EasyPost::Errors::InvalidObjectError.new(EasyPost::Constants::NO_PAYMENT_METHODS)
  end

  response
end