Ruby on rails 条带订阅-服务器响应状态为400

Ruby on rails 条带订阅-服务器响应状态为400,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,为什么我的条带订阅不起作用?我得到以下错误,但我无法解决该问题 当我在stripe管理面板中创建付款时,我得到了ID ref。我当前使用表中的ID与stripe中的ID匹配。我不知道如何纠正这一点 payment = customer.subscriptions.create( source: params[:stripeToken], plan: @subscription # the subscription.id is the databas

为什么我的条带订阅不起作用?我得到以下错误,但我无法解决该问题

当我在stripe管理面板中创建付款时,我得到了ID ref。我当前使用表中的ID与stripe中的ID匹配。我不知道如何纠正这一点

payment = customer.subscriptions.create(
        source: params[:stripeToken],
        plan: @subscription
        # the subscription.id is the database id
        # the plan.id in stripe uses the same id as that of the subscription.id in the database in order to select the right subsciption in stripe
      )
终端错误:

条带::InvalidRequestError此客户没有附加的付款来源: 服务器响应状态为400

在我的stripe帐户中,我有:

payments_controller.rb[在payments controller中创建操作]

def create
    @payment = Payment.new(payment_params)
    @subscription_id = @payment.subscription_id
    @event_id = @payment.event_id

    # ------- PAYMENT_SUBSCRIPTION -------
    if @subscription_id.present?
      @user = current_user
      @payment = Payment.new(payment_params)
      @subscription = @payment.subscription_id
      @payment.user_id = current_user.id

      if current_user.stripe_id?
        customer = Stripe::Customer.retrieve(current_user.stripe_id)
      else
        customer = Stripe::Customer.create(email: current_user.email)
      end

      payment = customer.subscriptions.create(
        source: params[:stripeToken],
        plan: @subscription
        # the subscription.id is the database id
        # the plan.id in stripe uses the same id as that of the subscription.id in the database in order to select the right subsciption in stripe
      )

      current_user.update(
        stripe_id: customer.id,
        stripe_subscription_pymt_id: payment.id,
        card_last4: params[:card_last4],
        card_exp_month: params[:card_exp_month],
        card_exp_year: params[:card_exp_year],
        card_type: params[:card_brand],
        recent_subscription_pymt_date: DateTime.now,
        recent_subscription_cancel_date: nil
      )


      # if payment is true/successful save the below params under payments table
      if payment.present?
        @payment.update(
          stripe_customer_id: customer.id,
          stripe_subscription_id: payment.id,
          stripe_payment_id: "none",
          subscription_payment_date: DateTime.now,
          event_payment_date: "none",
          event_payment_date_status: "none",
          user_card_type: params[:card_brand],
          user_card_last4: params[:card_last4],
          user_card_exp_month: params[:card_exp_month],
          user_card_exp_year:params[:card_exp_year],
          status: "success"
        )
      else
        @payment.update(
          stripe_customer_id: customer.id,
          stripe_subscription_id: payment.id,
          stripe_payment_id: "none",
          subscription_payment_date: DateTime.now,
          event_payment_date: "none",
          user_card_type: params[:card_brand],
          user_card_last4: params[:card_last4],
          user_card_exp_month: params[:card_exp_month],
          user_card_exp_year:params[:card_exp_year],
          status: "fail"
        )
      end

      respond_to do |format|
        if @payment.save
          MailerPaymentuserreceipt.paymentreceipt(@payment).deliver
          format.html { redirect_to account_user_path(current_user), notice: 'Your Subscription Payment was successful.' }
          format.json { render :show, status: :created, location: @payment }
        else
          format.html { redirect_to new_payment_path(subscription_id: @subscription.id), alert: 'Ensure all fields are completed'}
          format.json { render json: @payment.errors, status: :unprocessable_entity }
        end
      end

    end
  end
模式[模式中的付款表]

create_table "payments", force: :cascade do |t|
    t.string   "email"
    t.integer  "user_id"
    t.integer  "subscription_id"
    t.string   "reference"
    t.datetime "created_at",                       null: false
    t.datetime "updated_at",                       null: false
    t.integer  "event_id"
    t.string   "stripe_customer_id"
    t.string   "stripe_subscription_id"
    t.string   "stripe_payment_id"
    t.datetime "subscription_payment_date"
    t.datetime "event_payment_date"
    t.string   "user_card_type"
    t.string   "user_card_last4"
    t.integer  "user_card_exp_month"
    t.integer  "user_card_exp_year"
    t.string   "status"
    t.string   "event_payment_date_status"
    t.string   "subscription_payment_date_status"
  end
像这样试试

Stripe::Subscription.create(
  :customer => customer.id,
  :plan => @subscription
)

若要在条带上创建订阅,必须传递客户ID。虽然源是可选的,但它用于针对该客户添加支付源卡。并尝试在添加创建订阅时传递项目

例如:

Stripe::Subscription.create(
  :customer => "cus_D1nB01mz17dzfM",
  :items => [
    {
      :plan => "plan_D1keP6TJp3tjTA",
    },
  ]
)

我也有同样的问题,用你的suggestion@ARTLoe哦,你需要附上付款来源,以便客户订阅。你不能仅仅通过发送电子邮件来创造客户。
Stripe::Subscription.create(
  :customer => "cus_D1nB01mz17dzfM",
  :items => [
    {
      :plan => "plan_D1keP6TJp3tjTA",
    },
  ]
)