class Paidy::Charge

Attributes

capture_id[R]
id[R]

Public Class Methods

create(params) click to toggle source
# File lib/paidy/charge.rb, line 4
def create(params)
  res = Paidy.request(:post, 'payments', params, {})

  self.new(res['id'])
end
new(id) click to toggle source
# File lib/paidy/charge.rb, line 18
def initialize(id)
  @id = id
  @capture_id = nil
end
retrieve(id) click to toggle source
# File lib/paidy/charge.rb, line 10
def retrieve(id)
  instance = self.new(id)
  instance.refresh

  instance
end

Public Instance Methods

capture() click to toggle source
# File lib/paidy/charge.rb, line 25
def capture
  res = Paidy.request(:post, "#{base_path}/captures", {}, {})
  @capture_id = res['captures'][0]['id']

  self
end
close() click to toggle source
# File lib/paidy/charge.rb, line 32
def close
  res = Paidy.request(:post, "#{base_path}/close", {}, {})

  self
end
refresh() click to toggle source
# File lib/paidy/charge.rb, line 56
def refresh
  res = Paidy.request(:get, "payments/#{id}")

  if res['status'] == 'closed' && res['captures'].present?
    @capture_id = res['captures'][0]['id']
  end
end
refund(amount: nil, refund_reason: nil) click to toggle source
# File lib/paidy/charge.rb, line 38
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) click to toggle source
# File lib/paidy/charge.rb, line 48
def refund_or_close(amount: nil, refund_reason: nil)
  if capture_id.nil?
    close
  else
    refund(amount: amount, refund_reason: refund_reason)
  end
end

Private Instance Methods

base_path() click to toggle source
# File lib/paidy/charge.rb, line 66
def base_path
  "payments/#{id}"
end