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 1 commit
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
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
17 changes: 17 additions & 0 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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,

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Jup shouldn't copy paste I know

Copy link
Member

Choose a reason for hiding this comment

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

Haha is stripe using using the rocket hash in their docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that allows projects with older ruby to use stripe ad well I guess

:plan => "value",

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.

:email => current_user.email,

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.

:quantity => User.count

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.

)
end
end
1 change: 1 addition & 0 deletions app/views/charges/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>Thanks, you paid <strong>$5.00</strong>!</h2>
65 changes: 65 additions & 0 deletions app/views/charges/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<%= form_tag charges_path, method: "POST", id: "payment-form" do %>
<span class="payment-errors"></span>

<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number"/>
</label>
</div>

<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc"/>
</label>
</div>

<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" size="2" data-stripe="exp-month"/>
</label>
<span> / </span>
<input type="text" size="4" data-stripe="exp-year"/>
</div>

<button type="submit">Submit Payment</button>
<% end %>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/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();
}
};
</script>
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'],

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

:secret_key => ENV['STRIPE_SECRET_KEY']

Choose a reason for hiding this comment

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

Unnecessary spacing detected.
Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
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 spec/controllers/charges_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


RSpec.describe ChargesController, :type => :controller do

Choose a reason for hiding this comment

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

Use the new Ruby 1.9 hash syntax.


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 block body beginning.

end