April 24th
Covering:
Stripe
- Going to use PostgreSQL for the database this time
- Download Postgres.app for GUI interface
gem install pgin terminal will install postgres- ((
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_configin terminal will install pg gem if above doesn’t work)) rails new sneakers_pg_stripe --database=postgresql- Add Devise to Gemfile and Rspec to
:development, :testgroup in Gemfile - Bundle, then
rails g rspec:install && rails g devise:install - Change development database name to
sneakers_developmentand test database name tosneakers_testindatabase.yml(config folder) - Change production database name to
sneakersand change username to your name rails db:create– postgres is different to sqlite and actually requires you to create the database firstrails g scaffold Sneaker name description:text price:decimalrails db:migrate- Run
rails sto test database connection - When referencing other tables, make sure the other table is already created before referencing otherwise postgres will error out
- Fill seed file with shoes with name, description, price then
Sneaker.create!(sneakers) { |s| p s.name } rails db:seedrails g devise Usergem 'stripe'in Gemfile andgem 'dotenv-rails', '~> 2.4'to:development, :testgroup in Gemfile- Bundle
- Create
.env.developmentfile and add.env and .env*to.gitignorefile - Add to
.env.developmentfile: (include your own keys from Stripe website)PUBLISHABLE_KEY=key SECRET_KEY=key rails g controller Charges- Follow Stripe guide to set up form, views and controller
- Add
charges_controller.rbdetails tosneaker_controller.rb before_action :set_sneaker, only: [:show, :edit, :update, :destroy, :charge]at top ofsneaker_controller.rbto allow charge method access toset_sneaker- Add to routes:
resources :sneakers do member do post 'charge' end end rails g migration AddStripeIDToUser stripe_id- Migrate
before_action :authenticate_user!, only: [:charge]in top ofsneakers_controller.rb- Put the form into a partial and render it inside
show.html.erb - Add
<%= stylesheet_link_tag "https://checkout.stripe.com/v3/checkout/button.css"%>in application layout - Add inside
show.html.erb:<% if user_signed_in? %> <%= render 'partials/charge_form', sneaker: @sneaker, user: current_user %> <% else %> <%= link_to new_user_session_path, class: 'stripe-button-el' do%> <span style="display: block; min-height: 30px;">Pay with Card</span> <% end %> <% end %> - Look in to using Devise to redirect back after signing in
- Add
data-amount="<%= user.email %>"in the charge form partial - In
sneakers_controller.rbadd:def charge if current_user.stripe_id.nil? customer = Stripe::Customer.create( email: params[:stripeEmail], source: params[:stripeToken] ) current_user.stripe_id = customer.id current_user.save! end charge = Stripe::Charge.create( customer: current_user.stripe_id, amount: @sneaker.price, description: @sneaker.description, currency: 'aud' ) current_user.charges << Charge.new(charge_id: charge.id) flash[:notice] = "Payment Made!" redirect_back fallback_location sneakers_path rescue Stripe::CardError => e flash[:error] = e.message redirect_to new_charge_path end
Deploying to Heroku
- Navigate to project folder
heroku loginwith your Heroku account details- Ensure
gem pgis in your Gemfile (runbundle installif you have to add it) git initgit add .git commit -m "First commit"git statusto ensure all files are up to dateheroku createwill create a remote app on Herokugit push heroku masterto push project to Heroku- Check for any errors!
heroku run rails db:migrateto create database on Herokuheroku runwill allow you to run terminal commands on the Heroku appheroku run rails db:seedif you need to seed the database