Stripe,取消订阅Ruby on Rails

时间:2014-05-26 08:50:34

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

我试图取消已创建的订阅。我确实传递了正确的客户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

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

1 个答案:

答案 0 :(得分:1)

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

要检索订阅:首先需要客户:

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

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

subscriptions = customer.subscriptions.data

或者使用retrieve方法和订阅的stripe_id的特定订阅(doc ref:https://stripe.com/docs/api/ruby#retrieve_subscription):

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

希望这有帮助