From 6be51feeb2f105666e543ea6146263aac777fc1a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 27 Oct 2020 16:03:20 +0100 Subject: [PATCH] save stp_price_id for each plan --- app/models/plan.rb | 15 + ...01027092149_add_monthly_payment_to_plan.rb | 4 +- ...20201027145651_add_stp_price_id_to_plan.rb | 9 + db/structure.sql | 382 +++++++++--------- 4 files changed, 218 insertions(+), 192 deletions(-) create mode 100644 db/migrate/20201027145651_add_stp_price_id_to_plan.rb diff --git a/app/models/plan.rb b/app/models/plan.rb index 84696e026..673464e81 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -23,6 +23,7 @@ class Plan < ApplicationRecord after_create :create_spaces_prices after_create :create_statistic_type after_create :set_name + after_create :create_stripe_price validates :amount, :group, :base_name, presence: true validates :interval_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 } @@ -124,4 +125,18 @@ 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/db/migrate/20201027092149_add_monthly_payment_to_plan.rb b/db/migrate/20201027092149_add_monthly_payment_to_plan.rb index 3a02d0c05..b96618e45 100644 --- a/db/migrate/20201027092149_add_monthly_payment_to_plan.rb +++ b/db/migrate/20201027092149_add_monthly_payment_to_plan.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -# If a plan is marked as "monthly_payment", we can charge its subscriptions with a repayment schedule -# instead of a single invoice +# If a plan is marked as "monthly_payment", we can charge its subscriptions with a PaymentSchedule +# instead of a single Invoice class AddMonthlyPaymentToPlan < ActiveRecord::Migration[5.2] def change add_column :plans, :monthly_payment, :boolean diff --git a/db/migrate/20201027145651_add_stp_price_id_to_plan.rb b/db/migrate/20201027145651_add_stp_price_id_to_plan.rb new file mode 100644 index 000000000..ae60ea1a2 --- /dev/null +++ b/db/migrate/20201027145651_add_stp_price_id_to_plan.rb @@ -0,0 +1,9 @@ +# 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 diff --git a/db/structure.sql b/db/structure.sql index 8a93fa6d5..7712c8733 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1461,6 +1461,82 @@ CREATE SEQUENCE public.organizations_id_seq ALTER SEQUENCE public.organizations_id_seq OWNED BY public.organizations.id; +-- +-- Name: payment_schedule_items; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.payment_schedule_items ( + id bigint NOT NULL, + amount integer, + due_date timestamp without time zone, + payment_schedule_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +-- +-- Name: payment_schedule_items_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.payment_schedule_items_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: payment_schedule_items_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.payment_schedule_items_id_seq OWNED BY public.payment_schedule_items.id; + + +-- +-- Name: payment_schedules; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.payment_schedules ( + id bigint NOT NULL, + scheduled_type character varying, + scheduled_id bigint, + total integer, + stp_subscription_id character varying, + reference character varying, + payment_method character varying, + wallet_amount integer, + wallet_transaction_id bigint, + coupon_id bigint, + footprint character varying, + environment character varying, + invoicing_profile_id bigint, + operator_profile_id_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +-- +-- Name: payment_schedules_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.payment_schedules_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: payment_schedules_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.payment_schedules_id_seq OWNED BY public.payment_schedules.id; + + -- -- Name: plans; Type: TABLE; Schema: public; Owner: - -- @@ -1483,7 +1559,8 @@ CREATE TABLE public.plans ( interval_count integer DEFAULT 1, slug character varying, disabled boolean, - monthly_payment boolean + monthly_payment boolean, + stp_price_id character varying ); @@ -1883,82 +1960,6 @@ CREATE SEQUENCE public.projects_themes_id_seq ALTER SEQUENCE public.projects_themes_id_seq OWNED BY public.projects_themes.id; --- --- Name: repayment_schedule_items; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE public.repayment_schedule_items ( - id bigint NOT NULL, - amount integer, - due_date timestamp without time zone, - repayment_schedule_id bigint, - created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL -); - - --- --- Name: repayment_schedule_items_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE public.repayment_schedule_items_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: repayment_schedule_items_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE public.repayment_schedule_items_id_seq OWNED BY public.repayment_schedule_items.id; - - --- --- Name: repayment_schedules; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE public.repayment_schedules ( - id bigint NOT NULL, - scheduled_type character varying, - scheduled_id bigint, - total integer, - stp_subscription_id character varying, - reference character varying, - payment_method character varying, - wallet_amount integer, - wallet_transaction_id bigint, - coupon_id bigint, - footprint character varying, - environment character varying, - invoicing_profile_id bigint, - operator_profile_id_id bigint, - created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL -); - - --- --- Name: repayment_schedules_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE public.repayment_schedules_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: repayment_schedules_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE public.repayment_schedules_id_seq OWNED BY public.repayment_schedules.id; - - -- -- Name: reservations; Type: TABLE; Schema: public; Owner: - -- @@ -3270,6 +3271,20 @@ ALTER TABLE ONLY public.open_api_clients ALTER COLUMN id SET DEFAULT nextval('pu ALTER TABLE ONLY public.organizations ALTER COLUMN id SET DEFAULT nextval('public.organizations_id_seq'::regclass); +-- +-- Name: payment_schedule_items id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedule_items ALTER COLUMN id SET DEFAULT nextval('public.payment_schedule_items_id_seq'::regclass); + + +-- +-- Name: payment_schedules id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules ALTER COLUMN id SET DEFAULT nextval('public.payment_schedules_id_seq'::regclass); + + -- -- Name: plans id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3354,20 +3369,6 @@ ALTER TABLE ONLY public.projects_spaces ALTER COLUMN id SET DEFAULT nextval('pub ALTER TABLE ONLY public.projects_themes ALTER COLUMN id SET DEFAULT nextval('public.projects_themes_id_seq'::regclass); --- --- Name: repayment_schedule_items id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedule_items ALTER COLUMN id SET DEFAULT nextval('public.repayment_schedule_items_id_seq'::regclass); - - --- --- Name: repayment_schedules id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules ALTER COLUMN id SET DEFAULT nextval('public.repayment_schedules_id_seq'::regclass); - - -- -- Name: reservations id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3898,6 +3899,22 @@ ALTER TABLE ONLY public.organizations ADD CONSTRAINT organizations_pkey PRIMARY KEY (id); +-- +-- Name: payment_schedule_items payment_schedule_items_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedule_items + ADD CONSTRAINT payment_schedule_items_pkey PRIMARY KEY (id); + + +-- +-- Name: payment_schedules payment_schedules_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules + ADD CONSTRAINT payment_schedules_pkey PRIMARY KEY (id); + + -- -- Name: plans_availabilities plans_availabilities_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -3994,22 +4011,6 @@ ALTER TABLE ONLY public.projects_themes ADD CONSTRAINT projects_themes_pkey PRIMARY KEY (id); --- --- Name: repayment_schedule_items repayment_schedule_items_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedule_items - ADD CONSTRAINT repayment_schedule_items_pkey PRIMARY KEY (id); - - --- --- Name: repayment_schedules repayment_schedules_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules - ADD CONSTRAINT repayment_schedules_pkey PRIMARY KEY (id); - - -- -- Name: reservations reservations_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -4523,6 +4524,48 @@ CREATE INDEX index_open_api_calls_count_tracings_on_open_api_client_id ON public CREATE INDEX index_organizations_on_invoicing_profile_id ON public.organizations USING btree (invoicing_profile_id); +-- +-- Name: index_payment_schedule_items_on_payment_schedule_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedule_items_on_payment_schedule_id ON public.payment_schedule_items USING btree (payment_schedule_id); + + +-- +-- Name: index_payment_schedules_on_coupon_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedules_on_coupon_id ON public.payment_schedules USING btree (coupon_id); + + +-- +-- Name: index_payment_schedules_on_invoicing_profile_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedules_on_invoicing_profile_id ON public.payment_schedules USING btree (invoicing_profile_id); + + +-- +-- Name: index_payment_schedules_on_operator_profile_id_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedules_on_operator_profile_id_id ON public.payment_schedules USING btree (operator_profile_id_id); + + +-- +-- Name: index_payment_schedules_on_scheduled_type_and_scheduled_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedules_on_scheduled_type_and_scheduled_id ON public.payment_schedules USING btree (scheduled_type, scheduled_id); + + +-- +-- Name: index_payment_schedules_on_wallet_transaction_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_payment_schedules_on_wallet_transaction_id ON public.payment_schedules USING btree (wallet_transaction_id); + + -- -- Name: index_plans_availabilities_on_availability_id; Type: INDEX; Schema: public; Owner: - -- @@ -4656,48 +4699,6 @@ CREATE INDEX index_projects_themes_on_project_id ON public.projects_themes USING CREATE INDEX index_projects_themes_on_theme_id ON public.projects_themes USING btree (theme_id); --- --- Name: index_repayment_schedule_items_on_repayment_schedule_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedule_items_on_repayment_schedule_id ON public.repayment_schedule_items USING btree (repayment_schedule_id); - - --- --- Name: index_repayment_schedules_on_coupon_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedules_on_coupon_id ON public.repayment_schedules USING btree (coupon_id); - - --- --- Name: index_repayment_schedules_on_invoicing_profile_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedules_on_invoicing_profile_id ON public.repayment_schedules USING btree (invoicing_profile_id); - - --- --- Name: index_repayment_schedules_on_operator_profile_id_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedules_on_operator_profile_id_id ON public.repayment_schedules USING btree (operator_profile_id_id); - - --- --- Name: index_repayment_schedules_on_scheduled_type_and_scheduled_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedules_on_scheduled_type_and_scheduled_id ON public.repayment_schedules USING btree (scheduled_type, scheduled_id); - - --- --- Name: index_repayment_schedules_on_wallet_transaction_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_repayment_schedules_on_wallet_transaction_id ON public.repayment_schedules USING btree (wallet_transaction_id); - - -- -- Name: index_reservations_on_reservable_type_and_reservable_id; Type: INDEX; Schema: public; Owner: - -- @@ -5113,6 +5114,14 @@ CREATE RULE accounting_periods_upd_protect AS CREATE TRIGGER projects_search_content_trigger BEFORE INSERT OR UPDATE ON public.projects FOR EACH ROW EXECUTE PROCEDURE public.fill_search_vector_for_project(); +-- +-- Name: payment_schedules fk_rails_00308dc223; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules + ADD CONSTRAINT fk_rails_00308dc223 FOREIGN KEY (wallet_transaction_id) REFERENCES public.wallet_transactions(id); + + -- -- Name: o_auth2_mappings fk_rails_0b25d932a6; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5281,6 +5290,14 @@ ALTER TABLE ONLY public.event_price_categories ADD CONSTRAINT fk_rails_4dc2c47476 FOREIGN KEY (price_category_id) REFERENCES public.price_categories(id); +-- +-- Name: payment_schedules fk_rails_552bc65163; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules + ADD CONSTRAINT fk_rails_552bc65163 FOREIGN KEY (coupon_id) REFERENCES public.coupons(id); + + -- -- Name: tickets fk_rails_65422fe751; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5401,14 +5418,6 @@ ALTER TABLE ONLY public.projects_themes ADD CONSTRAINT fk_rails_9fd58ae797 FOREIGN KEY (project_id) REFERENCES public.projects(id); --- --- Name: repayment_schedules fk_rails_a9e6b045af; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules - ADD CONSTRAINT fk_rails_a9e6b045af FOREIGN KEY (operator_profile_id_id) REFERENCES public.invoicing_profiles(id); - - -- -- Name: projects_themes fk_rails_b021a22658; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5417,6 +5426,14 @@ ALTER TABLE ONLY public.projects_themes ADD CONSTRAINT fk_rails_b021a22658 FOREIGN KEY (theme_id) REFERENCES public.themes(id); +-- +-- Name: payment_schedules fk_rails_b38f5b39f6; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules + ADD CONSTRAINT fk_rails_b38f5b39f6 FOREIGN KEY (operator_profile_id_id) REFERENCES public.invoicing_profiles(id); + + -- -- Name: statistic_profiles fk_rails_bba64e5eb9; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5441,22 +5458,6 @@ ALTER TABLE ONLY public.projects_machines ADD CONSTRAINT fk_rails_c1427daf48 FOREIGN KEY (project_id) REFERENCES public.projects(id); --- --- Name: repayment_schedules fk_rails_c1530d5158; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules - ADD CONSTRAINT fk_rails_c1530d5158 FOREIGN KEY (invoicing_profile_id) REFERENCES public.invoicing_profiles(id); - - --- --- Name: repayment_schedules fk_rails_c313d6987a; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules - ADD CONSTRAINT fk_rails_c313d6987a FOREIGN KEY (wallet_transaction_id) REFERENCES public.wallet_transactions(id); - - -- -- Name: project_steps fk_rails_c6306005c3; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5473,14 +5474,6 @@ ALTER TABLE ONLY public.projects_components ADD CONSTRAINT fk_rails_c80c60ead3 FOREIGN KEY (project_id) REFERENCES public.projects(id); --- --- Name: repayment_schedules fk_rails_c858c7637d; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedules - ADD CONSTRAINT fk_rails_c858c7637d FOREIGN KEY (coupon_id) REFERENCES public.coupons(id); - - -- -- Name: statistic_profile_trainings fk_rails_cb689a8d3d; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5489,14 +5482,6 @@ ALTER TABLE ONLY public.statistic_profile_trainings ADD CONSTRAINT fk_rails_cb689a8d3d FOREIGN KEY (statistic_profile_id) REFERENCES public.statistic_profiles(id); --- --- Name: repayment_schedule_items fk_rails_cc00414030; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.repayment_schedule_items - ADD CONSTRAINT fk_rails_cc00414030 FOREIGN KEY (repayment_schedule_id) REFERENCES public.repayment_schedules(id); - - -- -- Name: accounting_periods fk_rails_cc9abff81f; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5521,6 +5506,14 @@ ALTER TABLE ONLY public.availability_tags ADD CONSTRAINT fk_rails_d262715d11 FOREIGN KEY (tag_id) REFERENCES public.tags(id); +-- +-- Name: payment_schedules fk_rails_d345f9b22a; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedules + ADD CONSTRAINT fk_rails_d345f9b22a FOREIGN KEY (invoicing_profile_id) REFERENCES public.invoicing_profiles(id); + + -- -- Name: slots_reservations fk_rails_d4ced1b26d; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5529,6 +5522,14 @@ ALTER TABLE ONLY public.slots_reservations ADD CONSTRAINT fk_rails_d4ced1b26d FOREIGN KEY (slot_id) REFERENCES public.slots(id); +-- +-- Name: payment_schedule_items fk_rails_d6030dd0e7; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.payment_schedule_items + ADD CONSTRAINT fk_rails_d6030dd0e7 FOREIGN KEY (payment_schedule_id) REFERENCES public.payment_schedules(id); + + -- -- Name: event_price_categories fk_rails_dcd2787d07; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -5858,6 +5859,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200721162939'), ('20201027092149'), ('20201027100746'), -('20201027101809'); +('20201027101809'), +('20201027145651');