diff --git a/app/models/pay/braintree/customer.rb b/app/models/pay/braintree/customer.rb index fd56a65e..55e37059 100644 --- a/app/models/pay/braintree/customer.rb +++ b/app/models/pay/braintree/customer.rb @@ -147,7 +147,7 @@ def save_transaction(transaction) end end - charge = charges.find_or_initialize_by(processor_id: transaction.id) + charge = Pay::Braintree::Charge.find_or_initialize_by(customer: self, processor_id: transaction.id) charge.update!(attrs) charge end diff --git a/app/models/pay/braintree/payment_method.rb b/app/models/pay/braintree/payment_method.rb index 1d12fa97..145d7594 100644 --- a/app/models/pay/braintree/payment_method.rb +++ b/app/models/pay/braintree/payment_method.rb @@ -4,7 +4,7 @@ class PaymentMethod < Pay::PaymentMethod def self.sync(id, object: nil, try: 0, retries: 1) object ||= Pay.braintree_gateway.payment_method.find(id) - pay_customer = Pay::Customer.find_by(processor: :braintree, processor_id: object.customer_id) + pay_customer = Pay::Braintree::Customer.find_by(processor_id: object.customer_id) return unless pay_customer pay_customer.save_payment_method(object, default: object.default?) diff --git a/app/models/pay/braintree/subscription.rb b/app/models/pay/braintree/subscription.rb index 5e07b7b2..8989c5b4 100644 --- a/app/models/pay/braintree/subscription.rb +++ b/app/models/pay/braintree/subscription.rb @@ -31,15 +31,12 @@ def self.sync(subscription_id, object: nil, name: nil, try: 0, retries: 1) attributes[:ends_at] = object.paid_through_date.end_of_day end - pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.id) - if pay_subscription + if (pay_subscription = find_by(customer: pay_customer, processor_id: object.id)) pay_subscription.with_lock { pay_subscription.update!(attributes) } else name ||= Pay.default_product_name - pay_subscription = pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.id)) + create!(attributes.merge(customer: pay_customer, name: name, processor_id: object.id)) end - - pay_subscription rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 if try <= retries diff --git a/app/models/pay/lemon_squeezy/charge.rb b/app/models/pay/lemon_squeezy/charge.rb index a331b702..279cf6f7 100644 --- a/app/models/pay/lemon_squeezy/charge.rb +++ b/app/models/pay/lemon_squeezy/charge.rb @@ -22,13 +22,11 @@ def self.sync_order(order_id, object: nil, try: 0, retries: 1) } # Update or create the charge - if (pay_charge = pay_customer.charges.find_by(processor_id: processor_id)) - pay_charge.with_lock do - pay_charge.update!(attributes) - end + if (pay_charge = find_by(customer: pay_customer, processor_id: processor_id)) + pay_charge.with_lock { pay_charge.update!(attributes) } pay_charge else - pay_customer.charges.create!(attributes.merge(processor_id: processor_id)) + create!(attributes.merge(customer: pay_customer, processor_id: processor_id)) end end diff --git a/app/models/pay/lemon_squeezy/subscription.rb b/app/models/pay/lemon_squeezy/subscription.rb index 2e8b0cbf..05d5a666 100644 --- a/app/models/pay/lemon_squeezy/subscription.rb +++ b/app/models/pay/lemon_squeezy/subscription.rb @@ -33,13 +33,11 @@ def self.sync(subscription_id, object: nil, name: Pay.default_product_name) end # Update or create the subscription - if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.id)) - pay_subscription.with_lock do - pay_subscription.update!(attributes) - end + if (pay_subscription = find_by(customer: pay_customer, processor_id: object.id)) + pay_subscription.with_lock { pay_subscription.update!(attributes) } pay_subscription else - pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.id)) + create!(attributes.merge(customer: pay_customer, name: name, processor_id: object.id)) end end diff --git a/app/models/pay/paddle_billing/charge.rb b/app/models/pay/paddle_billing/charge.rb index 3244efe5..ac1b7710 100644 --- a/app/models/pay/paddle_billing/charge.rb +++ b/app/models/pay/paddle_billing/charge.rb @@ -48,13 +48,11 @@ def self.sync(charge_id, object: nil, try: 0, retries: 1) 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 + if (pay_charge = find_by(customer: pay_customer, processor_id: object.id)) + pay_charge.with_lock { pay_charge.update!(attrs) } pay_charge else - pay_customer.charges.create!(attrs.merge(processor_id: object.id)) + create!(attrs.merge(customer: pay_customer, processor_id: object.id)) end rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 diff --git a/app/models/pay/paddle_billing/subscription.rb b/app/models/pay/paddle_billing/subscription.rb index 405d70e4..21b91762 100644 --- a/app/models/pay/paddle_billing/subscription.rb +++ b/app/models/pay/paddle_billing/subscription.rb @@ -54,13 +54,11 @@ def self.sync(subscription_id, object: nil, name: Pay.default_product_name, try: end # Update or create the subscription - if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: subscription_id)) - pay_subscription.with_lock do - pay_subscription.update!(attributes) - end + if (pay_subscription = find_by(customer: pay_customer, processor_id: subscription_id)) + pay_subscription.with_lock { pay_subscription.update!(attributes) } pay_subscription else - pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: subscription_id)) + create!(attributes.merge(customer: pay_customer, name: name, processor_id: subscription_id)) end rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 diff --git a/app/models/pay/stripe/charge.rb b/app/models/pay/stripe/charge.rb index f1974e0c..314a44ed 100644 --- a/app/models/pay/stripe/charge.rb +++ b/app/models/pay/stripe/charge.rb @@ -81,13 +81,11 @@ def self.sync(charge_id, object: nil, stripe_account: nil, try: 0, retries: 1) 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 + if (pay_charge = find_by(customer: pay_customer, processor_id: object.id)) + pay_charge.with_lock { pay_charge.update!(attrs) } pay_charge else - pay_customer.charges.create!(attrs.merge(processor_id: object.id)) + create!(attrs.merge(customer: pay_customer, processor_id: object.id)) end rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique try += 1 diff --git a/app/models/pay/stripe/payment_method.rb b/app/models/pay/stripe/payment_method.rb index a3b93e2a..c6ab235f 100644 --- a/app/models/pay/stripe/payment_method.rb +++ b/app/models/pay/stripe/payment_method.rb @@ -36,8 +36,8 @@ def self.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) 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 + where(customer: pay_customer).update_all(default: false) if default + pay_payment_method = where(customer: pay_customer, processor_id: object.id).first_or_initialize pay_payment_method.update!(attributes) pay_payment_method rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique diff --git a/app/models/pay/stripe/subscription.rb b/app/models/pay/stripe/subscription.rb index cdfccaab..8ac7f507 100644 --- a/app/models/pay/stripe/subscription.rb +++ b/app/models/pay/stripe/subscription.rb @@ -81,7 +81,7 @@ def self.sync(subscription_id, object: nil, name: nil, stripe_account: nil, try: end # Update or create the subscription - pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.id) + pay_subscription = find_by(customer: pay_customer, processor_id: object.id) if pay_subscription # If pause behavior is changing to `void`, record the pause start date # Any other pause status (or no pause at all) should have nil for start @@ -95,7 +95,7 @@ def self.sync(subscription_id, object: nil, name: nil, stripe_account: nil, try: else # Allow setting the subscription name in metadata, otherwise use the default name ||= object.metadata["pay_name"] || Pay.default_product_name - pay_subscription = pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.id)) + pay_subscription = create!(attributes.merge(customer: pay_customer, name: name, processor_id: object.id)) end # Cache the Stripe subscription on the Pay::Subscription that we return