Skip to content

Commit

Permalink
Moved Payments route as child of Users route
Browse files Browse the repository at this point in the history
  • Loading branch information
gorrillamcd committed Nov 10, 2012
1 parent 67652a5 commit 7ccb541
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
14 changes: 6 additions & 8 deletions app/controllers/payments_controller.rb
Expand Up @@ -21,20 +21,18 @@ def index
end

def new
@unpaid_subs = Subscription.where(:user_id => current_user.id).unpaid.includes(:course)
@unpaid_subs = current_user.subscriptions.unpaid.includes(:course)
@payment = Payment.new
end

def create
@unpaid_subs = Subscription.where(:user_id => current_user.id).unpaid.includes(:course)
@payment = Payment.new(params[:payment])
@unpaid_subs = current_user.subscriptions.unpaid.includes(:course)
@payment = current_user.payments.new(params[:payment])

# Set special attributes manually
@payment.user_id = current_user.id
@payment.email = current_user.email
# @payment.user_id = current_user.id
@payment.kind = @payment.determine_kind
@payment.amount = calculate_cost(@unpaid_subs)
if @payment.save_with_payment
@payment.amount = @unpaid_subs.calculate_cost
if @payment.charge_and_save
redirect_to dashboard_url, :notice => "Thank you for Paying!"
else
render :action => 'new'
Expand Down
3 changes: 2 additions & 1 deletion app/models/payment.rb
@@ -1,5 +1,6 @@
class Payment < ActiveRecord::Base
has_many :subscriptions
belongs_to :user

attr_accessor :stripe_card_token, :user_id, :email

Expand All @@ -23,7 +24,7 @@ def determine_kind
end


def save_with_payment
def charge_and_save
if valid? # TODO: Create a customer instead of charge for reuse of card details in future
charge = Stripe::Charge.create(amount: amount, description: "Charge for #{email}", card: stripe_card_token, currency: "usd") # TODO: require name as well for card
save!
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Expand Up @@ -6,8 +6,9 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable

# Associations
has_many :subscriptions
has_many :subscriptions, :extend => MoneyModule
has_many :courses, :through => :subscriptions, :uniq => true
has_many :payments

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Expand Up @@ -3,7 +3,9 @@
get 'users/index'

devise_for :users, :path_prefix => 'd'
resources :users
resources :users do
resources :payments
end

# To route High-Voltage pages use:
# match 'pages/home' => 'high_voltage/pages#show', :id => 'home'
Expand All @@ -27,8 +29,6 @@
resources :grades
end

resources :payments

resources :subscriptions, :only => [:create, :update]

end
5 changes: 5 additions & 0 deletions db/migrate/20121107223649_add_userid_to_payments.rb
@@ -0,0 +1,5 @@
class AddUseridToPayments < ActiveRecord::Migration
def change
add_column :payments, :user_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121027010838) do
ActiveRecord::Schema.define(:version => 20121107223649) do

create_table "answers", :force => true do |t|
t.string "text"
Expand Down Expand Up @@ -85,6 +85,7 @@
t.datetime "completed_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end

add_index "payments", ["completed_at"], :name => "index_payments_on_completed_at"
Expand Down
8 changes: 3 additions & 5 deletions lib/money_module.rb
Expand Up @@ -31,11 +31,9 @@ def to_dollars # Returns a decimal string (Dollars and cents): Returns human-fri
end
end

def calculate_cost(unpaid) # Used to return amount total in cents for payment
@amount_in_pesos = BigDecimal.new(unpaid.size) * COURSE_COST
@amount_in_cents = (@amount_in_pesos.div(EXCHANGE_RATE, 10).round(2) * 100).to_i
@amount = @amount_in_cents
@amount
def calculate_cost # Used to return amount total in cents for payment
pesos = BigDecimal.new(self.size) * COURSE_COST
cents = (pesos.div(EXCHANGE_RATE, 10).round(2) * 100).to_i
end

end

0 comments on commit 7ccb541

Please sign in to comment.