Class: SynapsePayRest::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay_rest/models/transaction/transaction.rb

Overview

TODO:

use mixins to remove duplication between Node and BaseNode.

TODO:

reduce duplicated logic between User/BaseNode/Transaction

Represents a transaction record and holds methods for constructing transaction instances from API calls. This is built on top of the SynapsePayRest::Transactions class and is intended to make it easier to use the API without knowing payload formats or knowledge of REST.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Transaction

Note:

Do not call directly. Use Transaction.create or other class method to instantiate via API action.

Returns a new instance of Transaction.



201
202
203
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 201

def initialize(**options)
  options.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def asset
  @asset
end

#client_idObject (readonly)

Returns the value of attribute client_id.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def client_id
  @client_id
end

#client_nameObject (readonly)

Returns the value of attribute client_name.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def client_name
  @client_name
end

#created_onObject (readonly)

Returns the value of attribute created_on.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def created_on
  @created_on
end

#currencyObject (readonly)

Returns the value of attribute currency.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def currency
  @currency
end

#fee_amountObject (readonly)

Returns the value of attribute fee_amount.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def fee_amount
  @fee_amount
end

#fee_noteObject (readonly)

Returns the value of attribute fee_note.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def fee_note
  @fee_note
end

#fee_to_idObject (readonly)

Returns the value of attribute fee_to_id.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def fee_to_id
  @fee_to_id
end

#feesObject (readonly)

Returns the value of attribute fees.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def fees
  @fees
end

#fromObject (readonly)

Returns the value of attribute from.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def from
  @from
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def id
  @id
end

#ipObject (readonly)

Returns the value of attribute ip.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def ip
  @ip
end

#latlonObject (readonly)

Returns the value of attribute latlon.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def latlon
  @latlon
end

#nodeSynapsePayRest::Node

Returns the node to which the transaction belongs.

Returns:



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def node
  @node
end

#noteObject (readonly)

Returns the value of attribute note.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def note
  @note
end

#process_onObject (readonly)

Returns the value of attribute process_on.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def process_on
  @process_on
end

#recent_statusObject (readonly)

Returns the value of attribute recent_status.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def recent_status
  @recent_status
end

#same_dayObject (readonly)

Returns the value of attribute same_day.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def same_day
  @same_day
end

#supp_idObject (readonly)

Returns the value of attribute supp_id.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def supp_id
  @supp_id
end

#timelineObject (readonly)

Returns the value of attribute timeline.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def timeline
  @timeline
end

#toObject (readonly)

Returns the value of attribute to.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def to
  @to
end

#to_idObject (readonly)

Returns the value of attribute to_id.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def to_id
  @to_id
end

#to_typeObject (readonly)

Returns the value of attribute to_type.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def to_type
  @to_type
end

#webhookObject (readonly)

Returns the value of attribute webhook.



12
13
14
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12

def webhook
  @webhook
end

Class Method Details

.all(node:, page: nil, per_page: nil) ⇒ Array<SynapsePayRest::Transaction>

Queries the API for all transactions belonging to the supplied node and returns them as Transaction instances.

Parameters:

  • node (SynapsePayRest::BaseNode)

    node to which the transaction belongs

  • page (String, Integer) (defaults to: nil)

    (optional) response will default to 1

  • per_page (String, Integer) (defaults to: nil)

    (optional) response will default to 20

Returns:

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 97

def all(node:, page: nil, per_page: nil)
  raise ArgumentError, 'node must be a type of BaseNode object' unless node.is_a?(BaseNode)
  [page, per_page].each do |arg|
    if arg && (!arg.is_a?(Integer) || arg < 1)
      raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
    end
  end

  response = node.user.client.trans.get(
    user_id: node.user.id,
    node_id: node.id,
    page: page,
    per_page: per_page
  )
  multiple_from_response(node, response['trans'])
end

.create(node:, to_type:, to_id:, amount:, currency:, ip:, **options) ⇒ SynapsePayRest::Transaction

TODO:

allow either to_node or to_type/to_id

TODO:

allow node to be entered as alternative to fee_to node

TODO:

validate if fee_to node is synapse-us

TODO:

allow multiple fees

Creates a new transaction in the API belonging to the provided node and returns a transaction instance from the response data.

Parameters:

  • node (SynapsePayRest::BaseNode)

    node to which the transaction belongs

  • to_id (String)

    node id of the receiving node

  • to_type (String)

    node type of the receiving node

  • amount (Float)

    100.0 = $100.00 for example

  • currency (String)

    e.g. ‘USD’

  • ip (String)
  • note (String)

    (optional)

  • process_in (Integer)

    (optional) days until processed (default/minimum 1)

  • fees (Array)

    (optional) fee amounts to add to the transaction. Example: [1.0, note: ‘Test Fee’, to: {id: ‘fee_node_id’}]

  • fee_amount (Float)

    (deprecated) fee amount to add to the transaction

  • fee_to_id (String)

    (deprecated) node id to which to send the fee (must be SYNAPSE-US)

  • fee_note (String)

    (deprecated)

  • supp_id (String)

    (optional)

  • idempotency_key (String)

    (optional)

Returns:

Raises:

See Also:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 45

def create(node:, to_type:, to_id:, amount:, currency:, ip:, **options)
  raise ArgumentError, 'cannot create a transaction with an UnverifiedNode' if node.is_a?(UnverifiedNode)
  raise ArgumentError, 'node must be a type of BaseNode object' unless node.is_a?(BaseNode)
  raise ArgumentError, 'amount must be a Numeric (Integer or Float)' unless amount.is_a?(Numeric)
  [to_type, to_id, currency, ip].each do |arg|
    if options[arg] && !options[arg].is_a?(String)
      raise ArgumentError, "#{arg} must be a String"
    end
  end

  payload = payload_for_create(node: node, to_type: to_type, to_id: to_id,
    amount: amount, currency: currency, ip: ip, **options)
  response = node.user.client.trans.create(
    user_id: node.user.id,
    node_id: node.id,
    payload: payload,
    idempotency_key: options[:idempotency_key],
  )
  from_response(node, response)
end

.find(node:, id:) ⇒ SynapsePayRest::Transaction

Queries the API for a transaction belonging to the supplied node by transaction id and returns a Transaction instance if found.

Parameters:

  • node (SynapsePayRest::BaseNode)

    node to which the transaction belongs

  • id (String)

    id of the transaction to find

Returns:

Raises:



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 75

def find(node:, id:)
  raise ArgumentError, 'node must be a type of BaseNode object' unless node.is_a?(BaseNode)
  raise ArgumentError, 'id must be a String' unless id.is_a?(String)

  response = node.user.client.trans.get(
    user_id: node.user.id,
    node_id: node.id,
    trans_id: id
  )
  from_response(node, response)
end

.from_response(node, response) ⇒ Object

TODO:

convert the nodes and users in response into User/Node objects

TODO:

rework to handle multiple fees

Note:

Shouldn’t need to call this directly.

Creates a Transaction from a response hash.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 120

def from_response(node, response)
  args = {
    node:          node,
    id:            response['_id'],
    amount:        response['amount']['amount'],
    currency:      response['amount']['currency'],
    client_id:     response['client']['id'],
    client_name:   response['client']['name'],
    created_on:    response['extra']['created_on'],
    ip:            response['extra']['ip'],
    latlon:        response['extra']['latlon'],
    note:          response['extra']['note'],
    process_on:    response['extra']['process_on'],
    same_day:      response['extra']['same_day'],
    supp_id:       response['extra']['supp_id'],
    webhook:       response['extra']['webhook'],
    fees:          response['fees'],
    recent_status: response['recent_status'],
    timeline:      response['timeline'],
    from:          response['from'],
    to:            response['to'],
    to_type:       response['to']['type'],
    to_id:         response['to']['id']
  }
  if response['fees'].any?
    args[:fee_amount] = response['fees'].first['fee']
    args[:fee_note]   = response['fees'].first['note']
    args[:fee_to_id]  = response['fees'].first['to']['id']
  end
  self.new(args)
end

Instance Method Details

#==(other) ⇒ Object

Checks if two Transaction instances have same id (different instances of same record).



244
245
246
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 244

def ==(other)
  other.instance_of?(self.class) && !id.nil? && id == other.id
end

#add_comment(comment) ⇒ Array<SynapsePayRest::Transaction>

Adds a comment to the transaction’s timeline/recent_status fields.

Parameters:

  • comment (String)

Returns:

Raises:



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 212

def add_comment(comment)
  payload = {'comment' => comment}
  response = node.user.client.trans.update(
    user_id: node.user.id,
    node_id: node.id,
    trans_id: id,
    payload: payload
  )
  if response['trans']
    # api v3.1
    self.class.from_response(node, response['trans'])
  else
    # api v3.1.1
    self.class.from_response(node, response)
  end
end

#cancelArray<SynapsePayRest::Transaction>

Cancels this transaction if it has not already settled.

Returns:

Raises:



234
235
236
237
238
239
240
241
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 234

def cancel
  response = node.user.client.trans.delete(
    user_id: node.user.id,
    node_id: node.id,
    trans_id: id
  )
  self.class.from_response(node, response)
end