Josh
Josh

Reputation: 589

Stripe and Rails 4 App Stripe::InvalidRequestError

I watched ryan bates rails cast on stripe integration. Everything almost went smooth until the very end. I feel like his cast is somewhat outdated and maybe stripe has updated api. I am receiving this error

Stripe::InvalidRequestError in SubscriptionsController.

It highlighted my subscription model page, specifically the customer create part

class Subscription < ActiveRecord::Base
  belongs_to :plan
  belongs_to :user
  validates_presence_of :plan_id
  validates_presence_of :email

  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
  end
end

here is my subscriptions controller

class SubscriptionsController < ApplicationController
  before_action :authenticate_user!

  def new
    plan = Plan.find(params[:plan_id])
    @subscription = plan.subscriptions.build
    @useremail = current_user.email
  end

  def create
    @subscription = current_user.build_subscription(subscription_params)
    if @subscription.save_with_payment
      redirect_to @subscription, :notice => "Thank you for subscribing!"
    else
      render :new
    end
  end

  def show
    @subscription = Subscription.find(params[:id])
  end

  private

  def subscription_params
      params.require(:subscription).permit(:plan_id, :email)
  end
end

Upvotes: 3

Views: 2343

Answers (1)

user240609
user240609

Reputation: 1060

You are not permitting the :stripe_card_token parameter in the subscription_params method in your controller.

To permit it, add :stripe_card_token to the permit list inside that method:

def subscription_params
  params.require(:subscription).permit(:plan_id, :email, :stripe_card_token)
end

Upvotes: 5

Related Questions