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
22
23
# File 'lib/paidy/charge.rb', line 18

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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#capture_idObject (readonly)

Returns the value of attribute capture_id.



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

def capture_id
  @capture_id
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



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

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



27
28
29
30
31
32
# File 'lib/paidy/charge.rb', line 27

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

  self
end

#closeObject



34
35
36
37
38
# File 'lib/paidy/charge.rb', line 34

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

  self
end

#refreshObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/paidy/charge.rb', line 58

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

  @amount = res['amount']
  @status = res['status']

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

#refund(amount: nil, refund_reason: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/paidy/charge.rb', line 40

def refund(amount: nil, refund_reason: nil)
  params = { capture_id: capture_id }
  params[:amount] = amount if amount.present?
  params[:reason] = refund_reason if refund_reason.present?

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

  self
end

#refund_or_close(amount: nil, refund_reason: nil) ⇒ Object



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

def refund_or_close(amount: nil, refund_reason: nil)
  if capture_id.nil?
    close
  else
    refund(amount: amount, refund_reason: refund_reason)
  end
end