From cd788511d79db2f2960d6f3ac1115be413a9a67a Mon Sep 17 00:00:00 2001 From: Marcel Horlings Date: Mon, 29 Jun 2015 12:52:50 +0200 Subject: [PATCH] Show and delete subscriptions --- .rubocop.yml | 17 ++++++++ app/controllers/charges_controller.rb | 41 +++++++++++++++---- app/models/subscription.rb | 20 +++++++++ app/views/charges/show.html.haml | 9 ++++ config/locales/en.yml | 6 +++ config/locales/nl.yml | 1 + config/routes/subdomain_present.rb | 4 +- ...26100838_add_subscription_id_to_account.rb | 5 +++ db/schema.rb | 8 ++-- 9 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 .rubocop.yml create mode 100644 app/models/subscription.rb create mode 100644 app/views/charges/show.html.haml create mode 100644 db/migrate/20150626100838_add_subscription_id_to_account.rb diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..9c315123 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,17 @@ +AllCops: + Exclude: + - "vendor/**/*" + - "db/**/*" +Style/StringLiterals: + EnforcedStyle: double_quotes + Enabled: true +Style/FileName: + Enabled: false +Metrics/AbcSize: + Description: A calculated magnitude based on number of assignments, branches, and + conditions. + Enabled: true + Max: 15 +Style/DotPosition: + EnforcedStyle: trailing + Enabled: true diff --git a/app/controllers/charges_controller.rb b/app/controllers/charges_controller.rb index d38e246c..d2a6856b 100644 --- a/app/controllers/charges_controller.rb +++ b/app/controllers/charges_controller.rb @@ -1,5 +1,9 @@ class ChargesController < ApplicationController before_action :chargin_set? + before_action :set_user_count + + def show + end def new end @@ -7,19 +11,38 @@ def new def create token = params[:stripeToken] - customer = Stripe::Customer.create( - source: token, - plan: "value", - email: current_user.email, - quantity: user_count - ) - pp customer.instance_methods(false) - current_account.update(stripe_id: customer.id) + if token + customer = Stripe::Customer.create( + source: token, + plan: "value", + email: current_user.email, + quantity: @user_count + ) + + current_account.update( + stripe_id: customer.id, + subscription_id: customer.subscriptions.data[0].id + ) + else + render :new, notice: t("payments.went_wrong_message") + end + end + + def destroy + cu = Stripe::Customer.retrieve(current_account.stripe_id) + + if cu.delete.deleted + current_account.update(stripe_id: nil, + subscription_id: nil) + render :show, success: t("payments.delete.success") + else + render :show, error: t("payments.delete.fails") + end end private - def user_count + def set_user_count @user_count ||= User.count end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 00000000..61e6ee5f --- /dev/null +++ b/app/models/subscription.rb @@ -0,0 +1,20 @@ +class Subscription + + def initialize + account ||= Account.find_by(subdomain: Apartment::tennant.current) + + @stripe_id = account.stripe_id + @subscription_id = account.subscription_id + @price = ENV["SUBSCRIPTIONS_PRICE"].to_f + @number_of_users = User.count + def + + def costs + @number_of_users * price + end + + def enabled? + stripe_id && subscription_id + end + +end diff --git a/app/views/charges/show.html.haml b/app/views/charges/show.html.haml new file mode 100644 index 00000000..81058a96 --- /dev/null +++ b/app/views/charges/show.html.haml @@ -0,0 +1,9 @@ +.outer + .container + %h2 + = t("payment.show.subscription") + .current-subscription + = t("payment.current_subscription") + = number_to_currency(@user_count * ENV["SUBSCRIPTIONS_PRICE"].to_f ) + + = link_to "remove subscription", destroy_charge_path, method: "DELETE" diff --git a/config/locales/en.yml b/config/locales/en.yml index a0820e0b..c35100d5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -181,6 +181,12 @@ en: card_number: Card Number cvc: CVC expiration_date: Expiration (MM/YYYY) + went_wrong_message: Something went wrong + edit: + header: Subscription + delete: + success: Subscription successful deleted + fail: Could not delete you're subscription project: errors: client_missing: If the project is billable it needs a client diff --git a/config/locales/nl.yml b/config/locales/nl.yml index cae783f8..ab9de1c9 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -238,6 +238,7 @@ nl: card_number: Creditcard nummer cvc: CVC expiration_date: Einddatum (MM/YYYY) + went_wrong_message: Er ging iets mis project: errors: client_missing: Als het project facturabel is moet er een klant geselecteerd worden diff --git a/config/routes/subdomain_present.rb b/config/routes/subdomain_present.rb index c2403eda..de8aa20c 100644 --- a/config/routes/subdomain_present.rb +++ b/config/routes/subdomain_present.rb @@ -20,7 +20,9 @@ resources :tags, only: [:show] resources :clients, only: [:show, :index, :edit, :update, :create] -resources :charges +resources :charges, only: [:new, :create] +delete "charges" => "charges#destroy", as: :destroy_charge +get "charges" => "charges#show", as: :show_charge get "user/edit" => "users#edit", as: :edit_user get "account/edit" => "accounts#edit", as: :edit_account diff --git a/db/migrate/20150626100838_add_subscription_id_to_account.rb b/db/migrate/20150626100838_add_subscription_id_to_account.rb new file mode 100644 index 00000000..f64214fe --- /dev/null +++ b/db/migrate/20150626100838_add_subscription_id_to_account.rb @@ -0,0 +1,5 @@ +class AddSubscriptionIdToAccount < ActiveRecord::Migration + def change + add_column :accounts, :subscription_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 245e5fb8..0e2ce9b4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,17 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150625203904) do +ActiveRecord::Schema.define(version: 20150626100838) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + enable_extension "hstore" create_table "accounts", force: :cascade do |t| - t.string "subdomain", default: "", null: false - t.integer "owner_id", default: 0, null: false + t.string "subdomain", default: "", null: false + t.integer "owner_id", default: 0, null: false t.datetime "created_at" t.datetime "updated_at" t.string "stripe_id" + t.string "subscription_id" end create_table "audits", force: :cascade do |t|