Class: Paidy::Charge

Inherits:
Object
  • Object
show all
Defined in:
lib/paidy/charge.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Charge

Returns a new instance of Charge.



18
19
20
21
# File 'lib/paidy/charge.rb', line 18

def initialize(id)
  @id = id
  @capture_id = nil
end

Instance Attribute Details

#capture_idObject (readonly)

Returns the value of attribute capture_id.



23
24
25
# File 'lib/paidy/charge.rb', line 23

def capture_id
  @capture_id
end

#idObject (readonly)

Returns the value of attribute id.



23
24
25
# File 'lib/paidy/charge.rb', line 23

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



23
24
25
# File 'lib/paidy/charge.rb', line 23

def status
  @status
end

Class Method Details

.create(params) ⇒ Object



4
5
6
7
8
# File 'lib/paidy/charge.rb', line 4

def create(params)
  res = Paidy.request(:post, 'payments', params, {})

  self.new(res['id'])
end

.retrieve(id) ⇒ Object



10
11
12
13
14
15
# File 'lib/paidy/charge.rb', line 10

def retrieve(id)
  instance = self.new(id)
  instance.refresh

  instance
end

Instance Method Details

#captureObject



25
26
27
28
29
30
# File 'lib/paidy/charge.rb', line 25

def capture
  res = Paidy.request(:post, "#{base_path}/captures", {}, {})
  @capture_id = res['captures'][0]['id']

  self
end

#closeObject



32
33
34
35
36
# File 'lib/paidy/charge.rb', line 32

def close
  res = Paidy.request(:post, "#{base_path}/close", {}, {})

  self
end

#refreshObject



52
53
54
55
56
57
58
# File 'lib/paidy/charge.rb', line 52

def refresh
  res = Paidy.request(:get, "payments/#{id}")

  if res['status'] == 'closed' && res['captures'].present?
    @capture_id = res['captures'][0]['id']
  end
end

#refundObject



38
39
40
41
42
# File 'lib/paidy/charge.rb', line 38

def refund
  res = Paidy.request(:post, "#{base_path}/refund", { capture_id: capture_id }, {})

  self
end

#refund_or_closeObject



44
45
46
47
48
49
50
# File 'lib/paidy/charge.rb', line 44

def refund_or_close
  if capture_id.nil?
    close
  else
    refund
  end
end