Class: Pay::Stripe::Charge
- Inherits:
-
Object
- Object
- Pay::Stripe::Charge
- Defined in:
- lib/pay/stripe/charge.rb
Instance Attribute Summary collapse
-
#pay_charge ⇒ Object
readonly
Returns the value of attribute pay_charge.
Class Method Summary collapse
Instance Method Summary collapse
- #charge ⇒ Object
-
#initialize(pay_charge) ⇒ Charge
constructor
A new instance of Charge.
- #refund!(amount_to_refund, **options) ⇒ Object
Constructor Details
#initialize(pay_charge) ⇒ Charge
Returns a new instance of Charge.
57 58 59 |
# File 'lib/pay/stripe/charge.rb', line 57 def initialize(pay_charge) @pay_charge = pay_charge end |
Instance Attribute Details
#pay_charge ⇒ Object (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, stripe_account: nil, try: 0, retries: 1) ⇒ 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pay/stripe/charge.rb', line 8 def self.sync(charge_id, object: nil, stripe_account: nil, try: 0, retries: 1) # Skip loading the latest charge details from the API if we already have it object ||= ::Stripe::Charge.retrieve(charge_id, {stripe_account: stripe_account}.compact) pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.customer) return unless pay_customer payment_method = object.payment_method_details.send(object.payment_method_details.type) attrs = { amount: object.amount, amount_refunded: object.amount_refunded, application_fee_amount: object.application_fee_amount, created_at: Time.at(object.created), currency: object.currency, stripe_account: pay_customer.stripe_account, metadata: object., payment_method_type: object.payment_method_details.type, brand: payment_method.try(:brand)&.capitalize, last4: payment_method.try(:last4).to_s, exp_month: payment_method.try(:exp_month).to_s, exp_year: payment_method.try(:exp_year).to_s, bank: payment_method.try(:bank_name) || payment_method.try(:bank) # eps, fpx, ideal, p24, acss_debit, etc } # Associate charge with subscription if we can if object.invoice invoice = (object.invoice.is_a?(::Stripe::Invoice) ? object.invoice : ::Stripe::Invoice.retrieve(object.invoice, {stripe_account: stripe_account}.compact)) attrs[:subscription] = pay_customer.subscriptions.find_by(processor_id: invoice.subscription) end # Update or create the charge if (pay_charge = pay_customer.charges.find_by(processor_id: object.id)) pay_charge.with_lock do pay_charge.update!(attrs) end pay_charge else pay_customer.charges.create!(attrs.merge(processor_id: object.id)) end rescue ActiveRecord::RecordInvalid try += 1 if try <= retries sleep 0.1 retry else raise end end |
Instance Method Details
#charge ⇒ Object
61 62 63 64 65 |
# File 'lib/pay/stripe/charge.rb', line 61 def charge ::Stripe::Charge.retrieve({id: processor_id, expand: ["customer", "invoice.subscription"]}, ) 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)
72 73 74 75 76 77 |
# File 'lib/pay/stripe/charge.rb', line 72 def refund!(amount_to_refund, **) ::Stripe::Refund.create(.merge(charge: processor_id, amount: amount_to_refund), ) pay_charge.update(amount_refunded: amount_to_refund) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |