#<Stripe::Customer:0x00007f1ac4d0b548> 的未定义方法

使用 Stripe API 提交卡详细信息后,我不断遇到这两个错误,然后应用程序应该创建一个新的订阅者并将它们重定向到库,但我却得到:

#Stripe::Customer:0x00007f1ac4d0b548 的未定义方法“订阅”

找不到 SubscriptionsController 的操作“index”

我总是先遇到第一个错误,然后当我刷新页面时,它会更改为第二个错误。

这是我的订阅控制器(/app/controllers/subscriptions_controller.rb):

    class SubscriptionsController < ApplicationController

    layout "subscribe"

    before_action :authenticate_user!, except: [:new, :create]


    def new

      if user_signed_in? && current_user.subscribed?

        redirect_to root_path, notice: "You are already a subscriber!"

      end

    end


    def create

      Stripe.api_key = Rails.application.credentials.stripe_api_key


      plan_id = params[:plan_id]

      plan = Stripe::Plan.retrieve(plan_id)

      token = params[:stripeToken]


      customer = if current_user.stripe_id?

                   Stripe::Customer.retrieve(current_user.stripe_id)

                 else

                   Stripe::Customer.create(email: current_user.email, source: token)

                 end


      subscription = customer.subscriptions.create(plan: plan.id)


      options = {

        stripe_id: customer.id,

        stripe_subscription_id: subscription.id,

        subscribed: true,

      }


      options.merge!(

        card_last4: params[:user][:card_last4],

        card_exp_month: params[:user][:card_exp_month],

        card_exp_year: params[:user][:card_exp_year],

        card_type: params[:user][:card_type]

      ) if params[:user][:card_last4]


      current_user.update(options)


      redirect_to root_path, notice: "Your subscription was set up successfully!"

    end


    def destroy

      customer = Stripe::Customer.retrieve(current_user.stripe_id)

      customer.subscriptions.retrieve(current_user.stripe_subscription_id).delete

      current_user.update(stripe_subscription_id: nil)


      redirect_to root_path, notice: "Your subscription has been cancelled."

    end

end


心有法竹
浏览 82回答 1
1回答

慕神8447489

创建订阅的方法有多种,如果我记得的话,那是一种较旧的方法,但现在您可以 - 并且应该 -像这样创建它:Stripe::Subscription.create({  customer: customer.id,  items: [    {price: plan.id},  ],})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript