Skip to content

Instantly share code, notes, and snippets.

@timokleemann
Created February 23, 2023 11:19
Show Gist options
  • Save timokleemann/e8286d7fd3bbe985c8c61a6697429e18 to your computer and use it in GitHub Desktop.
Save timokleemann/e8286d7fd3bbe985c8c61a6697429e18 to your computer and use it in GitHub Desktop.
Update Stripe prices
class Subscriptions::UpdatePrices < ApplicationService
# TODO: Delete after prices have been updated
PRICE_MATCHES = {
"plan_DOhUc1P6Qesw2f" => "price_1MdZGu2KzYRlf4h1FAiZdBWE",
"plan_DOhU5vUKafMQFa" => "price_1MdZGg2KzYRlf4h1V3Kd8omc",
"plan_DOhUJYfIE3CGPO" => "price_1MdZG02KzYRlf4h1cfZW4M4L",
"plan_DOhUIG5uF2eCFE" => "price_1MdZFg2KzYRlf4h1R0yISwAN",
"plan_DOhUQmjIe68Brc" => "price_1MdZEC2KzYRlf4h1oainNAlS",
"plan_DOhUF68re1Nrm6" => "price_1MdZDu2KzYRlf4h1bHU91Ngq"
}
def call
count = 0
starting_after = nil
subscriptions = []
loop do
results = Stripe::Subscription.list({:limit => 100, :starting_after => starting_after})
break if results.data.length == 0
subscriptions = subscriptions + results.data
starting_after = results.data.last.id
end
subscriptions.each do |subscription|
subscription_items = Stripe::SubscriptionItem.list({:limit => 100, :subscription => subscription.id})
subscription_items.each do |subscription_item|
new_price_id = PRICE_MATCHES.fetch(subscription_item.price.id, nil)
if new_price_id.present?
begin
Stripe::SubscriptionItem.update(
subscription_item.id,
{
:price => new_price_id
}
)
count = count + 1
puts "Subscription item #{subscription_item.id} successfully updated."
rescue Stripe::StripeError => e
puts "Subscription item #{subscription_item.id} could NOT be updated. Stripe error: #{e.message}"
end
end
end
end
puts "#{count} Subscription items were successfully updated."
return nil
rescue Stripe::StripeError => e
Rails.logger.error("Stripe error: #{e.message}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment