Skip to content

Commit

Permalink
Show and delete subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fatboypunk committed Jun 29, 2015
1 parent 88824bc commit cd78851
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 13 deletions.
17 changes: 17 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 32 additions & 9 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
class ChargesController < ApplicationController
before_action :chargin_set?
before_action :set_user_count

def show
end

def new
end

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

Expand Down
20 changes: 20 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/views/charges/show.html.haml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion config/routes/subdomain_present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20150626100838_add_subscription_id_to_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSubscriptionIdToAccount < ActiveRecord::Migration
def change
add_column :accounts, :subscription_id, :string
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit cd78851

Please sign in to comment.