Skip to content

Commit

Permalink
paiement
Browse files Browse the repository at this point in the history
  • Loading branch information
loundness committed Aug 9, 2018
1 parent d6655b9 commit c3d9959
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Pour masquer les clés
.env
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

# Pour le paiement
gem 'stripe'

# Pour cacher les clés
gem 'dotenv-rails'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'

Expand Down Expand Up @@ -46,6 +52,8 @@ gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Pour masquer les clés
gem 'dotenv-rails'
end

group :development do
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
dotenv (2.5.0)
dotenv-rails (2.5.0)
dotenv (= 2.5.0)
railties (>= 3.2, < 6.0)
erubi (1.7.1)
execjs (2.7.0)
faraday (0.15.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -102,6 +108,7 @@ GEM
minitest (5.11.3)
msgpack (1.2.4)
multi_json (1.13.1)
multipart-post (2.0.0)
nio4r (2.3.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
Expand Down Expand Up @@ -168,6 +175,8 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
stripe (3.21.0)
faraday (~> 0.10)
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
Expand Down Expand Up @@ -198,6 +207,7 @@ DEPENDENCIES
capybara (>= 2.15)
chromedriver-helper
coffee-rails (~> 4.2)
dotenv-rails
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
Expand All @@ -208,6 +218,7 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
stripe
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/charges.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/charges.scss
Original file line number Diff line number Diff line change
@@ -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/
27 changes: 27 additions & 0 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ChargesController < ApplicationController
def new
end

def create
# Amount in cents
@amount = 500

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

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

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


end
2 changes: 2 additions & 0 deletions app/helpers/charges_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ChargesHelper
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>Merci pour le paiement de <strong>€5.00</strong>!</h2>
19 changes: 19 additions & 0 deletions app/views/charges/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: €5.00</span>
</label>
</article>

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-currency="eur"
data-locale="auto"></script>
<% end %>
6 changes: 6 additions & 0 deletions config/initializers/stripes.rb
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :charges, only: [:new, :create]
post '/charges', to: 'charges#create'
get '/charges/new', to: 'charges#new'
end
7 changes: 7 additions & 0 deletions test/controllers/charges_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ChargesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit c3d9959

Please sign in to comment.