Class: Balanced::Hold

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/balanced/resources/hold.rb

Overview

A Hold is a reservation of funds on a funding source such as a Card. This reservation is guaranteed until the expires_at date. You may capture the Hold at any time before then which will create a Debit and transfer the funds to your Marketplace. If you do not capture the Hold it will be marked as invalid which is represented by the is_void field being set to true.

By default a Hold is created using the most recently added funding source on the Account. You may specify a specific funding source such as a Card or BankAccount uri.

Instance Attribute Summary

Attributes included from Resource

#attributes

Instance Method Summary collapse

Methods included from Resource

#copy_from, #destroy, #find, included, #method_missing, #reload, #save, #warn_on_positional

Constructor Details

#initialize(attributes = {}) ⇒ Hold

Returns a new instance of Hold.



16
17
18
19
20
21
22
# File 'lib/balanced/resources/hold.rb', line 16

def initialize attributes = {}
  Balanced::Utils.stringify_keys! attributes
  unless attributes.has_key? 'uri'
    attributes['uri'] = self.class.uri
  end
  super attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Balanced::Resource

Instance Method Details

#capture(*args) ⇒ Debit

Captures a valid Hold and returns a Debit representing the transfer of funds from the buyer’s Account to your Marketplace.

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/balanced/resources/hold.rb', line 40

def capture *args
  warn_on_positional args

  options = args.last.is_a?(Hash) ? args.pop : {}
  amount = args[0] || options.fetch(:amount) { nil }
  appears_on_statement_as = args[1] || options.fetch(:appears_on_statement_as) { nil }
  meta = args[2] || options.fetch(:meta) { nil }
  description = args[3] || options.fetch(:description) { nil }

  amount ||= self.amount
  self..debit(
      :amount => amount,
      :appears_on_statement_as => appears_on_statement_as,
      :hold_uri => self.uri,
      :meta => meta,
      :description => description
  )
end

#voidObject

Cancels an active Hold.



26
27
28
29
30
31
32
33
34
# File 'lib/balanced/resources/hold.rb', line 26

def void
  self.is_void = true
  begin
    save
  rescue Balanced::Error
    self.is_void = false
    raise
  end
end