From a9cc0b391b5d961ecc4d6dde1294052c6822fe5f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 27 Oct 2020 16:46:38 +0100 Subject: [PATCH] save products and prices to stripe for each plans --- CHANGELOG.md | 1 + app/models/plan.rb | 22 +++++++--------------- app/workers/stripe_worker.rb | 29 +++++++++++++++++++++++++++++ lib/tasks/fablab/stripe.rake | 10 ++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c97dcc1b9..9c95b8b76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Removed fab-manager email address from the seeds - Fix a bug: in the settings area, boolean switches are always shown as false - Updated stripe gem to 5.21.0 +- [TODO DEPLOY] `rails fablab:stripe:plans_prices` ## v4.6.2 2020 October 23 diff --git a/app/models/plan.rb b/app/models/plan.rb index 673464e81..cc108cb81 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -23,7 +23,9 @@ class Plan < ApplicationRecord after_create :create_spaces_prices after_create :create_statistic_type after_create :set_name - after_create :create_stripe_price + after_create :update_stripe_price + + after_update :update_stripe_price, if: :saved_change_to_amount? validates :amount, :group, :base_name, presence: true validates :interval_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 } @@ -107,6 +109,10 @@ class Plan < ApplicationRecord StatisticType.where(statistic_index_id: stat_index.first.id).where('label LIKE ?', "%#{human_readable_duration}%").first end + def update_stripe_price + StripeWorker.perform_async(:create_stripe_price, self) + end + private def create_statistic_subtype @@ -125,18 +131,4 @@ class Plan < ApplicationRecord def set_name update_columns(name: human_readable_name) end - - def create_stripe_price - price = Stripe::Price.create( - { - currency: Setting.get('currency'), - unit_amount: amount, - metadata: { plan_id: id } - }, - { api_key: Setting.get('stripe_secret_key') } - ) - update_columns(stp_price_id: price.id) - rescue Stripe::StripeError => e - logger.error e - end end diff --git a/app/workers/stripe_worker.rb b/app/workers/stripe_worker.rb index 184b805d9..2640c2603 100644 --- a/app/workers/stripe_worker.rb +++ b/app/workers/stripe_worker.rb @@ -44,4 +44,33 @@ class StripeWorker cpn = Stripe::Coupon.retrieve(coupon_code, api_key: Setting.get('stripe_secret_key')) cpn.delete end + + def create_stripe_price(plan) + product = if !plan.stp_price_id.nil? + p = Stripe::Price.update( + plan.stp_price_id, + { metadata: { archived: true } }, + { api_key: Setting.get('stripe_secret_key') } + ) + p.product + else + p = Stripe::Product.create( + { + name: plan.name, + metadata: { plan_id: plan.id } + }, { api_key: Setting.get('stripe_secret_key') } + ) + p.id + end + + price = Stripe::Price.create( + { + currency: Setting.get('stripe_currency'), + unit_amount: plan.amount, + product: product + }, + { api_key: Setting.get('stripe_secret_key') } + ) + plan.update_columns(stp_price_id: price.id) + end end diff --git a/lib/tasks/fablab/stripe.rake b/lib/tasks/fablab/stripe.rake index 3b88fa376..9caf35471 100644 --- a/lib/tasks/fablab/stripe.rake +++ b/lib/tasks/fablab/stripe.rake @@ -53,6 +53,16 @@ namespace :fablab do puts 'Done' end + desc 'set stp_price_id to plans' + task plans_prices: :environment do + puts 'No plans, exiting...' and return if Plan.count.zero? + + w = StripeWorker.new + Plan.all.each do |p| + w.perform(:create_stripe_price, p) + end + end + def print_on_line(str) print "#{str}\r" $stdout.flush