-
Notifications
You must be signed in to change notification settings - Fork 268
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
Changes from 5 commits
6205c72
1730d5a
88824bc
cd78851
f99a26b
5ee1b55
6a47ded
31aab64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||
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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. |
||
@subscription_id = account.subscription_id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary spacing detected. |
||
end | ||
|
||
def enabled? | ||
stripe_id && subscription_id | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at class body end. |
||
end |
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: "€") |
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(); | ||
} | ||
}; |
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" |
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 |
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 |
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 |
There was a problem hiding this comment.
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]