mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-29 18:52:22 +01:00
save products and prices to stripe for each plans
This commit is contained in:
parent
6be51feeb2
commit
a9cc0b391b
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user