Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshamann committed Jun 26, 2017
1 parent 1a38ba9 commit 03946fb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
30 changes: 22 additions & 8 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
class ChargesController < ApplicationController
layout "application"

before_action :authenticate_user!
before_action :amount_to_be_charged
layout "application"
before_action :description

def new
end

def create
customer = StripeTool.create_customer(email: params[:stripeEmail], stripe_token: params[:stripeToken])
charge = StripeTool.create_charge(customer_id: customer.id, amount: @amount, description: @description)

customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)

charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => @description,
:currency => 'usd'
)

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

def thanks

end

private
def amount_to_be_charged
current_order = Order.find(session[:order_id])
@amount = current_order.subtotal * 100
end

def description
@description = "Sample Logistics Webshop"
end
end
18 changes: 0 additions & 18 deletions app/models/concerns/stripe_tool.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/charges/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="container">
<h2>Thanks, you paid <strong><%= pretty_amount(@amount)%></strong>!</h2>
</div>
9 changes: 5 additions & 4 deletions app/views/charges/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<div class="payment">
<div class="col s3 offset-s1">
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<h5><span style="bold">Total Amount Payable: <%= pretty_amount(@amount)%></span></h5>

</article>
</div>
</div>


<div class="payment">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
<b><script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Sample Logistics Webshop"
data-description="<%= @description %>"
data-amount="<%= @amount %>"
data-email="<%= current_user.email %>"
data-locale="auto"></script>
data-locale="auto"></script></b>
<% end %>
</div>
7 changes: 3 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
#devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :charges

get 'thanks', to: 'charges#thanks', as: 'thanks'

resource :cart, only: [:show]
resources :order_items, only: [:create, :update, :destroy]

resources :charges


root "application#index"
resources :items
devise_for :users, controllers: {
Expand Down

0 comments on commit 03946fb

Please sign in to comment.