Class: Pay::Stripe::Charge

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_charge) ⇒ Charge

Returns a new instance of Charge.



37
38
39
# File 'lib/pay/stripe/charge.rb', line 37

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/stripe/charge.rb', line 4

def pay_charge
  @pay_charge
end

Class Method Details

.sync(charge_id, object: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pay/stripe/charge.rb', line 8

def self.sync(charge_id, object: nil)
  object ||= ::Stripe::Charge.retrieve(id: charge_id)
  owner = Pay.find_billable(processor: :stripe, processor_id: object.customer)
  return unless owner

  attrs = {
    amount: object.amount,
    amount_refunded: object.amount_refunded,
    application_fee_amount: object.application_fee_amount,
    card_exp_month: object.payment_method_details.card.exp_month,
    card_exp_year: object.payment_method_details.card.exp_year,
    card_last4: object.payment_method_details.card.last4,
    card_type: object.payment_method_details.card.brand,
    created_at: Time.at(object.created),
    currency: object.currency,
    stripe_account: owner.
  }

  # Associate charge with subscription if we can
  if object.invoice
    invoice = (object.invoice.is_a?(::Stripe::Invoice) ? object.invoice : ::Stripe::Invoice.retrieve(object.invoice))
    attrs[:subscription] = Pay::Subscription.find_by(processor: :stripe, processor_id: invoice.subscription)
  end

  pay_charge = owner.charges.find_or_initialize_by(processor: :stripe, processor_id: object.id)
  pay_charge.update(attrs)
  pay_charge
end

Instance Method Details

#chargeObject



41
42
43
44
45
# File 'lib/pay/stripe/charge.rb', line 41

def charge
  ::Stripe::Charge.retrieve({id: processor_id, expand: ["customer", "invoice.subscription"]}, {stripe_account: })
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#refund!(amount_to_refund, **options) ⇒ Object

stripe.com/docs/api/refunds/create

refund! refund!(5_00) refund!(5_00, refund_application_fee: true)



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

def refund!(amount_to_refund, **options)
  ::Stripe::Refund.create(options.merge(charge: processor_id, amount: amount_to_refund), {stripe_account: })
  pay_charge.update(amount_refunded: amount_to_refund)
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end