Class: BraspagRest::Sale

Inherits:
Hashie::IUTrash show all
Includes:
Hashie::Extensions::Coercion
Defined in:
lib/braspag-rest/sale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hashie::IUTrash

#inverse_attributes

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/braspag-rest/sale.rb', line 5

def errors
  @errors
end

Class Method Details

.find(request_id, payment_id) ⇒ Object



15
16
17
18
19
# File 'lib/braspag-rest/sale.rb', line 15

def self.find(request_id, payment_id)
  response = BraspagRest::Request.get_sale(request_id, payment_id)

  new(response.parsed_body.merge('RequestId' => request_id))
end

.find_by_order_id(request_id, order_id) ⇒ Object



21
22
23
24
25
26
# File 'lib/braspag-rest/sale.rb', line 21

def self.find_by_order_id(request_id, order_id)
  response = BraspagRest::Request.get_sales_for_merchant_order_id(request_id, order_id)
  payments = response.parsed_body['Payments']

  Array(payments).map { |payment| BraspagRest::Sale.find(request_id, payment['PaymentId']) }
end

Instance Method Details

#cancel(amount = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/braspag-rest/sale.rb', line 40

def cancel(amount = nil)
  response = BraspagRest::Request.void(request_id, payment.id, amount)

  if response.success?
    reload
  else
    initialize_errors(response.parsed_body)
  end

  response.success?
end

#capture(amount = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/braspag-rest/sale.rb', line 52

def capture(amount = nil)
  response = BraspagRest::Request.capture(request_id, payment.id, (amount || payment.amount))

  if response.success?
    self.payment.initialize_attributes(response.parsed_body)
  else
    initialize_errors(response.parsed_body) and return false
  end

  payment.captured?
end

#reloadObject



64
65
66
67
68
69
70
71
# File 'lib/braspag-rest/sale.rb', line 64

def reload
  if !request_id.nil? && payment && !payment.id.nil?
    reloaded_reference = self.class.find(request_id, payment.id)
    self.initialize_attributes(reloaded_reference.inverse_attributes)
  end

  self
end

#saveObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/braspag-rest/sale.rb', line 28

def save
  response = BraspagRest::Request.authorize(request_id, inverse_attributes)

  if response.success?
    initialize_attributes(response.parsed_body)
  else
    initialize_errors(response.parsed_body) and return false
  end

  true
end