I'm implementing Stripe's custom payment form as described in the Stripe documentation (Stripe Documentation, Stripe Git Repository) and I have trouble setting the instance variable @userafter form submission.
Controller
def payment
@user = User.find(session[:user_id])
if params[:stripeToken] != nil
@token = params[:stripeToken]
if @user.update(:stripe_pmt_token => @token)
create_stripe_customer
redirect_to confirm_path
else
render 'payment'
end
end
end
def create_stripe_customer
Stripe.api_key = "<TOKEN>"
#Create a Customer
@customer = Stripe::Customer.create(
:source => @user[:stripe_pmt_token],
:description => "#{@user.first_name}"
)
@user.update(:stripe_customer_id => @customer.id)
end
Routes
get 'payment' => 'checkout#payment'
post 'payment' => 'checkout#payment'
Server Logs
Started POST "/payment" for 69.23.75.159 at 2016-03-31 11:49:01 +0000
Processing by CheckoutController#payment as HTML
Parameters: {"stripeToken"=>"tok_17v<TOKEN>"}
Can't verify CSRF token authenticity
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.2ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/checkout_controller.rb:5:in `payment'
When the payment page loads for the first time the instance variable @user = User.find(session[:user_id])gets set correctly. However, when the payment form is submitted and the code of def paymentexecutes the second time the instance variable is not set correctly.
Error message: ActiveRecord::RecordNotFound and Couldn't find User with 'id'=
Somehow rails cannot retrieve the [:user_id] value from the session, even though it is set and I am able to display it on the payment page with <%= session[:user_id] %>. Please let me know why this is the case and what I need to change. Thanks!
paymentmethod as both a GET and POST? - Robin Fisheruserand the send that token back to Stripe to create acustomerin Stripe. - mr. jman