Skip to content

Commit

Permalink
Stripe setup
Browse files Browse the repository at this point in the history
  • Loading branch information
builtbykasey committed May 23, 2017
1 parent de9dc8c commit 8329d77
Show file tree
Hide file tree
Showing 30 changed files with 230 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -15,3 +15,6 @@
/log/*
!/log/.keep
/tmp

# Ignore application configuration
/config/application.yml
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -22,6 +22,7 @@ source 'https://rubygems.org'
gem 'figaro'
gem 'devise'
gem 'pundit'
gem 'stripe'

group :development, :test do
gem 'byebug'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -70,6 +70,8 @@ GEM
railties (>= 3.0.0)
faker (1.7.3)
i18n (~> 0.5)
faraday (0.12.1)
multipart-post (>= 1.2, < 3)
figaro (1.1.1)
thor (~> 0.14)
globalid (0.4.0)
Expand All @@ -90,6 +92,7 @@ GEM
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.2)
multipart-post (2.0.0)
nokogiri (1.7.2)
mini_portile2 (~> 2.1.0)
orm_adapter (0.5.0)
Expand Down Expand Up @@ -179,6 +182,8 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
stripe (2.9.0)
faraday (~> 0.9)
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.7)
Expand Down Expand Up @@ -219,6 +224,7 @@ DEPENDENCIES
shoulda
spring
sqlite3
stripe
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/charges.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
8 changes: 8 additions & 0 deletions app/assets/stylesheets/application.scss
Expand Up @@ -21,3 +21,11 @@
margin-top: 9px;
padding-right: 15px;
}

.field {
padding: 5px 0;
}

.actions {
padding: 8px 0;
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/charges.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Charges controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
35 changes: 35 additions & 0 deletions app/controllers/charges_controller.rb
@@ -0,0 +1,35 @@
class ChargesController < ApplicationController

def create
@amount = 15_00

customer = Stripe::Customer.create(
email: current_user.email,
card: params[:stripeToken]
)

charge = Stripe::Charge.create(
customer: customer.id,
amount: @amount,
description: "BigMoney Membership - #{current_user.email}",
currency: 'usd'
)

flash[:notice] = "Thanks for all the money, #{current_user.email}! Feel free to pay me again."
redirect_to root_path

rescue Stripe::CardError => e
flash[:alert] = e.message
redirect_to new_charge_path
end

def new
@amount = 15_00
@stripe_btn_data = {
key: "#{ Rails.configuration.stripe[:publishable_key] }",
description: "BigMoney Membership - #{current_user.email}",
amount: @amount
}
end

end
2 changes: 2 additions & 0 deletions app/helpers/charges_helper.rb
@@ -0,0 +1,2 @@
module ChargesHelper
end
4 changes: 4 additions & 0 deletions app/models/wiki.rb
Expand Up @@ -2,4 +2,8 @@ class Wiki < ActiveRecord::Base
belongs_to :user

default_scope { order('created_at DESC') }

validates :title, length: { minimum: 2 }, presence: true
validates :body, length: { minimum: 5 }, presence: true
validates :user, presence: true
end
4 changes: 4 additions & 0 deletions app/views/charges/new.html.erb
@@ -0,0 +1,4 @@
<%= form_tag charges_path do %>
<h4>Click the button!</h4>
<script class='stripe-button' src="https://checkout.stripe.com/checkout.js" data-key="<%= @stripe_btn_data[:key] %>" data-amount="<%= @stripe_btn_data[:amount] %>" data-description="<%= @stripe_btn_data[:description] %>" ></script>
<% end %>
Expand Up @@ -13,4 +13,4 @@
</div>
<% end %>
<%= render "users/shared/links" %>
<%= render "devise/shared/links" %>
Expand Up @@ -22,4 +22,4 @@
</div>
<% end %>
<%= render "users/shared/links" %>
<%= render "devise/shared/links" %>
Expand Up @@ -13,4 +13,4 @@
</div>
<% end %>
<%= render "users/shared/links" %>
<%= render "devise/shared/links" %>
48 changes: 48 additions & 0 deletions app/views/devise/registrations/edit.html.erb
@@ -0,0 +1,48 @@
<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>

<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
<% if @minimum_password_length %>
<br />
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>
38 changes: 38 additions & 0 deletions app/views/devise/registrations/new.html.erb
@@ -0,0 +1,38 @@
<h2>Sign up</h2>

<div class="row">
<div class="col-md-4">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, class: 'form-control', placeholder: "First Name" %>
</div>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, class: 'form-control', placeholder: "Email Address", autofocus: true %>
</div>

<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password,class: 'form-control', placeholder: "Password", autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: "Confirm Password", autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
26 changes: 26 additions & 0 deletions app/views/devise/sessions/new.html.erb
@@ -0,0 +1,26 @@
<h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>

<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
File renamed without changes.
Expand Up @@ -13,4 +13,4 @@
</div>
<% end %>
<%= render "users/shared/links" %>
<%= render "devise/shared/links" %>
5 changes: 5 additions & 0 deletions app/views/users/registrations/edit.html.erb
Expand Up @@ -3,6 +3,11 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :name %><br />
<%= f.name_field :name, autofocus: true %>
</div>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/users/registrations/new.html.erb
Expand Up @@ -3,6 +3,11 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/stripe.rb
@@ -0,0 +1,6 @@
Rails.configuration.stripe = {
publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
secret_key: ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,8 @@

resources :wikis

resources :charges, only: [:new, :create]

get 'about' => 'welcome#about'

root 'welcome#index'
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/charges_controller_spec.rb
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe ChargesController, type: :controller do

end
15 changes: 15 additions & 0 deletions spec/helpers/charges_helper_spec.rb
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the ChargesHelper. For example:
#
# describe ChargesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ChargesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/models/wiki_spec.rb
Expand Up @@ -6,6 +6,13 @@

it { is_expected.to belong_to(:user) }

it { is_expected.to validate_presence_of(:title) }
it { is_expected.to validate_presence_of(:body) }
it { is_expected.to validate_presence_of(:user) }

it { is_expected.to validate_length_of(:title).is_at_least(2) }
it { is_expected.to validate_length_of(:body).is_at_least(5) }

describe "attributes" do
it "has title body and user attributes" do
expect(wiki).to have_attributes(title: "New Wiki Title", body: "New Wiki Body", user: user)
Expand Down

0 comments on commit 8329d77

Please sign in to comment.