Class: Pay::Stripe::Charge
- Inherits:
-
Charge
- Object
- ApplicationRecord
- Charge
- Pay::Stripe::Charge
- Defined in:
- app/models/pay/stripe/charge.rb
Constant Summary collapse
- EXPAND =
["balance_transaction", "payment_intent", "refunds.data.balance_transaction"]
Class Method Summary collapse
- .sync(charge_id, object: nil, stripe_account: nil, try: 0, retries: 1) ⇒ Object
- .sync_payment_intent(id, stripe_account: nil) ⇒ Object
Instance Method Summary collapse
- #api_record ⇒ Object
- #capture(**options) ⇒ Object
- #captured? ⇒ Boolean
-
#credit_note!(**options) ⇒ Object
Adds a credit note to a Stripe Invoice.
-
#refund!(amount_to_refund, **options) ⇒ Object
Issues a CreditNote if there’s an invoice, otherwise uses a Refund This allows Tax to be handled properly.
- #stripe_invoice ⇒ Object
- #stripe_object ⇒ Object
Methods inherited from Charge
#amount_refunded_with_currency, #amount_with_currency, #charged_to, find_by_processor_and_id, #full_refund?, #partial_refund?, #refunded?, #sync!
Class Method Details
.sync(charge_id, object: nil, stripe_account: nil, try: 0, retries: 1) ⇒ Object
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/pay/stripe/charge.rb', line 16 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({id: charge_id, expand: EXPAND}, {stripe_account: stripe_account}.compact) if object.customer.blank? Rails.logger.debug "Stripe Charge #{object.id} does not have a customer" return end pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.customer) if pay_customer.blank? Rails.logger.debug "Pay::Customer #{object.customer} is not in the database while syncing Stripe Charge #{object.id}" return end payment_method = object.payment_method_details.try(object.payment_method_details.type) attrs = { object: object.to_hash, amount: object.amount, amount_refunded: object.amount_refunded, application_fee_amount: object.application_fee_amount, bank: payment_method.try(:bank_name) || payment_method.try(:bank), # eps, fpx, ideal, p24, acss_debit, etc brand: payment_method.try(:brand)&.capitalize, created_at: Time.at(object.created), currency: object.currency, exp_month: payment_method.try(:exp_month).to_s, exp_year: payment_method.try(:exp_year).to_s, last4: payment_method.try(:last4).to_s, metadata: object., payment_method_type: object.payment_method_details.type, stripe_account: pay_customer.stripe_account, stripe_receipt_url: object.receipt_url } # Associate charge with subscription if we can invoice_payments = ::Stripe::InvoicePayment.list({payment: {type: :payment_intent, payment_intent: object.payment_intent}, status: :paid, expand: ["data.invoice.total_discount_amounts.discount"]}, {stripe_account: stripe_account}.compact) if invoice_payments.any? && (invoice = invoice_payments.first.invoice) attrs[:stripe_invoice] = invoice.to_hash attrs[:subtotal] = invoice.subtotal attrs[:tax] = invoice.total - invoice.total_excluding_tax.to_i if (subscription = invoice.parent.try(:subscription_details).try(:subscription)) attrs[:subscription] = pay_customer.subscriptions.find_by(processor_id: subscription) end end # Update or create the charge if (pay_charge = find_by(customer: pay_customer, processor_id: object.id)) pay_charge.with_lock { pay_charge.update!(attrs) } pay_charge else create!(attrs.merge(customer: pay_customer, processor_id: object.id)) end rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique if try > retries raise else try += 1 sleep 0.15**try retry end end |
.sync_payment_intent(id, stripe_account: nil) ⇒ Object
11 12 13 14 |
# File 'app/models/pay/stripe/charge.rb', line 11 def self.sync_payment_intent(id, stripe_account: nil) payment_intent = ::Stripe::PaymentIntent.retrieve({id: id}, {stripe_account: stripe_account}.compact) sync(payment_intent.latest_charge, stripe_account: stripe_account) end |
Instance Method Details
#api_record ⇒ Object
77 78 79 80 81 |
# File 'app/models/pay/stripe/charge.rb', line 77 def api_record ::Stripe::Charge.retrieve({id: processor_id, expand: EXPAND}, ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#capture(**options) ⇒ Object
stripe.com/docs/payments/capture-later
capture capture(amount_to_capture: 15_00)
120 121 122 123 124 125 126 127 |
# File 'app/models/pay/stripe/charge.rb', line 120 def capture(**) raise Pay::Stripe::Error, "no payment_intent on charge" unless payment_intent.present? payment_intent_id = payment_intent.is_a?(::Stripe::PaymentIntent) ? payment_intent.id : payment_intent ::Stripe::PaymentIntent.capture(payment_intent_id, , ) self.class.sync(processor_id) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#captured? ⇒ Boolean
129 130 131 |
# File 'app/models/pay/stripe/charge.rb', line 129 def captured? amount_captured > 0 end |
#credit_note!(**options) ⇒ Object
Adds a credit note to a Stripe Invoice
108 109 110 111 112 113 114 |
# File 'app/models/pay/stripe/charge.rb', line 108 def credit_note!(**) raise Pay::Stripe::Error, "no Stripe Invoice on Pay::Charge" if stripe_invoice.blank? ::Stripe::CreditNote.create({invoice: stripe_invoice.id}.merge(), ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#refund!(amount_to_refund, **options) ⇒ Object
Issues a CreditNote if there’s an invoice, otherwise uses a Refund This allows Tax to be handled properly
stripe.com/docs/api/credit_notes/create stripe.com/docs/api/refunds/create
refund! refund!(5_00) refund!(5_00, refund_application_fee: true)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/pay/stripe/charge.rb', line 92 def refund!(amount_to_refund, **) amount_to_refund ||= amount if stripe_invoice.present? description = .delete(:description) || I18n.t("pay.refund") lines = [{type: :custom_line_item, description: description, quantity: 1, unit_amount: amount_to_refund}] credit_note!(**.merge(refund_amount: amount_to_refund, lines: lines)) else ::Stripe::Refund.create(.merge(charge: processor_id, amount: amount_to_refund), ) end update!(amount_refunded: amount_refunded + amount_to_refund) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#stripe_invoice ⇒ Object
133 134 135 136 137 |
# File 'app/models/pay/stripe/charge.rb', line 133 def stripe_invoice if (value = data.dig("stripe_invoice")) ::Stripe::Invoice.construct_from(value) end end |
#stripe_object ⇒ Object
139 140 141 |
# File 'app/models/pay/stripe/charge.rb', line 139 def stripe_object ::Stripe::Charge.construct_from(object) if object? end |