为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

心有法竹 2023-11-02 19:59:13
使用 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."    endend
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

创建订阅的方法有多种,如果我记得的话,那是一种较旧的方法,但现在您可以 - 并且应该 -像这样创建它:

Stripe::Subscription.create({

  customer: customer.id,

  items: [

    {price: plan.id},

  ],

})


查看完整回答
反对 回复 2023-11-02
  • 1 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信