Class: SynapsePayRest::Transaction
- Inherits:
-
Object
- Object
- SynapsePayRest::Transaction
- Defined in:
- lib/synapse_pay_rest/models/transaction/transaction.rb
Overview
use mixins to remove duplication between Node and BaseNode.
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
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_name ⇒ Object
readonly
Returns the value of attribute client_name.
-
#created_on ⇒ Object
readonly
Returns the value of attribute created_on.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#fee_amount ⇒ Object
readonly
Returns the value of attribute fee_amount.
-
#fee_note ⇒ Object
readonly
Returns the value of attribute fee_note.
-
#fee_to_id ⇒ Object
readonly
Returns the value of attribute fee_to_id.
-
#fees ⇒ Object
readonly
Returns the value of attribute fees.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#latlon ⇒ Object
readonly
Returns the value of attribute latlon.
-
#node ⇒ SynapsePayRest::Node
The node to which the transaction belongs.
-
#note ⇒ Object
readonly
Returns the value of attribute note.
-
#process_on ⇒ Object
readonly
Returns the value of attribute process_on.
-
#recent_status ⇒ Object
readonly
Returns the value of attribute recent_status.
-
#same_day ⇒ Object
readonly
Returns the value of attribute same_day.
-
#supp_id ⇒ Object
readonly
Returns the value of attribute supp_id.
-
#timeline ⇒ Object
readonly
Returns the value of attribute timeline.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#to_id ⇒ Object
readonly
Returns the value of attribute to_id.
-
#to_type ⇒ Object
readonly
Returns the value of attribute to_type.
-
#webhook ⇒ Object
readonly
Returns the value of attribute webhook.
Class Method Summary collapse
-
.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.
-
.create(node:, to_type:, to_id:, amount:, currency:, ip:, **options) ⇒ SynapsePayRest::Transaction
Creates a new transaction in the API belonging to the provided node and returns a transaction instance from the response data.
-
.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.
-
.from_response(node, response) ⇒ Object
Creates a Transaction from a response hash.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks if two Transaction instances have same id (different instances of same record).
-
#add_comment(comment) ⇒ Array<SynapsePayRest::Transaction>
Adds a comment to the transaction’s timeline/recent_status fields.
-
#cancel ⇒ Array<SynapsePayRest::Transaction>
Cancels this transaction if it has not already settled.
-
#initialize(**options) ⇒ Transaction
constructor
A new instance of Transaction.
Constructor Details
#initialize(**options) ⇒ Transaction
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(**) .each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#amount ⇒ Object (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 |
#asset ⇒ Object (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_id ⇒ Object (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_name ⇒ Object (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_on ⇒ Object (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 |
#currency ⇒ Object (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_amount ⇒ Object (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_note ⇒ Object (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_id ⇒ Object (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 |
#fees ⇒ Object (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 |
#from ⇒ Object (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 |
#id ⇒ Object (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 |
#ip ⇒ Object (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 |
#latlon ⇒ Object (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 |
#node ⇒ SynapsePayRest::Node
Returns the node to which the transaction belongs.
12 13 14 |
# File 'lib/synapse_pay_rest/models/transaction/transaction.rb', line 12 def node @node end |
#note ⇒ Object (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_on ⇒ Object (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_status ⇒ Object (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_day ⇒ Object (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_id ⇒ Object (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 |
#timeline ⇒ Object (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 |
#to ⇒ Object (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_id ⇒ Object (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_type ⇒ Object (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 |
#webhook ⇒ Object (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.
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
allow either to_node or to_type/to_id
allow node to be entered as alternative to fee_to node
validate if fee_to node is synapse-us
allow multiple fees
Creates a new transaction in the API belonging to the provided node and returns a transaction instance from the response data.
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:, **) 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 [arg] && ![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, **) response = node.user.client.trans.create( user_id: node.user.id, node_id: node.id, payload: payload, idempotency_key: [: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.
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
convert the nodes and users in response into User/Node objects
rework to handle multiple fees
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.
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 |
#cancel ⇒ Array<SynapsePayRest::Transaction>
Cancels this transaction if it has not already settled.
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 |