mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
add stp_product_id to all items that can be scheduled
This commit is contained in:
parent
78847bdb4b
commit
ed5b90cbdc
@ -7,6 +7,7 @@
|
||||
- Fix a bug: when a cash coupon was used, an invalid amount is shown in the statistics
|
||||
- [TODO DEPLOY] `rails fablab:stripe:plans_prices`
|
||||
- [TODO DEPLOY] `rails fablab:maintenance:rebuild_stylesheet`
|
||||
- [TODO DEPLOY] `rails fabalb:stripe:set_product_id`
|
||||
|
||||
## v4.6.3 2020 October 28
|
||||
|
||||
|
@ -29,6 +29,8 @@ class Machine < ApplicationRecord
|
||||
|
||||
after_create :create_statistic_subtype
|
||||
after_create :create_machine_prices
|
||||
after_create :update_stripe_product
|
||||
after_update :update_stripe_product, if: :saved_change_to_name?
|
||||
after_update :update_statistic_subtype, if: :saved_change_to_name?
|
||||
after_destroy :remove_statistic_subtype
|
||||
|
||||
@ -72,4 +74,10 @@ class Machine < ApplicationRecord
|
||||
def destroyable?
|
||||
reservations.empty?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_stripe_product
|
||||
StripeWorker.perform_async(:create_or_update_stp_product, Machine.name, id)
|
||||
end
|
||||
end
|
||||
|
@ -8,10 +8,4 @@ class PaymentSchedule < ApplicationRecord
|
||||
belongs_to :coupon
|
||||
belongs_to :invoicing_profile
|
||||
belongs_to :operator_profile, foreign_key: :operator_profile_id, class_name: 'InvoicingProfile'
|
||||
|
||||
after_create :create_stripe_subscription
|
||||
|
||||
def create_stripe_subscription
|
||||
StripeWorker.perform_async(:create_stripe_subscription, id)
|
||||
end
|
||||
end
|
||||
|
@ -23,9 +23,8 @@ class Plan < ApplicationRecord
|
||||
after_create :create_spaces_prices
|
||||
after_create :create_statistic_type
|
||||
after_create :set_name
|
||||
after_create :update_stripe_price
|
||||
|
||||
after_update :update_stripe_price, if: :saved_change_to_amount?
|
||||
after_create :update_stripe_product
|
||||
after_update :update_stripe_product, if: :saved_change_to_base_name?
|
||||
|
||||
validates :amount, :group, :base_name, presence: true
|
||||
validates :interval_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
|
||||
@ -111,10 +110,6 @@ 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
|
||||
@ -133,4 +128,8 @@ class Plan < ApplicationRecord
|
||||
def set_name
|
||||
update_columns(name: human_readable_name)
|
||||
end
|
||||
|
||||
def update_stripe_product
|
||||
StripeWorker.perform_async(:create_or_update_stp_product, Plan.name, id)
|
||||
end
|
||||
end
|
||||
|
@ -26,6 +26,8 @@ class Space < ApplicationRecord
|
||||
|
||||
after_create :create_statistic_subtype
|
||||
after_create :create_space_prices
|
||||
after_create :update_stripe_product
|
||||
after_update :update_stripe_product, if: :saved_change_to_name?
|
||||
after_update :update_statistic_subtype, if: :saved_change_to_name?
|
||||
after_destroy :remove_statistic_subtype
|
||||
|
||||
@ -60,4 +62,10 @@ class Space < ApplicationRecord
|
||||
def destroyable?
|
||||
reservations.empty?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_stripe_product
|
||||
StripeWorker.perform_async(:create_or_update_stp_product, Space.name, id)
|
||||
end
|
||||
end
|
||||
|
@ -28,6 +28,8 @@ class Training < ApplicationRecord
|
||||
|
||||
after_create :create_statistic_subtype
|
||||
after_create :create_trainings_pricings
|
||||
after_create :update_stripe_product
|
||||
after_update :update_stripe_product, if: :saved_change_to_name?
|
||||
after_update :update_statistic_subtype, if: :saved_change_to_name?
|
||||
after_destroy :remove_statistic_subtype
|
||||
|
||||
@ -64,4 +66,8 @@ class Training < ApplicationRecord
|
||||
TrainingsPricing.create(training: self, group: group, amount: 0)
|
||||
end
|
||||
end
|
||||
|
||||
def update_stripe_product
|
||||
StripeWorker.perform_async(:create_or_update_stp_product, Training.name, id)
|
||||
end
|
||||
end
|
||||
|
@ -50,8 +50,11 @@ class PaymentScheduleService
|
||||
ps.scheduled = subscription
|
||||
ps.payment_method = payment_method
|
||||
ps.operator_profile_id = operator
|
||||
# TODO, fields: reference, wallet_amount, wallet_transaction_id, footprint, environment, invoicing_profile
|
||||
items.each do |item|
|
||||
item.payment_schedule = ps
|
||||
end
|
||||
|
||||
StripeWorker.perform_async(:create_stripe_subscription, ps.id)
|
||||
end
|
||||
end
|
||||
|
@ -45,33 +45,27 @@ class StripeWorker
|
||||
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)
|
||||
def create_or_update_stp_product(class_name, id)
|
||||
object = class_name.constantize.find(id)
|
||||
if !object.stp_product_id.nil?
|
||||
Stripe::Product.update(
|
||||
object.stp_product_id,
|
||||
{ name: object.name },
|
||||
{ api_key: Setting.get('stripe_secret_key') }
|
||||
)
|
||||
p.product
|
||||
else
|
||||
product = Stripe::Product.create(
|
||||
{
|
||||
name: object.name,
|
||||
metadata: {
|
||||
id: object.id,
|
||||
type: class_name
|
||||
}
|
||||
}, { api_key: Setting.get('stripe_secret_key') }
|
||||
)
|
||||
object.update_attributes(stp_product_id: product.id)
|
||||
end
|
||||
end
|
||||
|
||||
def create_stripe_subscription(payment_schedule_id, first_invoice_items)
|
||||
@ -79,7 +73,7 @@ class StripeWorker
|
||||
|
||||
items = []
|
||||
first_invoice_items.each do |fii|
|
||||
# TODO, fill this prices with real data
|
||||
# TODO, fill this prices with real data
|
||||
price = Stripe::Price.create({
|
||||
unit_amount: 2000,
|
||||
currency: 'eur',
|
||||
@ -91,14 +85,15 @@ class StripeWorker
|
||||
{ api_key: Setting.get('stripe_secret_key') })
|
||||
items.push(price: price[:id])
|
||||
end
|
||||
Stripe::Subscription.create({
|
||||
customer: payment_schedule.invoicing_profile.user.stp_customer_id,
|
||||
cancel_at: payment_schedule.scheduled.expiration_date,
|
||||
promotion_code: payment_schedule.coupon&.code,
|
||||
add_invoice_items: items,
|
||||
items: [
|
||||
{ price: payment_schedule.scheduled.plan.stp_price_id }
|
||||
]
|
||||
}, { api_key: Setting.get('stripe_secret_key') })
|
||||
stp_subscription = Stripe::Subscription.create({
|
||||
customer: payment_schedule.invoicing_profile.user.stp_customer_id,
|
||||
cancel_at: payment_schedule.scheduled.expiration_date,
|
||||
promotion_code: payment_schedule.coupon&.code,
|
||||
add_invoice_items: items,
|
||||
items: [
|
||||
{ price: payment_schedule.scheduled.plan.stp_price_id }
|
||||
]
|
||||
}, { api_key: Setting.get('stripe_secret_key') })
|
||||
payment_schedule.update_attributes(stp_subscription_id: stp_subscription.id)
|
||||
end
|
||||
end
|
||||
|
@ -2,23 +2,20 @@
|
||||
|
||||
class MigratePlanStats < ActiveRecord::Migration[4.2]
|
||||
def up
|
||||
index = StatisticIndex.where({es_type_key: 'subscription'}).first
|
||||
if index
|
||||
StatisticType.where({statistic_index_id: index.id}).destroy_all
|
||||
index = StatisticIndex.where(es_type_key: 'subscription').first
|
||||
return unless index
|
||||
|
||||
Plan.all.each do |p|
|
||||
p.create_statistic_type
|
||||
end
|
||||
end
|
||||
StatisticType.where(statistic_index_id: index.id).destroy_all
|
||||
Plan.all.each(&:create_statistic_type)
|
||||
end
|
||||
|
||||
def down
|
||||
index = StatisticIndex.where({es_type_key: 'subscription'}).first
|
||||
if index
|
||||
StatisticType.where({statistic_index_id: index.id}).destroy_all
|
||||
index = StatisticIndex.where(es_type_key: 'subscription').first
|
||||
return unless index
|
||||
|
||||
StatisticType.create!({statistic_index_id: index.id, key: 'month', label: 'Abonnements mensuels', graph: true, simple: true})
|
||||
StatisticType.create!({statistic_index_id: index.id, key: 'year', label: 'Abonnements annuels', graph: true, simple: true})
|
||||
end
|
||||
StatisticType.where(statistic_index_id: index.id).destroy_all
|
||||
|
||||
StatisticType.create!(statistic_index_id: index.id, key: 'month', label: 'Abonnements mensuels', graph: true, simple: true)
|
||||
StatisticType.create!(statistic_index_id: index.id, key: 'year', label: 'Abonnements annuels', graph: true, simple: true)
|
||||
end
|
||||
end
|
||||
|
@ -1,9 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Save the id of the Stripe::Price associated with the current plan.
|
||||
# This is used for payment schedules
|
||||
class AddStpPriceIdToPlan < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :plans, :stp_price_id, :string
|
||||
end
|
||||
end
|
16
db/migrate/20201112092002_add_stp_product_id_to_objects.rb
Normal file
16
db/migrate/20201112092002_add_stp_product_id_to_objects.rb
Normal file
@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Save the id of the Stripe::Product associated with the current plan or reservable object.
|
||||
# This is used for payment schedules.
|
||||
# Machines, Trainings and Spaces can be reserved jointly with a subscription that can have a
|
||||
# payment schedule, so we must associate them with Stripe::Product.
|
||||
# This is not the case for Events (we can't buy event+subscription) so we dot no associate a
|
||||
# Stripe::Product with the events.
|
||||
class AddStpProductIdToObjects < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :plans, :stp_product_id, :string
|
||||
add_column :machines, :stp_product_id, :string
|
||||
add_column :spaces, :stp_product_id, :string
|
||||
add_column :trainings, :stp_product_id, :string
|
||||
end
|
||||
end
|
@ -1166,7 +1166,8 @@ CREATE TABLE public.machines (
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone,
|
||||
slug character varying,
|
||||
disabled boolean
|
||||
disabled boolean,
|
||||
stp_product_id character varying
|
||||
);
|
||||
|
||||
|
||||
@ -1561,7 +1562,7 @@ CREATE TABLE public.plans (
|
||||
slug character varying,
|
||||
disabled boolean,
|
||||
monthly_payment boolean,
|
||||
stp_price_id character varying
|
||||
stp_product_id character varying
|
||||
);
|
||||
|
||||
|
||||
@ -2150,7 +2151,8 @@ CREATE TABLE public.spaces (
|
||||
created_at timestamp without time zone NOT NULL,
|
||||
updated_at timestamp without time zone NOT NULL,
|
||||
characteristics text,
|
||||
disabled boolean
|
||||
disabled boolean,
|
||||
stp_product_id character varying
|
||||
);
|
||||
|
||||
|
||||
@ -2680,7 +2682,8 @@ CREATE TABLE public.trainings (
|
||||
slug character varying,
|
||||
description text,
|
||||
public_page boolean DEFAULT true,
|
||||
disabled boolean
|
||||
disabled boolean,
|
||||
stp_product_id character varying
|
||||
);
|
||||
|
||||
|
||||
@ -5876,6 +5879,6 @@ INSERT INTO "schema_migrations" (version) VALUES
|
||||
('20201027092149'),
|
||||
('20201027100746'),
|
||||
('20201027101809'),
|
||||
('20201027145651');
|
||||
('20201112092002');
|
||||
|
||||
|
||||
|
@ -53,13 +53,20 @@ 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?
|
||||
|
||||
desc 'set stp_product_id to all plans/machines/trainings/spaces'
|
||||
task set_product_id: :environment do
|
||||
w = StripeWorker.new
|
||||
Plan.all.each do |p|
|
||||
w.perform(:create_stripe_price, p)
|
||||
w.perform(:create_or_update_stp_product, Plan.name, p.id)
|
||||
end
|
||||
Machine.all.each do |m|
|
||||
w.perform(:create_or_update_stp_product, Machine.name, m.id)
|
||||
end
|
||||
Training.all.each do |t|
|
||||
w.perform(:create_or_update_stp_product, Training.name, t.id)
|
||||
end
|
||||
Space.all.each do |s|
|
||||
w.perform(:create_or_update_stp_product, Space.name, s.id)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user