Class: Pay::PaddleClassic::Charge

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_charge) ⇒ Charge

Returns a new instance of Charge.



8
9
10
# File 'lib/pay/paddle_classic/charge.rb', line 8

def initialize(pay_charge)
  @pay_charge = pay_charge
end

Instance Attribute Details

#pay_chargeObject (readonly)

Returns the value of attribute pay_charge.



4
5
6
# File 'lib/pay/paddle_classic/charge.rb', line 4

def pay_charge
  @pay_charge
end

Instance Method Details

#chargeObject



12
13
14
15
16
17
18
19
# File 'lib/pay/paddle_classic/charge.rb', line 12

def charge
  return unless customer.subscription
  payments = PaddleClassic.client.payments.list(subscription_id: customer.subscription.processor_id)
  charges = payments.data.select { |p| p[:id].to_s == processor_id }
  charges.try(:first)
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#refund!(amount_to_refund) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pay/paddle_classic/charge.rb', line 21

def refund!(amount_to_refund)
  return unless customer.subscription
  payments = PaddleClassic.client.payments.list(subscription_id: customer.subscription.processor_id, is_paid: 1)
  if payments.total > 0
    PaddleClassic.client.payments.refund(order_id: payments.data.last[:id], amount: amount_to_refund)
    pay_charge.update(amount_refunded: amount_to_refund)
  else
    raise Error, "Payment not found"
  end
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end