【问题标题】:Stripe, Canceling Subscription Ruby on RailsStripe,取消订阅 Ruby on Rails
【发布时间】:2014-05-26 21:17:41
【问题描述】:

我正在尝试取消已创建的订阅。我确实传递了正确的客户 ID 和计划 ID。但是我的终端出现错误提示

Stripe::InvalidRequestError (Customer cus_xxxxxxxxxxx does not have a subscription with ID Golden)

这是我的控制器方法。

def create
    sub_plan = params[:plan_id]
    subscription_plan_id = SubscriptionPlan.where(plan_id:sub_plan).first.id
    token = params[:stripeToken]
    email = current_user.email
    customer = Stripe::Customer.create(
    :card => token,
    :plan => sub_plan,
    :email => email
    )
    Subscription.create(subscription_plan_id:subscription_plan_id,user_id:current_user.id,stripe_customer_token:customer[:id])
    redirect_to '/membership'
  end

  def destroy
    p "---------------------------"
    subscription_plan_id = SubscriptionPlan.where(plan_id:params[:plan_id]).first.id
    customer_id = Subscription.where(subscription_plan_id:subscription_plan_id,user_id:current_user.id).first.stripe_customer_token
    customer = Stripe::Customer.retrieve(customer_id) 
    customer.subscriptions.retrieve(params[:plan_id]).delete()
    Subscription.where(subscription_plan_id:subscription_plan_id,user_id:current_user.id).first.destroy
  end

在我的创建方法中,用户创建了一个成功发生的订阅,它也出现在我的条带帐户上。 但是在试图破坏它时会发生此错误。有什么问题吗。请帮忙。提前感谢

【问题讨论】:

    标签: ruby-on-rails ruby stripe-payments


    【解决方案1】:

    无法使用计划 ID 检索条带订阅。

    要检索订阅:首先您需要一个客户:

    customer = Stripe::Customer.retrieve("cus_xxxxxxxxxx")
    

    与客户一起,您可以检索他的所有订阅:

    subscriptions = customer.subscriptions.data
    

    或具有检索方法和订阅的 stripe_id 的特定订阅(文档参考:https://stripe.com/docs/api/ruby#retrieve_subscription):

    subscription = customer.subscriptions.retrieve("sub_xxxxxxxxxx")
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 2012-01-16
      • 1970-01-01
      • 2020-05-19
      • 2017-06-16
      • 2014-09-07
      • 1970-01-01
      • 2016-10-18
      • 2021-10-24
      相关资源
      最近更新 更多