· 7 years ago · Oct 02, 2018, 07:02 PM
1class GeneratePlansOnConnectedStripeAccountWorker
2 include Sidekiq::Worker
3
4 def perform(current_user_id)
5
6 @current_user = User.where(id: current_user_id).first
7 puts @current_user
8
9 if @current_user.stripe_account_id.present?
10
11 #iterate through every plan in the Plans table
12 Plan.all.each do |p|
13
14 #check if plan already exists on cleaners account
15 @existing_plan = Stripe::Plan.retrieve(p.stripe_plan_id, {:stripe_account =>@current_user.stripe_account_id})
16 #if it doesn't exist then create the plan on the cleaners account
17 unless @existing_plan
18
19 Stripe.api_key = ENV['STRIPE_API_KEY']
20 plan = Stripe::Plan.create(
21 {
22 :amount => p.amount,
23 :interval => p.interval,
24 :interval_count => p.interval_count,
25 :product => {
26 :name => p.product_name
27 },
28 :currency => "gbp",
29 :id => p.stripe_plan_id
30 }, :stripe_account =>@current_user.stripe_account_id)
31 end
32 end
33 else
34 puts "User has not linked their stripe account"
35 end
36 end
37end