Class: Charger::Transaction

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/charger/transaction.rb

Constant Summary collapse

TYPES =
%W{charge refund payment credit payment_authorization info adjustment}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/charger/transaction.rb', line 47

def self.all
  transactions = []
  params = {page: 1, per_page: 200}

  loop do
    response = page(params)
    break if response.empty?
    params[:page] += 1
    transactions += response
  end

  transactions
end

.find(id) ⇒ Object



34
35
36
# File 'lib/charger/transaction.rb', line 34

def self.find id
  new(client.get("transactions/#{id}")['transaction'])
end

.find_by_subscription_id(id, params = {}) ⇒ Array<Transaction>

Finds a set of transactions for a subscription

Optional Parameters:

* kinds[] : An array of transaction types, see above
* since_id : Returns transactions with an id greater than or equal to
  the one specified
* max_id : Returns transactions with an id less than or equal to the one
  specified
* since_date : (format YYYY-MM-DD) Returns transactions with a
  created_at date greater than or equal to the one specified
* until_date : (format YYYY-MM-DD) Returns transactions with a
  created_at date less than or equal to the one specified
* page : default is 1
* per_page : default is 20

Parameters:

  • id (Integer)
  • params (Hash) (defaults to: {})

    See optional params

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/charger/transaction.rb', line 79

def self.find_by_subscription_id id, params={}
  params = {:page => 1, :per_page => 20}.merge(params)
  transactions = []
  loop do
    response = client.get("subscriptions/#{id}/transactions", params: params)
    break if response.empty?
    response.each do |data|
      transactions << new(data['transaction'])
    end
    params[:page] += 1
  end
  transactions
end

.page(params = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/charger/transaction.rb', line 38

def self.page params={}
  params = {:page => 1, :per_page => 20}.merge(params)
  transactions = []
  client.get("transactions", params: params).each do |data|
    transactions << new(data['transaction'])
  end
  transactions
end

Instance Method Details

#reloadObject



28
29
30
31
32
# File 'lib/charger/transaction.rb', line 28

def reload
  return false if id.nil?
  self.attributes = client.get("transactions/#{id}")['transaction']
  true
end

#subscriptionObject



20
21
22
# File 'lib/charger/transaction.rb', line 20

def subscription
  @subscription ||= Subscription.find(subscription_id)
end

#subscription=(sub) ⇒ Object



24
25
26
# File 'lib/charger/transaction.rb', line 24

def subscription= sub
  @subscription = sub
end