Class: NitroPay::Transaction

Inherits:
Connection show all
Defined in:
lib/nitro_pay/transaction.rb

Overview

Transaction Obj, but can Abstract nested objs like Costumer

Instance Attribute Summary collapse

Attributes inherited from Connection

#api_version, #auth, #domain, #end_point, #end_point_versioned, #path, #protocol, #recurrent_tid, #request_params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#delete_json_request, #delete_request, #get_json_request, #get_request, #post_json_request, #post_request, #put_json_request, #put_request, #url_requested

Constructor Details

#initialize(params = {}) ⇒ Transaction

Constructor



10
11
12
13
14
# File 'lib/nitro_pay/transaction.rb', line 10

def initialize(params = {})
  super # super init call
  # Base redirect_link if test_env is set (so the redirect is just appended)
  self.redirect_link = "#{self.end_point_versioned}/transactions" if params[:test_env]
end

Instance Attribute Details

#respObject

Returns the value of attribute resp.



5
6
7
# File 'lib/nitro_pay/transaction.rb', line 5

def resp
  @resp
end

#sold_itemsObject

Returns the value of attribute sold_items.



7
8
9
# File 'lib/nitro_pay/transaction.rb', line 7

def sold_items
  @sold_items
end

#statusObject

can be API Status: self.status = NitroPay::Status.new OR/AND Transaction Status



6
7
8
# File 'lib/nitro_pay/transaction.rb', line 6

def status
  @status
end

#tidObject

Returns the value of attribute tid.



4
5
6
# File 'lib/nitro_pay/transaction.rb', line 4

def tid
  @tid
end

Class Method Details

.find(tid) ⇒ Object

STATIC methods ================

GET /api/transactions/:tid by the tid passed



127
128
# File 'lib/nitro_pay/transaction.rb', line 127

def self.find(tid)
end

Instance Method Details

#charge_page(full_resp = false) ⇒ Object

POST /api/transactions/page return operator page URL, like the Cielo Page



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nitro_pay/transaction.rb', line 35

def charge_page(full_resp=false)
  custom_http_params(skip_formatters=true)
  # SetUp redirect dynamic if is test
  self.request_params[:transaction][:redirect_link] = "#{self.redirect_link}" if self.request_params[:transaction][:test_env]

  # dynamic path (it is written when a specific method use it)
  self.path = 'checkouts'

  # using json_request because need only the answer (do not use something like it HTTP Code)
  self.resp = self.post_json_request unless full_resp
  self.resp = self.post_request if full_resp
  self.resp
end

#charge_store(full_resp = false) ⇒ Object

POST /api/transactions



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nitro_pay/transaction.rb', line 50

def charge_store(full_resp=false)
  custom_http_params

  # dynamic path (it is written when a specific method use it)
  self.path = 'transactions/store'

  # using json_request because need only the answer (do not use something like it HTTP Code)
  full_resp ? self.resp = self.post_request : self.resp = self.post_json_request

  # return it received resp
  self.resp
end

#checkout_page_urlObject

Return it Purchase URL, to pay on the OperatorPage



17
18
19
# File 'lib/nitro_pay/transaction.rb', line 17

def checkout_page_url
  hash_resp[:checkout_page]
end

#hash_respObject

return it hash resp when resp is a string



116
117
118
# File 'lib/nitro_pay/transaction.rb', line 116

def hash_resp
  self.resp.is_a?(String) ? JSON.parse(self.resp).it_keys_to_sym : self.resp
end

#payment_history(tid = nil, full_resp = false) ⇒ Object

Return the payments executed for the purchase passed



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nitro_pay/transaction.rb', line 88

def payment_history(tid=nil, full_resp=false)
  setup_tid(tid)
  self.path = "transactions/#{self.recurrent_tid}/subscription/payment_history"
  self.path = "#{self.path}#{self.request_params.it_keys_to_get_param}"

  # Perform the request
  full_resp ? self.resp = self.get_request : self.resp = self.get_json_request

  # return it received resp
  self.resp
end

#setup_tid(tid) ⇒ Object

Set it TID up



121
122
123
# File 'lib/nitro_pay/transaction.rb', line 121

def setup_tid(tid)
  tid ? self.recurrent_tid = tid : self.recurrent_tid = get_global_subscription[:tid]
end

#unformed_received_amountObject



30
31
32
# File 'lib/nitro_pay/transaction.rb', line 30

def unformed_received_amount
  NitroPay::Currency.to_operator_str self.hash_resp[:amount].to_s
end

#unsubscribe(tid = nil, full_resp = false) ⇒ Object

Stop a recurrence based on it transaction tid



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

def unsubscribe(tid=nil, full_resp=false)
  setup_tid(tid)
  self.path = "transactions/#{self.recurrent_tid}/subscription/unsubscribe"

  # Perform the request
  full_resp ? self.resp = self.delete_request : self.resp = self.delete_json_request

  # return it received resp
  self.resp
end

#up_to_date(tid = nil, full_resp = false) ⇒ Object

Check if a subscription is up-to-date or have any pending



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nitro_pay/transaction.rb', line 101

def up_to_date(tid=nil, full_resp=false)
  setup_tid(tid)

  # Create/customize the path & add the auth as param
  self.path = "transactions/#{self.recurrent_tid}/subscription/up-to-date"
  self.path = "#{self.path}#{self.request_params.it_keys_to_get_param}"

  # Perform the request
  full_resp ? self.resp = self.get_request : self.resp = self.get_json_request

  # return it received resp
  self.resp
end

#update_subscription(tid = nil, full_resp = false) ⇒ Object

Update the recurrence amount



64
65
66
67
68
69
70
71
72
73
# File 'lib/nitro_pay/transaction.rb', line 64

def update_subscription(tid=nil, full_resp=false)
  setup_tid(tid)
  self.path = "transactions/#{self.recurrent_tid}/subscription"

  # Perform the request
  full_resp ? self.resp = self.put_request : self.resp = self.put_json_request

  # return it received resp
  self.resp
end

#verifyObject

GET /api/transactions/:tid by it attr



22
23
24
25
26
27
28
# File 'lib/nitro_pay/transaction.rb', line 22

def verify
  auth_hash = {}
  !self.tid.nil? ? tid = self.tid : tid = self.resp[:tid]
  auth_hash[:auth] = self.request_params[:auth]
  if tid.nil? then return {error:'TID not received'} else self.path = "transactions/#{tid}#{auth_hash.it_keys_to_get_param}" end
  return self.get_json_request
end