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

时间:2018-06-09 14:33:21

标签: ruby-on-rails stripe-payments

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

当我在条带管理面板中创建付款时,我获得了ID和ID。我目前使用表格中的ID来匹配条带中的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
      )
终端

错误

  

Stripe :: InvalidRequestError(此客户没有附加的付款来源):   服务器响应状态400

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

enter image description here

在我的条带帐户中我有这个:

enter image description here

payments_controller.rb [在支付控制器中创建操作]

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

2 个答案:

答案 0 :(得分:1)

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

示例:

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

答案 1 :(得分:0)

试试这个

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