Skip to main content

Stripe integration on Ruby On Rails

Rails • Asked by raphael

Hi Thank you for your great course:

i am coding with : https://www.lynda.com/Stripe-API-tutorials/Adding-Stripe-Payments-your-Ruby-Rails-Application/540343-2.html

then i got an error as follows:


Stripe::InvalidRequestError in SubscriptionsController#create
Received unknown parameter: source
Extracted source (around line #11):
begin
subscription = customer.subscriptions.create(
source: params[:stripeToken],
plan: params[:plan]
)


other functions are working but only "Subscription" related part is not working.
as far as i researched, it's attribute to version upgrade of Stripe from 1.5 to 2
"The subscription endpoints no longer support the source parameter."
source: https://stripe.com/docs/upgrades

Is there any way i can quickly fix this?

thank you


Hey Raphael,

Basically like they said, you'd just create the source rather than passing it in to the subscription

customer = Stripe::Customer.create
customer.sources.create(source: params[:stripeToken])
customer.subscriptions.create(plan: params[:plan])

thank you but i still get error

undefined local variable or method `subscription' for #<SubscriptionsController:0x007ff010ecd7d8> Did you mean? subscription_url
Extracted source (around line #20):

18customer.subscriptions.create(plan: params[:plan])
19
20  current_user.assign_attributes(stripe_subscription_id: subscription.id)
21      current_user.assign_attributes(
22       card_brand: params[:card_brand],
23        card_last4: params[:card_last4],

with updated code:

class SubscriptionsController < ApplicationController
  before_action :authenticate_user!

  def new
  end

  def create
    customer = current_user.stripe_customer

    begin
  #    subscription = customer.subscriptions.create(
  #     customer = Stripe::Customer.create
  #       customer.sources.create(source: params[:stripeToken])
        # customer.subscriptions.create(plan: params[:plan])
  #     )
        customer = Stripe::Customer.create
        customer.sources.create(source: params[:stripeToken])
        customer.subscriptions.create(plan: params[:plan])

You need to save the new subscription to a variable.

subscription = customer.subscriptions.create(plan: params[:plan])

thank you very much :D

this is working! wonderful :D

  def create
    begin
    customer = current_user.stripe_customer
    customer.sources.create(source: params[:stripeToken])
    subscription = customer.subscriptions.create(plan: params[:plan])

Hi Chris,

I was dealing with this same error for days!!!! and with this responses you solve my problem!!! So many thanks!!! AMAZING TUTORIALS!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.