Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Adding creditcard payment #334

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions .sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ SINGLE_TENANT_MODE=false
S3_BUCKET_NAME="s3_bucket_name"
AWS_ACCESS_KEY_ID="aws_access_key_id"
AWS_SECRET_ACCESS_KEY="aws_secret_access_key"
SUBSCRIPTIONS_PRICE="2.00"
STRIPE_PUBLIC_KEY=pk_test_DqOJ0VeQaE9LlQjm4xi4Qig4
STRIPE_SECRET_KEY=sk_test_FhdIi5AuJfYvmjLT6xDzb28w
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gem "paperclip", "~> 4.2"
gem "aws-sdk", "< 2.0"
gem "redcarpet"
gem "holidays"
gem "stripe"

source "https://rails-assets.org" do
gem "rails-assets-chartjs"
Expand Down
15 changes: 14 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ GEM
devise (>= 3.2.0)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.24)
unf (>= 0.0.5, < 1.0.0)
dotenv (0.11.1)
dotenv-deployment (~> 0.0.2)
dotenv-deployment (0.0.2)
Expand Down Expand Up @@ -164,6 +166,8 @@ GEM
high_voltage (2.2.0)
highline (1.6.21)
holidays (1.2.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
http_accept_language (2.0.1)
i18n (0.7.0)
jquery-atwho-rails (1.0.0)
Expand Down Expand Up @@ -194,6 +198,7 @@ GEM
neat (1.5.1)
bourbon (>= 3.1)
sass (~> 3.2.19)
netrc (0.10.3)
newrelic_rpm (3.9.0.229)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
Expand Down Expand Up @@ -256,6 +261,10 @@ GEM
redcarpet (3.2.2)
responders (2.0.2)
railties (>= 4.2.0.alpha, < 5)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec-core (3.1.7)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
Expand Down Expand Up @@ -311,6 +320,9 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
stripe (1.22.0)
json (~> 1.8.1)
rest-client (~> 1.4)
temple (0.6.7)
terminal-table (1.4.5)
thor (0.19.1)
Expand Down Expand Up @@ -406,6 +418,7 @@ DEPENDENCIES
simplecov
spring
spring-commands-rspec
stripe
timecop
title
twitter-text
Expand All @@ -414,4 +427,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.10.3
1.10.5
56 changes: 56 additions & 0 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class ChargesController < ApplicationController
before_action :chargin_set?
before_action :set_subscription

def show
end

def new
end

def create

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for create is too high. [16.16/15]

token = params[:stripeToken]

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)
@subscription = Subscription.new(current_account)

render :show, success: t("payments.delete.success")
else
render :show, error: t("payments.delete.fails")
end
end

private

def set_subscription
@subscription ||= Subscription.new(current_account)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 trailing blank lines detected.

def chargin_set?
Hours.single_tenant_mode? == false &&
ENV["STRIPE_PUBLIC_KEY"] &&
ENV["STRIPE_SECRET_KEY"]
end
end
21 changes: 21 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Subscription
include ActiveModel::Model
include ActiveModel::Validations
attr_reader :number_of_users, :price, :stripe_id, :subscription_id

def initialize(account)
@stripe_id = account.stripe_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

@subscription_id = account.subscription_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

@price = ENV["SUBSCRIPTIONS_PRICE"].to_f
@number_of_users = User.count
end

def costs
enabled? ? @number_of_users * price : 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

end

def enabled?
stripe_id && subscription_id
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at class body end.

end
1 change: 1 addition & 0 deletions app/views/accounts/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.outer
.container
= link_to t("payment_link"), new_charge_path
%h1= title
.danger-zone
%h2= t("account.danger_zone")
Expand Down
3 changes: 3 additions & 0 deletions app/views/charges/create.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%h2
Thanks, you paid
%strong= number_to_currency(ENV["SUBSCRIPTIONS_PRICE"].to_f * @user_count.to_f, precision: 2, unit: "€")
52 changes: 52 additions & 0 deletions app/views/charges/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%script{:src => "https://js.stripe.com/v2/", :type => "text/javascript"}
= form_tag charges_path, method: "POST", id: "payment-form" do
%span.payment-errors
.form-row
%label
%span= t "payments.labels.card_number"
%input{"data-stripe" => "number", :size => "20", :type => "text"}/
.form-row
%label
%span= t "payments.labels.cvc"
%input{"data-stripe" => "cvc", :size => "4", :type => "text"}/
.form-row
%label
%span= t "payments.labels.expiration_date"
%input{"data-stripe" => "exp-month", :size => "2", :type => "text"}/
%span /
%input{"data-stripe" => "exp-year", :size => "4", :type => "text"}/
%button{:type => "submit"}= t "payments.pay_button"
%script{:src => "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"}
:javascript
// This identifies your website in the createToken call below
Stripe.setPublishableKey('#{ENV["STRIPE_PUBLIC_KEY"]}');
jQuery(function($) {
$('#payment-form').submit(function(event) {
var $form = $(this);

// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);

Stripe.card.createToken($form, stripeResponseHandler);

// Prevent the form from submitting with the default action
return false;
});
});

function stripeResponseHandler(status, response) {
var $form = $('#payment-form');

if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
11 changes: 11 additions & 0 deletions app/views/charges/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.outer
.container
= link_to "new charges", new_charge_path
%h2
= t("payment.show.subscription")
.current-subscription
= t("payment.current_subscription")
= number_to_currency(@subscription.costs, unit: "€")

- if @subscription.enabled?
= link_to "remove subscription", destroy_charge_path, method: "DELETE"
10 changes: 10 additions & 0 deletions config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if Hours.single_tenant_mode? == false &&
ENV["STRIPE_PUBLIC_KEY"] &&
ENV["STRIPE_SECRET_KEY"]
Rails.configuration.stripe = {
publishable_key: ENV["STRIPE_PUBLIC_KEY"],
secret_key: ENV["STRIPE_SECRET_KEY"]
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
end
13 changes: 13 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ en:
invalid_characters: contains invalid characters
last_name: Last Name
password: Password
payment_link: Payments
repeat_password: Repeat Password
subdomain: Subdomain
warning: Deleting your account will remove you and everyone else's data from %{subdomain}
Expand Down Expand Up @@ -174,6 +175,18 @@ en:
edit_user: Edit profile
entries: My Entries
no_hours_registered: Nobody spent any time on %{project} yet
payments:
pay_button: Submit Payment
labels:
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
8 changes: 8 additions & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ nl:
invalid_characters: bevat invalide karakters
last_name: Achternaam
password: Wachtwoord
payment_link: Betalen
repeat_password: Bevesting wachtwoord
subdomain: subdomein
warning: Het verwijderen van je account zal jouw en iedereens gegevens op %{subdomain}
Expand Down Expand Up @@ -231,6 +232,13 @@ nl:
edit_user: Gegevens aanpassen
entries: Mijn Uren
no_hours_registered: Niemand heeft nog tijd aan %{project} besteed
payments:
pay_button: Betalen
labels:
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
3 changes: 3 additions & 0 deletions config/routes/subdomain_present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

resources :tags, only: [:show]
resources :clients, only: [:show, :index, :edit, :update, :create]
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/20150625203904_add_stripe_id_to_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStripeIdToAccount < ActiveRecord::Migration
def change
add_column :accounts, :stripe_id, :string
end
end
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
9 changes: 6 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150224115957) 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