Skip to content

Commit

Permalink
Refactor class methods. Fixes #1123
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Dec 30, 2024
1 parent 663fa76 commit b7044fa
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/models/pay/braintree/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/pay/braintree/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
7 changes: 2 additions & 5 deletions app/models/pay/braintree/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/models/pay/lemon_squeezy/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions app/models/pay/lemon_squeezy/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions app/models/pay/paddle_billing/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/models/pay/paddle_billing/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/models/pay/stripe/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/pay/stripe/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/pay/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b7044fa

Please sign in to comment.