Class: Pay::Paddle::Charge

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle/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/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/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/charge.rb', line 12

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

#refund!(amount_to_refund) ⇒ Object


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

def refund!(amount_to_refund)
  return unless customer.subscription
  payments = PaddlePay::Subscription::Payment.list({subscription_id: customer.subscription.processor_id, is_paid: 1})
  if payments.count > 0
    PaddlePay::Subscription::Payment.refund(payments.last[:id], {amount: amount_to_refund})
    pay_charge.update(amount_refunded: amount_to_refund)
  else
    raise Error, "Payment not found"
  end
rescue ::PaddlePay::PaddlePayError => e
  raise Pay::Paddle::Error, e
end