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 2 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
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.4
25 changes: 25 additions & 0 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ChargesController < ApplicationController
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]

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a Customer
customer = Stripe::Customer.create(
source: token,
plan: "value",
email: current_user.email,
quantity: user_count
)
current_account.update(stripe_id: customer.id)
end

private

def user_count
@user_count ||= User.count
end
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.

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();
}
};
6 changes: 6 additions & 0 deletions config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rails.configuration.stripe = {
publishable_key: ENV["STRIPE_PUBLIC_KEY"],
secret_key: ENV["STRIPE_SECRET_KEY"]
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ 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)
project:
errors:
client_missing: If the project is billable it needs a client
Expand Down
6 changes: 6 additions & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ 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)
project:
errors:
client_missing: Als het project facturabel is moet er een klant geselecteerd worden
Expand Down
1 change: 1 addition & 0 deletions config/routes/subdomain_present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

resources :tags, only: [:show]
resources :clients, only: [:show, :index, :edit, :update, :create]
resources :charges

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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150224115957) do
ActiveRecord::Schema.define(version: 20150625203904) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -21,6 +21,7 @@
t.integer "owner_id", default: 0, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "stripe_id"
end

create_table "audits", force: :cascade do |t|
Expand Down