Skip to content
Browse files

[FEAT] payment and API charges

  • Loading branch information...
1 parent 77a73cd commit af6d74f29add32106bc613fffad82a642bbab6fc @eladmeidar eladmeidar committed Jun 12, 2014
View
13 app/controllers/api/v1/base_controller.rb
@@ -5,6 +5,19 @@ class Api::V1::BaseController < ApplicationController
before_filter :authenticate_user_from_token!
before_filter :authenticate_user!
+ around_filter :protect_api_methods
+
+ def protect_api_methods
+ if current_user.credits <= 0
+ respond_with({error: "You are out of credits, please purchase more."}, status: :payment_required)
+ else
+ yield
+ current_user.credits_in_cents -= 1
+ current_user.save
+ response.headers["X-FAKER-IO-CREDITS-REMAINING"] = current_user.credits_in_cents.to_s
+ end
+ end
+
private
def authenticate_user_from_token!
View
2 app/controllers/application_controller.rb
@@ -1,7 +1,9 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
+
protect_from_forgery with: :exception
+
def after_sign_in_path_for(resource)
dashboard_index_path
end
View
7 app/controllers/dashboard_controller.rb
@@ -1,6 +1,9 @@
-class DashboardController < PurchasesController
+class DashboardController < ApplicationController
+
+ before_filter :authenticate_user!
+
def index
- @amount = params[:amount]
+
end
end
View
11 app/controllers/purchases_controller.rb
@@ -2,7 +2,7 @@ class PurchasesController < ApplicationController
before_filter :authenticate_user!
def create
- @amount = params[:amount]
+ @amount = params[:amount].to_i * 100
#Creates a Stripe Customer object, for associating with the purchase
customer = Stripe::Customer.create(
@@ -14,13 +14,16 @@ def create
purchase = Stripe::Charge.create(
customer: customer.id, #Note -- this is NOT the user_id in your app
amount: @amount,
- description: "Premium Memberhsip - #{current_user.email}",
+ description: "Faker.io Credits - #{current_user.email}",
currency: 'usd'
)
flash[:notice] = "Thanks for your payment, #{current_user.email}!"
+ current_user.credits_in_cents = current_user.credits_in_cents + @amount
+ current_user.save
+
redirect_to dashboard_index_path
#Stripe will send back CardErrors, with friendly messages
@@ -39,8 +42,8 @@ def new
class: 'stripe-button',
data: {
key: "#{ Rails.configuration.stripe[:publishable_key] }",
- description: "Premium Membership - #{current_user.email}",
- amount: 100
+ description: "Faker.io Credits - #{current_user.email}"#,
+# amount: 100
# We're like the Snapchat for Wikipedia. But really,
# change this amount. Stripe won't charge $9 billion.
}
View
6 app/models/user.rb
@@ -12,7 +12,11 @@ def ensure_authentication_token
self.authentication_token = generate_authentication_token
end
end
-
+
+ def credits
+ self.credits_in_cents.to_f / 100.0
+ end
+
private
def generate_uuid
View
17 app/views/dashboard/index.html.erb
@@ -2,10 +2,15 @@
<h1>Dashboard</h1>
</div>
-<div class="col-md-8">
- <ul class="list-group">
- <li class="list-group-items">
- <%= @amount %>
- </li>
- </ul>
+<div class="row">
+ <div class="well">
+ <div class="col-md-8">
+ <strong>Your remaining credits:</strong>
+ </div>
+ <div class="col-md-4">
+ <%= link_to "Buy More", new_purchase_url, class: "btn btn-lg btn-success pull-right" %>
+ <%= number_to_currency(current_user.credits) %>
+ </div>
+ <div class="clearfix"></div>
+ </div>
</div>
View
9 app/views/purchases/new.html.haml
@@ -2,7 +2,8 @@
/The form doesn't pass the amount to the create action,
/so we have to pass it manually.
- = hidden_field_tag :amount, @stripe_btn_hash[:data][:amount]
-
- %h4 Click the button!
- %script{ @stripe_btn_hash }
+ .controls
+ %label Enter the amount you wish to charge your account with
+ = text_field_tag :amount, @stripe_btn_hash[:data][:amount]
+ .controls
+ %script{ @stripe_btn_hash }
View
4 config/initializers/initializing_the_stripe.rb
@@ -1,7 +1,7 @@
# Store the environment variables on the Rails.configuration object
Rails.configuration.stripe = {
- :publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
- :secret_key => ENV['STRIPE_SECRET_KEY']
+ :publishable_key => 'pk_test_nqmHCw1SfH53pcDIo8o4CZXs',
+ :secret_key => 'sk_test_9BSKUfvoFChrKCCqczB0bxRG'
}
# Set our app-stored secret key with Stripe
View
5 config/routes.rb
@@ -12,6 +12,11 @@
end
resources :purchases, only: [:new, :create]
devise_for :users
+
+ authenticated :user do
+ root to: "dashboard#index", as: :user_dashboard
+ end
+
resources :dashboard, only: [:index]
root :to => "home#index"
end
View
5 db/migrate/20140612141920_add_credits_in_cents_to_users.rb
@@ -0,0 +1,5 @@
+class AddCreditsInCentsToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :credits_in_cents, :integer, default: 0
+ end
+end
View
3 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20140605141830) do
+ActiveRecord::Schema.define(version: 20140612141920) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
@@ -28,6 +28,7 @@
t.datetime "updated_at"
t.string "authentication_token"
t.string "uuid"
+ t.integer "credits_in_cents", default: 0
end
add_index "users", ["email"], name: "index_users_on_email", unique: true

0 comments on commit af6d74f

Please sign in to comment.
Something went wrong with that request. Please try again.