首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >条带订阅-服务器响应状态为400

条带订阅-服务器响应状态为400
EN

Stack Overflow用户
提问于 2018-06-09 22:33:21
回答 2查看 855关注 0票数 0

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

当我在条带管理面板中创建付款时,我得到了ID参考。我目前使用我的表中的ID来匹配条带中的ID。我不确定如何纠正这一点。

代码语言:javascript
复制
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

代码语言:javascript
复制
2018-06-09T16:27:10.457567+00:00 app[web.1]: Completed 500 Internal Server Error in 987ms
2018-06-09T16:27:10.459118+00:00 app[web.1]: 
2018-06-09T16:27:10.459122+00:00 app[web.1]: Stripe::InvalidRequestError (This customer has no attached payment source):
2018-06-09T16:27:10.459124+00:00 app[web.1]: app/controllers/payments_controller.rb:58:in `create'
2018-06-09T16:27:10.459125+00:00 app[web.1]: 
2018-06-09T16:27:10.459127+00:00 app[web.1]: 
2018-06-09T16:27:10.774887+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.spefz.com request_id=81e62211-2807-4dd3-a9bb-ea260afe0998 fwd="37.152.39.155" dyno=web.1 connect=0ms service=2ms status=200 bytes=202 protocol=https
2018-06-09T16:27:50.921236+00:00 app[api]: Starting process with command `bin/rails console` by user richill
2018-06-09T16:28:01.326416+00:00 heroku[run.7880]: Awaiting client
2018-06-09T16:28:01.346795+00:00 heroku[run.7880]: Starting process with command `bin/rails console`
2018-06-09T16:28:01.714175+00:00 heroku[run.7880]: State changed from starting to up

在我的条带帐户中,我有以下内容:

付款控制器中的payments_controller.rb创建操作

代码语言:javascript
复制
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

架构中的模式支付表

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-11 14:43:57

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

示例:

代码语言:javascript
复制
Stripe::Subscription.create(
  :customer => "cus_D1nB01mz17dzfM",
  :items => [
    {
      :plan => "plan_D1keP6TJp3tjTA",
    },
  ]
)
票数 1
EN

Stack Overflow用户

发布于 2018-06-10 02:12:16

试着这样做

代码语言:javascript
复制
Stripe::Subscription.create(
  :customer => customer.id,
  :plan => @subscription
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50775345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档