{"action":"created","issue":{"url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/131","labels_url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/131/labels{/name}","comments_url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/131/comments","events_url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/131/events","html_url":"https://github.com/RailsApps/rails-stripe-membership-saas/issues/131","id":71955343,"number":131,"title":"Can't change plans or cancel account after signing up with the same email","user":{"login":"ThreeWest","id":10321887,"avatar_url":"https://avatars.githubusercontent.com/u/10321887?v=3","gravatar_id":"","url":"https://api.github.com/users/ThreeWest","html_url":"https://github.com/ThreeWest","followers_url":"https://api.github.com/users/ThreeWest/followers","following_url":"https://api.github.com/users/ThreeWest/following{/other_user}","gists_url":"https://api.github.com/users/ThreeWest/gists{/gist_id}","starred_url":"https://api.github.com/users/ThreeWest/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThreeWest/subscriptions","organizations_url":"https://api.github.com/users/ThreeWest/orgs","repos_url":"https://api.github.com/users/ThreeWest/repos","events_url":"https://api.github.com/users/ThreeWest/events{/privacy}","received_events_url":"https://api.github.com/users/ThreeWest/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-04-29T18:44:58Z","updated_at":"2015-05-11T02:01:14Z","closed_at":null,"body":"Hi, I'm runing Rails 4.2 and I'm using this app (built with the composer).\r\n\r\nI'm getting:\r\nCompleted 500 Internal Server Error in 1283ms                                                                                     \r\n                                                                                                                                  \r\nStripe::InvalidRequestError (Customer cus_69I57CUBHgGx3z does not have a subscription with ID sub_69I53Q4VAeansf):                \r\n  app/controllers/registrations_controller.rb:72:in `cancel_subscription'    \r\n\r\nThis only happens if signup with the app under one of the plans (example: monthly) and then I login to that account as a user and switch plans (example: to yearly). When I log back in to cancel the account, I get this error.  \r\n\r\nThe thing is, the customer it's complaining about is one I created (using the same email address) about 15 minutes before the one I'm trying to cancel now and that I was able to sign up , switch plans and cancel with no errrors. Also I verified the user was completely removed from the db when I cancelled. But the customer is still in stripe.\r\n\r\nNow I've created a new account with that same email address (and was given a new customer ID in stripe) I can see my plan change didn't work either for this account.\r\n\r\nSo basically if a user signs up with the same email address, it will create two accounts in stripe but only update one that could potentially (or actually) be deleted from the site db.\r\n\r\nupdate: it looks like the first subscription was not removed from the payola subscriptions db table as well. When I clicked \"cancel account\" it removed the user from the db but not the subscription on the first account. It also didn't delete the customer from stripe.  So I guess It's better to add a check to not allow someone to signup with the same email address if there is already and existing subscription with that email attached in the payola subscriptions table.  Or it would be great if there was another way to track subscriptions for a user other than email addresses (like a subscription id)."},"comment":{"url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/comments/100738215","html_url":"https://github.com/RailsApps/rails-stripe-membership-saas/issues/131#issuecomment-100738215","issue_url":"https://api.github.com/repos/RailsApps/rails-stripe-membership-saas/issues/131","id":100738215,"user":{"login":"jmschultz","id":593594,"avatar_url":"https://avatars.githubusercontent.com/u/593594?v=3","gravatar_id":"","url":"https://api.github.com/users/jmschultz","html_url":"https://github.com/jmschultz","followers_url":"https://api.github.com/users/jmschultz/followers","following_url":"https://api.github.com/users/jmschultz/following{/other_user}","gists_url":"https://api.github.com/users/jmschultz/gists{/gist_id}","starred_url":"https://api.github.com/users/jmschultz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmschultz/subscriptions","organizations_url":"https://api.github.com/users/jmschultz/orgs","repos_url":"https://api.github.com/users/jmschultz/repos","events_url":"https://api.github.com/users/jmschultz/events{/privacy}","received_events_url":"https://api.github.com/users/jmschultz/received_events","type":"User","site_admin":false},"created_at":"2015-05-11T02:01:14Z","updated_at":"2015-05-11T02:01:14Z","body":"I've solved this issue. The bug is the find_by call in RegistrationsController#cancel_subscription. \r\n\r\nThe line:\r\n`subscription = Payola::Subscription.find_by!(email: current_user.email)`\r\n\r\nNeeds to be updated to:\r\n`subscription = Payola::Subscription.find_by(email: current_user.email, state: 'active')`\r\n\r\nAlternatively, it might make more sense to delete the payola_subscription record when Customers cancel their accounts.\r\n"}}