jeudi 18 juin 2015

Stripe API implementation Undefined Method

student at working on a project here. I am building a Wiki SaaS project with Stripe integration.

Keep getting this error. I wonder, does this have to do with a poor connection on my part to the API?

Regards

Started GET "/charges/new" for ::1 at 2015-06-18 12:54:24 -0400
Processing by ChargesController#new as HTML
  Rendered charges/new.html.erb within layouts/application (2.0ms)
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `[]' for nil:NilClass):
    1: <%= form_tag charges_path do %>
    2:   <h4>Click the button!</h4>
    3:   <script class='stripe-button' src="http://ift.tt/1doUtf9" data-key="<%= @stripe_btn_data[:key] %>" data-amount=<%= @stripe_btn_data[:amount] %> data-description="<%= @stripe_btn_data[:description] %>" ></script>
    4: <% end %>
  app/views/charges/new.html.erb:3:in `block in _app_views_charges_new_html_erb___2358486347452759289_70204561671400'
  app/views/charges/new.html.erb:1:in `_app_views_charges_new_html_erb___2358486347452759289_70204561671400'

Here is my setup so far. I got my API keys from my Swipe account. I removed the keys for security.

application.yml

SENDGRID_PASSWORD: mypw
SENDGRID_USERNAME: myusr

# Stripe

PUBLISHABLE_KEY: pk_test
SECRET_KEY: sk_test

I then set with Figaro

▶ figaro heroku:set -e production
Setting config vars and restarting jonm-blocipedia... done, v10
PUBLISHABLE_KEY:   pk_test
SECRET_KEY:        sk_test
SENDGRID_PASSWORD: mypw
SENDGRID_USERNAME: myapp

I then created stripe.rb

# Store the environment variables on the Rails.configuration object
Rails.configuration.stripe = {
  publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
  secret_key: ENV['STRIPE_SECRET_KEY']
}

# Set our app-stored secret key with stripe
Stripe.api_key = Rails.configuration.stripe[:secret_key]

and created the Charges Controller

~/code/Blocipedia  stripe ✗                                                                                                                           5h48m ⚑ ◒  
▶ rails g controller Charges new
      create  app/controllers/charges_controller.rb
       route  get 'charges/new'
      invoke  erb
      create    app/views/charges
      create    app/views/charges/new.html.erb
      invoke  helper
      create    app/helpers/charges_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/charges.coffee
      invoke    scss
      create      app/assets/stylesheets/charges.scss

class ChargesController < ApplicationController
  def new
    @stripe_btn_data = {
      key: "#{ Rails.configuration.stripe[:publishable_key] }",
      description: "BigMoney Membership - #{current_user.name}",
      amount: Amount.default
    }
  end

  def create
  # Creates a Stripe Customer object, for associating
  # with the charge
  customer = Stripe::Customer.create(
    email: current_user.email,
    card: params[:stripeToken]
    )

  # Where the real magic happens
  charge = Stripe::Charge.create(
    customer: customer.id, # Note -- this is NOT the user_id in my app
    amount: Amount.default,
    description: "BigMoney Membership - #{current_user.email}",
    currency: 'usd'
    )

  flash[:success] = "Thanks for all the money, #{current_user.email}! Feel free to pay me again."
  redirect_to_ user_path(current_user) # or wherever

  # Stripe will send back CardErrors, with friendly messages
  # when something goes wrong.
  # This 'rescue block' catches and displays those errors.
  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end
end

The view/charges/new.html.erb

<%= form_tag charges_path do %>
  <h4>Click the button!</h4>
  <script class='stripe-button' src="http://ift.tt/1doUtf9" data-key="<%= @stripe_btn_data[:key] %>" data-amount=<%= @stripe_btn_data[:amount] %> data-description="<%= @stripe_btn_data[:description] %>" ></script>
<% end %>

Updated my routes.rb

 # resourceful routing to point to charges controller
  resources :charges, only: [:new, :create]

Created my amount model and migrated.

~/code/Blocipedia  stripe ✗                                                                                                                           6h14m ⚑ ◒  
▶ rails g model Amount amount:integer
      invoke  active_record
      create    db/migrate/20150618172410_create_amounts.rb
      create    app/models/amount.rb

~/code/Blocipedia  stripe ✗                                                                                                                           6h50m ⚑ ◒  
▶ rake db:migrate
== 20150618172410 CreateAmounts: migrating ====================================
-- create_table(:amounts)
   -> 0.0014s
== 20150618172410 CreateAmounts: migrated (0.0015s) ===========================

The model I made

class Amount < ActiveRecord::Base
  def self.default
    amount = 10_00
  end
end

Aucun commentaire:

Enregistrer un commentaire