Class: EasyPost::Billing

Inherits:
Resource show all
Defined in:
lib/easypost/billing.rb

Overview

Billing class that users can manage their payment and fund

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key, #name, #parent, #unsaved_values

Class Method Summary collapse

Methods inherited from Resource

all, class_name, create, #delete, each, #refresh, retrieve, #save, url, #url

Methods inherited from EasyPostObject

#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from EasyPost::EasyPostObject

Class Method Details

.delete_payment_method(primary_or_secondary, api_key = nil) ⇒ Object

Delete a payment method.



19
20
21
22
23
24
25
26
27
28
# File 'lib/easypost/billing.rb', line 19

def self.delete_payment_method(primary_or_secondary, api_key = nil)
  payment_info = EasyPost::Billing.get_payment_method_info(primary_or_secondary.downcase)
  endpoint = payment_info[0]
  payment_id = payment_info[1]

  EasyPost.make_request(:delete, "#{endpoint}/#{payment_id}", api_key)

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

.fund_wallet(amount, primary_or_secondary = 'primary', api_key = nil) ⇒ Object

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



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/easypost/billing.rb', line 6

def self.fund_wallet(amount, primary_or_secondary = 'primary', api_key = nil)
  payment_info = EasyPost::Billing.get_payment_method_info(primary_or_secondary.downcase)
  endpoint = payment_info[0]
  payment_id = payment_info[1]

  wrapped_params = { amount: amount }
  EasyPost.make_request(:post, "#{endpoint}/#{payment_id}/charges", api_key, wrapped_params)

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

.retrieve_payment_methods(api_key = nil) ⇒ Object

Retrieve all payment methods.



31
32
33
34
35
36
37
38
39
# File 'lib/easypost/billing.rb', line 31

def self.retrieve_payment_methods(api_key = nil)
  response = EasyPost.make_request(:get, '/v2/payment_methods', api_key)

  if response['id'].nil?
    raise EasyPost::Error.new('Billing has not been setup for this user. Please add a payment method.')
  end

  EasyPost::Util.convert_to_easypost_object(response, api_key)
end