Skip to content

Commit 0476cab

Browse files
authored
Merge pull request #180 from mollie/GH-169
Add Balance API
2 parents f3cdeca + f659c19 commit 0476cab

File tree

15 files changed

+559
-0
lines changed

15 files changed

+559
-0
lines changed

examples/balances/get-primary.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
primary_balance = Mollie::Balance.primary

examples/balances/get-report.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
2+
report = balance.report

examples/balances/get.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
2+
transactions = balance.transactions

examples/balances/list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
balances = Mollie::Balance.all

lib/mollie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Mollie
1313

1414
require 'mollie/base'
1515
require 'mollie/amount'
16+
require 'mollie/balance'
1617
require 'mollie/chargeback'
1718
require 'mollie/client'
1819
require 'mollie/customer'
@@ -31,6 +32,8 @@ module Mollie
3132
require 'mollie/subscription'
3233
require 'mollie/terminal'
3334

35+
require 'mollie/balance/report'
36+
require 'mollie/balance/transaction'
3437
require 'mollie/customer/mandate'
3538
require 'mollie/customer/payment'
3639
require 'mollie/customer/subscription'

lib/mollie/balance.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Mollie
2+
class Balance < Base
3+
attr_accessor :id,
4+
:mode,
5+
:currency,
6+
:description,
7+
:status,
8+
:transfer_frequency,
9+
:transfer_reference,
10+
:_links
11+
12+
attr_reader :transfer_threshold,
13+
:transfer_destination,
14+
:available_amount,
15+
:pending_amount,
16+
:created_at
17+
18+
alias links _links
19+
20+
def self.primary(options = {})
21+
get('primary', options)
22+
end
23+
24+
def transfer_threshold=(amount)
25+
@transfer_threshold = Mollie::Amount.new(amount)
26+
end
27+
28+
def transfer_destination=(transfer_destination)
29+
@transfer_destination = OpenStruct.new(transfer_destination) if transfer_destination.is_a?(Hash)
30+
end
31+
32+
def available_amount=(amount)
33+
@available_amount = Mollie::Amount.new(amount)
34+
end
35+
36+
def pending_amount=(amount)
37+
@pending_amount = Mollie::Amount.new(amount)
38+
end
39+
40+
def created_at=(created_at)
41+
@created_at = Time.parse(created_at.to_s)
42+
end
43+
44+
def report(options = {})
45+
response = Client.instance.perform_http_call("GET", "balances/#{id}", "report", {}, options)
46+
Balance::Report.new(response)
47+
end
48+
49+
def transactions(options = {})
50+
Balance::Transaction.all(options.merge(balance_id: id))
51+
end
52+
end
53+
end

lib/mollie/balance/report.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Mollie
2+
class Balance
3+
class Report < Base
4+
attr_accessor :balance_id,
5+
:time_zone,
6+
:grouping,
7+
:totals,
8+
:_links
9+
10+
attr_reader :from,
11+
:until
12+
13+
alias links _links
14+
15+
def from=(from)
16+
@from = Date.parse(from)
17+
end
18+
19+
def until=(until_date)
20+
# `until` is a reserved keyword
21+
@until = Date.parse(until_date)
22+
end
23+
end
24+
end
25+
end

lib/mollie/balance/transaction.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Mollie
2+
class Balance
3+
class Transaction < Base
4+
attr_accessor :id,
5+
:type,
6+
:_links
7+
8+
attr_reader :result_amount,
9+
:initial_amount,
10+
:deductions,
11+
:context,
12+
:created_at
13+
14+
alias links _links
15+
16+
def self.embedded_resource_name
17+
"balance_transactions"
18+
end
19+
20+
def result_amount=(amount)
21+
@result_amount = Mollie::Amount.new(amount)
22+
end
23+
24+
def initial_amount=(amount)
25+
@initial_amount = Mollie::Amount.new(amount)
26+
end
27+
28+
def deductions=(amount)
29+
@deductions = Mollie::Amount.new(amount)
30+
end
31+
32+
def context=(context)
33+
@context = OpenStruct.new(context) if context.is_a?(Hash)
34+
end
35+
36+
def created_at=(created_at)
37+
@created_at = Time.parse(created_at.to_s)
38+
end
39+
end
40+
end
41+
end

test/fixtures/balances/get.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"resource": "balance",
3+
"id": "bal_gVMhHKqSSRYJyPsuoPNFH",
4+
"mode": "live",
5+
"currency": "EUR",
6+
"description": "Primary balance",
7+
"availableAmount": {
8+
"currency": "EUR",
9+
"value": "905.25"
10+
},
11+
"pendingAmount": {
12+
"currency": "EUR",
13+
"value": "0.00"
14+
},
15+
"transferFrequency": "twice-a-month",
16+
"transferThreshold": {
17+
"currency": "EUR",
18+
"value": "5.00"
19+
},
20+
"transferReference": "Mollie settlement",
21+
"transferDestination": {
22+
"type": "bank-account",
23+
"beneficiaryName": "John Doe",
24+
"bankAccount": "NL55INGB0000000000"
25+
},
26+
"status": "active",
27+
"createdAt": "2019-01-10T12:06:28+00:00",
28+
"_links": {
29+
"self": {
30+
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH",
31+
"type": "application/hal+json"
32+
},
33+
"documentation": {
34+
"href": "https://docs.mollie.com/reference/get-balance",
35+
"type": "text/html"
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)