2021-12-17 13:00:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
require "stripe"
|
|
|
|
require "highline/import"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
desc "Import subscriptions from Stripe"
|
|
|
|
task "subscriptions:subscriptions_import" => :environment do
|
2021-12-17 13:00:19 -05:00
|
|
|
setup_api
|
|
|
|
products = get_stripe_products
|
|
|
|
strip_products_to_import = []
|
|
|
|
|
|
|
|
procourse_import = false
|
2022-12-29 07:35:06 -05:00
|
|
|
procourse_import_response =
|
|
|
|
ask("Were the subscriptions you are importing created in Procourse Memberships?: (y/N)")
|
|
|
|
procourse_import = true if procourse_import_response.downcase == "y"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
products.each do |product|
|
2022-12-29 07:35:06 -05:00
|
|
|
confirm_import =
|
|
|
|
ask("Do you wish to import product #{product[:name]} (id: #{product[:id]}): (y/N)")
|
|
|
|
next if confirm_import.downcase != "y"
|
2021-12-17 13:00:19 -05:00
|
|
|
strip_products_to_import << product
|
|
|
|
end
|
|
|
|
|
|
|
|
import_products(strip_products_to_import)
|
|
|
|
import_subscriptions(procourse_import)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_stripe_products(starting_after: nil)
|
2022-12-29 07:35:06 -05:00
|
|
|
puts "Getting products from Stripe API"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
all_products = []
|
|
|
|
|
|
|
|
loop do
|
2022-12-29 07:35:06 -05:00
|
|
|
products =
|
|
|
|
Stripe::Product.list({ type: "service", starting_after: starting_after, active: true })
|
2021-12-17 13:00:19 -05:00
|
|
|
all_products += products[:data]
|
|
|
|
break if products[:has_more] == false
|
|
|
|
starting_after = products[:data].last["id"]
|
|
|
|
end
|
|
|
|
|
|
|
|
all_products
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_stripe_subscriptions(starting_after: nil)
|
2022-12-29 07:35:06 -05:00
|
|
|
puts "Getting Subscriptions from Stripe API"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
all_subscriptions = []
|
|
|
|
|
|
|
|
loop do
|
2022-12-29 07:35:06 -05:00
|
|
|
subscriptions = Stripe::Subscription.list({ starting_after: starting_after, status: "active" })
|
2021-12-17 13:00:19 -05:00
|
|
|
all_subscriptions += subscriptions[:data]
|
|
|
|
break if subscriptions[:has_more] == false
|
|
|
|
starting_after = subscriptions[:data].last["id"]
|
|
|
|
end
|
|
|
|
|
|
|
|
all_subscriptions
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_stripe_customers(starting_after: nil)
|
2022-12-29 07:35:06 -05:00
|
|
|
puts "Getting Customers from Stripe API"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
all_customers = []
|
|
|
|
|
|
|
|
loop do
|
|
|
|
customers = Stripe::Customer.list({ starting_after: starting_after })
|
|
|
|
all_customers += customers[:data]
|
|
|
|
break if customers[:has_more] == false
|
|
|
|
starting_after = customers[:data].last["id"]
|
|
|
|
end
|
|
|
|
|
|
|
|
all_customers
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_products(products)
|
2022-12-29 07:35:06 -05:00
|
|
|
puts "Importing products:"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
products.each do |product|
|
|
|
|
puts "Looking for external_id #{product[:id]} ..."
|
|
|
|
if DiscourseSubscriptions::Product.find_by(external_id: product[:id]).blank?
|
|
|
|
DiscourseSubscriptions::Product.create(external_id: product[:id])
|
|
|
|
puts "Subscriptions Product external_id: #{product[:id]} CREATED"
|
|
|
|
else
|
|
|
|
puts "Subscriptions Product external_id: #{product[:id]} already exists"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_subscriptions(procourse_import)
|
2022-12-29 07:35:06 -05:00
|
|
|
puts "Importing subscriptions"
|
2021-12-17 13:00:19 -05:00
|
|
|
product_ids = DiscourseSubscriptions::Product.all.pluck(:external_id)
|
|
|
|
|
|
|
|
all_customers = get_stripe_customers
|
|
|
|
puts "Total available Stripe Customers: #{all_customers.length.to_s}, the first of which is customer id: #{all_customers[0][:description]}"
|
|
|
|
|
|
|
|
subscriptions = get_stripe_subscriptions
|
|
|
|
puts "Total Active Subscriptions available: #{subscriptions.length.to_s}"
|
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
subscriptions_for_products =
|
|
|
|
subscriptions.select { |sub| product_ids.include?(sub[:items][:data][0][:price][:product]) }
|
2021-12-17 13:00:19 -05:00
|
|
|
puts "Total Subscriptions matching Products to Import: #{subscriptions_for_products.length.to_s}"
|
|
|
|
|
|
|
|
subscriptions_for_products.each do |subscription|
|
|
|
|
product_id = subscription[:items][:data][0][:plan][:product]
|
|
|
|
customer_id = subscription[:customer]
|
|
|
|
subscription_id = subscription[:id]
|
|
|
|
|
|
|
|
if procourse_import
|
|
|
|
stripe_customer = all_customers.select { |cust| cust[:id] == customer_id }
|
|
|
|
user_id = stripe_customer[0][:description].to_i
|
|
|
|
username = nil
|
|
|
|
else
|
|
|
|
user_id = subscription[:metadata][:user_id].to_i
|
|
|
|
username = subscription[:metadata][:username]
|
|
|
|
end
|
|
|
|
|
|
|
|
if product_id && customer_id && subscription_id
|
2022-12-29 07:35:06 -05:00
|
|
|
subscriptions_customer =
|
|
|
|
DiscourseSubscriptions::Customer.find_by(
|
|
|
|
user_id: user_id,
|
|
|
|
customer_id: customer_id,
|
|
|
|
product_id: product_id,
|
|
|
|
)
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
if subscriptions_customer.nil? && user_id && user_id > 0
|
|
|
|
# create the customer record if doesn't exist only if the user_id and username match, which
|
|
|
|
# prevents issues if multiple sites use the same Stripe account. Does not apply to a Procourse import.
|
|
|
|
user = User.find(user_id)
|
|
|
|
if procourse_import || (user && (user.username == username))
|
2022-12-29 07:35:06 -05:00
|
|
|
subscriptions_customer =
|
|
|
|
DiscourseSubscriptions::Customer.create(
|
|
|
|
user_id: user_id,
|
|
|
|
customer_id: customer_id,
|
|
|
|
product_id: product_id,
|
|
|
|
)
|
|
|
|
puts "Subscriptions Customer user_id: #{user_id}, customer_id: #{customer_id}, product_id: #{product_id}) CREATED"
|
2021-12-17 13:00:19 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Subscriptions Customer user_id: #{user_id}, customer_id: #{customer_id}, product_id: #{product_id}) already exists"
|
|
|
|
end
|
|
|
|
|
|
|
|
if subscriptions_customer
|
2022-12-29 07:35:06 -05:00
|
|
|
if DiscourseSubscriptions::Subscription.find_by(
|
|
|
|
customer_id: subscriptions_customer.id,
|
|
|
|
external_id: subscription_id,
|
|
|
|
).blank?
|
2021-12-17 13:00:19 -05:00
|
|
|
DiscourseSubscriptions::Subscription.create(
|
|
|
|
customer_id: subscriptions_customer.id,
|
2022-12-29 07:35:06 -05:00
|
|
|
external_id: subscription_id,
|
2021-12-17 13:00:19 -05:00
|
|
|
)
|
|
|
|
puts "Discourse Subscription customer_id: #{subscriptions_customer.id}, external_id: #{subscription_id}) CREATED"
|
|
|
|
else
|
|
|
|
puts "Discourse Subscription customer_id: #{subscriptions_customer.id}, external_id: #{subscription_id}) already exists"
|
|
|
|
end
|
|
|
|
|
|
|
|
if procourse_import
|
|
|
|
# Update Procourse Stripe data as it would be if it were created by discourse_subscriptions
|
|
|
|
discourse_user = User.find(user_id)
|
|
|
|
puts "Discourse User: #{discourse_user.username_lower} found for Strip metadata update ..."
|
|
|
|
|
2022-12-29 07:35:06 -05:00
|
|
|
updated_subscription =
|
|
|
|
Stripe::Subscription.update(
|
|
|
|
subscription_id,
|
|
|
|
{ metadata: { user_id: user_id, username: discourse_user.username_lower } },
|
|
|
|
)
|
2022-06-22 19:51:14 -04:00
|
|
|
puts "Stripe Subscription: #{updated_subscription[:id]}, metadata: #{updated_subscription[:metadata]} UPDATED"
|
2021-12-17 13:00:19 -05:00
|
|
|
|
|
|
|
updated_customer = Stripe::Customer.update(customer_id, { email: discourse_user.email })
|
|
|
|
puts "Stripe Customer: #{updated_customer[:id]}, email: #{updated_customer[:email]} UPDATED"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_api
|
2022-12-29 07:35:06 -05:00
|
|
|
api_key = SiteSetting.discourse_subscriptions_secret_key || ask("Input Stripe secret key")
|
2021-12-17 13:00:19 -05:00
|
|
|
Stripe.api_key = api_key
|
|
|
|
end
|