Rails and Stripe

I have watched the video at http://railscasts.com/episodes/288-billing-with-stripe

I have transposed subscriptions for user in the code below.

my gem file is below

source ‘https://rubygems.org

git_source(:github) do |repo_name|

repo_name = “#{repo_name}/#{repo_name}” unless repo_name.include?("/")

https://github.com/#{repo_name}.git

end

gem ‘rails’, ‘5.1.2’

gem ‘bcrypt’, ‘3.1.11’

gem ‘faker’, ‘1.7.3’

gem ‘carrierwave’, ‘1.1.0’

gem ‘mini_magick’, ‘4.7.0’

gem ‘fog-aws’, ‘2.0.0’

gem ‘nokogiri’, ‘1.8.1’

gem ‘will_paginate’, ‘3.1.6’

gem ‘bootstrap-will_paginate’, ‘1.0.0’

gem ‘bootstrap-sass’,‘3.3.7’

gem ‘puma’, ‘3.9.1’

gem ‘sass-rails’, ‘5.0.6’

gem ‘uglifier’, ‘3.2.0’

gem ‘coffee-rails’, ‘4.2.2’

gem ‘jquery-rails’, ‘4.3.1’

gem ‘turbolinks’, ‘5.0.1’

gem ‘jbuilder’, ‘2.7.0’

gem ‘stripe’, ‘3.17.0’

group :development, :test do

gem ‘sqlite3’, ‘1.3.13’

gem ‘byebug’, ‘9.0.6’, platform: :mri

end

group :development do

gem ‘web-console’, ‘3.5.1’

gem ‘listen’, ‘3.0.8’

gem ‘spring’, ‘2.0.2’

gem ‘spring-watcher-listen’, ‘2.0.1’

end

group :test do

gem ‘rails-controller-testing’, ‘1.0.2’

gem ‘minitest-reporters’, ‘1.1.14’

gem ‘guard’, ‘2.13.0’

gem ‘guard-minitest’, ‘2.4.4’

end

group :production do

gem ‘pg’, ‘0.18.4’

end

Windows does not include zoneinfo files, so bundle the tzinfo-data gem

gem ‘tzinfo-data’, platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Now I am getting this error displayed ‘Uncaught Error: rails-ujs has already been loaded!’

I am using gem ‘rails’, ‘5.1.2’ not rails 3

Cheers Dave

gem 'stripe'

config/initializers/stripe.rb

Stripe.api_key = "tGaNWsIG3Qy6zvXB8wv4rEcizJpSXjF4"
STRIPE_PUBLIC_KEY = "pk_KcSyS2qPWdT5SdrwkQg0vNSyhZgqP"

layouts/application.html.erb

<%= javascript_include_tag "https://js.stripe.com/v1/", "application" %>
<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>

subscriptions/new.html.erb

<%= f.hidden_field :stripe_card_token %>

<div class="field">
  <%= f.label :email %>
  <%= f.text_field :email %>
</div>
<% if @subscription.stripe_card_token.present? %>
  Credit card has been provided.
<% else %>
  <div class="field">
    <%= label_tag :card_number, "Credit Card Number" %>
    <%= text_field_tag :card_number, nil, name: nil %>
  </div>
  <div class="field">
    <%= label_tag :card_code, "Security Code on Card (CVV)" %>
    <%= text_field_tag :card_code, nil, name: nil %>
  </div>
  <div class="field">
    <%= label_tag :card_month, "Card Expiration" %>
    <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
  </div>
<% end %>
<div id="stripe_error">
  <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>

app/assets/javascripts/subscriptions.js.coffee

  jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
subscription =
setupForm: ->
$('#new_subscription').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
subscription.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, subscription.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#subscription_stripe_card_token').val(response.id)
$('#new_subscription')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)

controllers/subscriptions_controller.rb

def create
  @subscription = Subscription.new(params[:subscription  ])
if @subscription    .save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
  else
    render :new
  end
end

models/subscription.rb

attr_accessor :stripe_card_token

def save_with_payment
  if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self    .stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
  errors.add :base, "There was a problem with your credit card."
  false
end

bash

rails g migration add_stripe_to_subscriptions stripe_customer_token:string
rake db:migrate