Skip to content

Commit

Permalink
Add Balance API
Browse files Browse the repository at this point in the history
Closes GH-169
  • Loading branch information
justincase committed Dec 1, 2024
1 parent f3cdeca commit f659c19
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/balances/get-primary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
primary_balance = Mollie::Balance.primary
2 changes: 2 additions & 0 deletions examples/balances/get-report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
report = balance.report
1 change: 1 addition & 0 deletions examples/balances/get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
2 changes: 2 additions & 0 deletions examples/balances/list-transactions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
balance = Mollie::Balance.get("bal_gVMhHKqSSRYJyPsuoPNFH")
transactions = balance.transactions
1 change: 1 addition & 0 deletions examples/balances/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
balances = Mollie::Balance.all
3 changes: 3 additions & 0 deletions lib/mollie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Mollie

require 'mollie/base'
require 'mollie/amount'
require 'mollie/balance'
require 'mollie/chargeback'
require 'mollie/client'
require 'mollie/customer'
Expand All @@ -31,6 +32,8 @@ module Mollie
require 'mollie/subscription'
require 'mollie/terminal'

require 'mollie/balance/report'
require 'mollie/balance/transaction'
require 'mollie/customer/mandate'
require 'mollie/customer/payment'
require 'mollie/customer/subscription'
Expand Down
53 changes: 53 additions & 0 deletions lib/mollie/balance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Mollie
class Balance < Base
attr_accessor :id,
:mode,
:currency,
:description,
:status,
:transfer_frequency,
:transfer_reference,
:_links

attr_reader :transfer_threshold,
:transfer_destination,
:available_amount,
:pending_amount,
:created_at

alias links _links

def self.primary(options = {})
get('primary', options)
end

def transfer_threshold=(amount)
@transfer_threshold = Mollie::Amount.new(amount)
end

def transfer_destination=(transfer_destination)
@transfer_destination = OpenStruct.new(transfer_destination) if transfer_destination.is_a?(Hash)
end

def available_amount=(amount)
@available_amount = Mollie::Amount.new(amount)
end

def pending_amount=(amount)
@pending_amount = Mollie::Amount.new(amount)
end

def created_at=(created_at)
@created_at = Time.parse(created_at.to_s)
end

def report(options = {})
response = Client.instance.perform_http_call("GET", "balances/#{id}", "report", {}, options)
Balance::Report.new(response)
end

def transactions(options = {})
Balance::Transaction.all(options.merge(balance_id: id))
end
end
end
25 changes: 25 additions & 0 deletions lib/mollie/balance/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Mollie
class Balance
class Report < Base
attr_accessor :balance_id,
:time_zone,
:grouping,
:totals,
:_links

attr_reader :from,
:until

alias links _links

def from=(from)
@from = Date.parse(from)
end

def until=(until_date)
# `until` is a reserved keyword
@until = Date.parse(until_date)
end
end
end
end
41 changes: 41 additions & 0 deletions lib/mollie/balance/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Mollie
class Balance
class Transaction < Base
attr_accessor :id,
:type,
:_links

attr_reader :result_amount,
:initial_amount,
:deductions,
:context,
:created_at

alias links _links

def self.embedded_resource_name
"balance_transactions"
end

def result_amount=(amount)
@result_amount = Mollie::Amount.new(amount)
end

def initial_amount=(amount)
@initial_amount = Mollie::Amount.new(amount)
end

def deductions=(amount)
@deductions = Mollie::Amount.new(amount)
end

def context=(context)
@context = OpenStruct.new(context) if context.is_a?(Hash)
end

def created_at=(created_at)
@created_at = Time.parse(created_at.to_s)
end
end
end
end
38 changes: 38 additions & 0 deletions test/fixtures/balances/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"resource": "balance",
"id": "bal_gVMhHKqSSRYJyPsuoPNFH",
"mode": "live",
"currency": "EUR",
"description": "Primary balance",
"availableAmount": {
"currency": "EUR",
"value": "905.25"
},
"pendingAmount": {
"currency": "EUR",
"value": "0.00"
},
"transferFrequency": "twice-a-month",
"transferThreshold": {
"currency": "EUR",
"value": "5.00"
},
"transferReference": "Mollie settlement",
"transferDestination": {
"type": "bank-account",
"beneficiaryName": "John Doe",
"bankAccount": "NL55INGB0000000000"
},
"status": "active",
"createdAt": "2019-01-10T12:06:28+00:00",
"_links": {
"self": {
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH",
"type": "application/hal+json"
},
"documentation": {
"href": "https://docs.mollie.com/reference/get-balance",
"type": "text/html"
}
}
}
34 changes: 34 additions & 0 deletions test/fixtures/balances/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"count": 1,
"_embedded": {
"balances": [
{
"resource": "balance",
"id": "bal_one"
},
{
"resource": "balance",
"id": "bal_two"
},
{
"resource": "balance",
"id": "bal_three"
}
]
},
"_links": {
"documentation": {
"href": "https://docs.mollie.com/reference/v2/balances-api/list-balances",
"type": "text/html"
},
"self": {
"href": "https://api.mollie.com/v2/balances?limit=5",
"type": "application/hal+json"
},
"previous": null,
"next": {
"href": "https://api.mollie.com/v2/balances?from=bal_gVMhHKqSSRYJyPsuoPABC&limit=5",
"type": "application/hal+json"
}
}
}
65 changes: 65 additions & 0 deletions test/fixtures/balances/list_transactions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"count": 2,
"_embedded": {
"balance_transactions": [
{
"resource": "balance_transaction",
"id": "baltr_QM24QwzUWR4ev4Xfgyt29A",
"type": "refund",
"resultAmount": {
"currency": "EUR",
"value": "-10.25"
},
"initialAmount": {
"currency": "EUR",
"value": "-10.00"
},
"deductions": {
"currency": "EUR",
"value": "-0.25"
},
"context": {
"paymentId": "tr_7UhSN1zuXS",
"refundId": "re_4qqhO89gsT"
},
"createdAt": "2021-01-10T12:06:28+00:00"
},
{
"resource": "balance_transaction",
"id": "baltr_WhmDwNYR87FPDbiwBhUXCh",
"type": "payment",
"resultAmount": {
"currency": "EUR",
"value": "9.71"
},
"initialAmount": {
"currency": "EUR",
"value": "10.00"
},
"deductions": {
"currency": "EUR",
"value": "-0.29"
},
"context": {
"paymentId": "tr_7UhSN1zuXS"
},
"createdAt": "2021-01-10T12:06:28+00:00"
}
]
},
"_links": {
"self": {
"href": "...",
"type": "application/hal+json"
},
"previous": null,
"next": {
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?from=baltr_rXeW2yPqqDUyfAqq8fS5Bg&limit=5",
"type": "application/hal+json"
},
"documentation": {
"href": "...",
"type": "text/html"
}
}
}
Loading

0 comments on commit f659c19

Please sign in to comment.