Stripe - Cancelling a subscription

2019-05-11 03:16发布

Update: The problem is that Stripe responds with the card_token when a new customer is created rather than the customer id. Probably I am doing something wrong.

I have setup Stripe on my Rails app and then integrate it with Devise. It works fine as it creates a new customer on my Stripe account and a new user on my Rails app. When a registration takes place I save the Stripe token in my users' table as stripe_card_token (from the stripe's respond).

I want users to be able to cancel their subscription.

I use the "stripe-ruby" gem and saw that there are methods inside the gem for subscription cancellation like the "cancel_subscription" method. However I am not sure how I call this method.

I checked Stripe's API for cancelling a subscription and the example is:

customer = Stripe::Customer.retrieve({CUSTOMER_ID})
customer.subscriptions.retrieve({SUBSCRIPTION_ID}).delete()

but I don't have the customer's ID. The only attribute I save while making a customer is the token send on the Stripe's respond.

In the User.rb i create a customer with this method

  def create_a_customer
    token = self.stripe_card_token
    customer = Stripe::Customer.create(
      :card => token,
      :plan => 1,
      :email => self.email
      )         
  end

When I create the customer I save the response.id as the token:

  handleStripeResponse: (status, response) ->
if status == 200
  $('#user_stripe_card_token').val(response.id)
  $('#new_user')[0].submit()
else
  $('#stripe_error').text(response.error.message)
  $('#stripe_error').show()
  $('input[type=submit]').attr('disabled', false)

This response.id is the card's token and not customer's id...

I am confused on how to use the gem as it gives no examples, and the API asks about customer ID, instead the ID I get is the card's token. I am pretty sure I am missing something.

Any guidance, help is greatly appreciated!

0条回答
登录 后发表回答