FIX: Allow more than 10 Stripe records (#62)

This commit is contained in:
haroldus- 2021-05-23 11:30:09 +10:00 committed by GitHub
parent d83425872f
commit ccd9454da8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 3 deletions

View File

@ -21,7 +21,28 @@ end
def get_stripe_products def get_stripe_products
puts 'Getting products from Stripe API' puts 'Getting products from Stripe API'
Stripe::Product.list starting_after = nil
products = []
loop do
results = Stripe::Product.list(limit: 100, starting_after: starting_after)
break if results.data.length == 0
products = products + results.data
starting_after = results.data.last.id
end
products
end
def get_stripe_subscriptions
puts 'Getting subscriptions from Stripe API'
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
end end
def import_products(products) def import_products(products)
@ -36,8 +57,8 @@ end
def import_subscriptions def import_subscriptions
puts 'Importing subscriptions' puts 'Importing subscriptions'
product_ids = DiscourseSubscriptions::Product.all.pluck(:external_id) product_ids = DiscourseSubscriptions::Product.all.pluck(:external_id)
subscriptions = Stripe::Subscription.list subscriptions = get_stripe_subscriptions
subscriptions_for_products = subscriptions[:data].select { |sub| product_ids.include?(sub[:items][:data][0][:plan][:product]) } subscriptions_for_products = subscriptions.select { |sub| product_ids.include?(sub[:items][:data][0][:plan][:product]) }
subscriptions_for_products.each do |subscription| subscriptions_for_products.each do |subscription|
product_id = subscription[:items][:data][0][:plan][:product] product_id = subscription[:items][:data][0][:plan][:product]