Class: Pay::Stripe::PaymentMethod
- Inherits:
-
Object
- Object
- Pay::Stripe::PaymentMethod
- Defined in:
- lib/pay/stripe/payment_method.rb
Instance Attribute Summary collapse
-
#pay_payment_method ⇒ Object
readonly
Returns the value of attribute pay_payment_method.
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.
-
#initialize(pay_payment_method) ⇒ PaymentMethod
constructor
A new instance of PaymentMethod.
-
#make_default! ⇒ Object
Sets payment method as default.
Constructor Details
#initialize(pay_payment_method) ⇒ PaymentMethod
Returns a new instance of PaymentMethod.
8 9 10 |
# File 'lib/pay/stripe/payment_method.rb', line 8 def initialize(pay_payment_method) @pay_payment_method = pay_payment_method end |
Instance Attribute Details
#pay_payment_method ⇒ Object (readonly)
Returns the value of attribute pay_payment_method.
4 5 6 |
# File 'lib/pay/stripe/payment_method.rb', line 4 def pay_payment_method @pay_payment_method end |
Class Method Details
.extract_attributes(payment_method) ⇒ Object
Extracts payment method details from a Stripe::PaymentMethod object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pay/stripe/payment_method.rb', line 62 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
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 |
# File 'lib/pay/stripe/payment_method.rb', line 29 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.customer.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
13 14 15 16 17 18 |
# File 'lib/pay/stripe/payment_method.rb', line 13 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
21 22 23 24 25 26 |
# File 'lib/pay/stripe/payment_method.rb', line 21 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
82 83 84 |
# File 'lib/pay/stripe/payment_method.rb', line 82 def detach ::Stripe::PaymentMethod.detach(processor_id, ) end |
#make_default! ⇒ Object
Sets payment method as default
77 78 79 |
# File 'lib/pay/stripe/payment_method.rb', line 77 def make_default! ::Stripe::Customer.update(customer.processor_id, {invoice_settings: {default_payment_method: processor_id}}, ) end |