Class: Pay::Stripe::PaymentMethod
- Inherits:
-
PaymentMethod
- Object
- ApplicationRecord
- PaymentMethod
- Pay::Stripe::PaymentMethod
- Defined in:
- app/models/pay/stripe/payment_method.rb
Class Method Summary collapse
-
.extract_attributes(payment_method) ⇒ Object
Extracts payment method details from a Stripe::PaymentMethod object.
-
.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) ⇒ Object
Syncs PaymentMethod objects from Stripe.
-
.sync_payment_intent(id, stripe_account: nil) ⇒ Object
Syncs a PaymentIntent’s payment method to the database.
-
.sync_setup_intent(id, stripe_account: nil) ⇒ Object
Syncs a SetupIntent’s payment method to the database.
Instance Method Summary collapse
-
#detach ⇒ Object
Remove payment method.
-
#make_default! ⇒ Object
Sets payment method as default.
Methods inherited from PaymentMethod
find_by_processor_and_id, pay_processor_for, #payment_processor
Class Method Details
.extract_attributes(payment_method) ⇒ Object
Extracts payment method details from a Stripe::PaymentMethod object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/pay/stripe/payment_method.rb', line 54 def self.extract_attributes(payment_method) details = payment_method.try(payment_method.type) { payment_method_type: payment_method.type, email: details.try(:email), # Link brand: details.try(:brand)&.capitalize, last4: details.try(:last4).to_s, exp_month: details.try(:exp_month).to_s, exp_year: details.try(:exp_year).to_s, bank: details.try(:bank_name) || details.try(:bank) # eps, fpx, ideal, p24, acss_debit, etc } end |
.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) ⇒ Object
Syncs PaymentMethod objects from Stripe
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 |
# File 'app/models/pay/stripe/payment_method.rb', line 21 def self.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) object ||= ::Stripe::PaymentMethod.retrieve(id, {stripe_account: stripe_account}.compact) if object.customer.blank? Rails.logger.debug "Stripe PaymentMethod #{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 PaymentMethod #{object.id}" return end default_payment_method_id = pay_customer.api_record.invoice_settings.default_payment_method default = (id == default_payment_method_id) attributes = extract_attributes(object).merge(default: default, stripe_account: stripe_account) pay_customer.payment_methods.update_all(default: false) if default pay_payment_method = pay_customer.payment_methods.where(processor_id: object.id).first_or_initialize pay_payment_method.update!(attributes) pay_payment_method rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 if try <= retries sleep 0.1 retry else raise end end |
.sync_payment_intent(id, stripe_account: nil) ⇒ Object
Syncs a PaymentIntent’s payment method to the database
5 6 7 8 9 10 |
# File 'app/models/pay/stripe/payment_method.rb', line 5 def self.sync_payment_intent(id, stripe_account: nil) payment_intent = ::Stripe::PaymentIntent.retrieve({id: id, expand: ["payment_method"]}, {stripe_account: stripe_account}.compact) payment_method = payment_intent.payment_method return unless payment_method Pay::Stripe::PaymentMethod.sync(payment_method.id, object: payment_method) end |
.sync_setup_intent(id, stripe_account: nil) ⇒ Object
Syncs a SetupIntent’s payment method to the database
13 14 15 16 17 18 |
# File 'app/models/pay/stripe/payment_method.rb', line 13 def self.sync_setup_intent(id, stripe_account: nil) setup_intent = ::Stripe::SetupIntent.retrieve({id: id, expand: ["payment_method"]}, {stripe_account: stripe_account}.compact) payment_method = setup_intent.payment_method return unless payment_method Pay::Stripe::PaymentMethod.sync(payment_method.id, object: payment_method) end |
Instance Method Details
#detach ⇒ Object
Remove payment method
74 75 76 |
# File 'app/models/pay/stripe/payment_method.rb', line 74 def detach ::Stripe::PaymentMethod.detach(processor_id, ) end |
#make_default! ⇒ Object
Sets payment method as default
69 70 71 |
# File 'app/models/pay/stripe/payment_method.rb', line 69 def make_default! ::Stripe::Customer.update(customer.processor_id, {invoice_settings: {default_payment_method: processor_id}}, ) end |