{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/cashier/issues/167","labels_url":"https://api.github.com/repos/laravel/cashier/issues/167/labels{/name}","comments_url":"https://api.github.com/repos/laravel/cashier/issues/167/comments","events_url":"https://api.github.com/repos/laravel/cashier/issues/167/events","html_url":"https://github.com/laravel/cashier/issues/167","id":79031190,"number":167,"title":"Check if coupon is valid before calling subscription->create()","user":{"login":"mikefrancis","id":1672610,"avatar_url":"https://avatars.githubusercontent.com/u/1672610?v=3","gravatar_id":"","url":"https://api.github.com/users/mikefrancis","html_url":"https://github.com/mikefrancis","followers_url":"https://api.github.com/users/mikefrancis/followers","following_url":"https://api.github.com/users/mikefrancis/following{/other_user}","gists_url":"https://api.github.com/users/mikefrancis/gists{/gist_id}","starred_url":"https://api.github.com/users/mikefrancis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikefrancis/subscriptions","organizations_url":"https://api.github.com/users/mikefrancis/orgs","repos_url":"https://api.github.com/users/mikefrancis/repos","events_url":"https://api.github.com/users/mikefrancis/events{/privacy}","received_events_url":"https://api.github.com/users/mikefrancis/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-21T14:52:54Z","updated_at":"2015-05-22T09:37:51Z","closed_at":null,"body":"If you're using Cashier to provide subscriptions on, say, a sign-up page, then there isn't a(n easy) way to check if the coupon is valid without first creating the user.\r\n\r\n    public function store()\r\n    {\r\n      $user  = App\\User::create($data);\r\n      $user->subscription('plan')->withCoupon($invalidCoupon)->create($token);\r\n      // etc.\r\n    }\r\n\r\nThis will throw the `Stripe\\Error\\InvalidRequest` exception, however your new user is already created and isn't informed as to whether their coupon code was valid or not, which doesn't seem ideal.\r\n\r\nFor the time being I'm doing:\r\n\r\n    public function store()\r\n    {\r\n      $coupon = Request::get('coupon');\r\n      if ($coupon !== '')\r\n      {\r\n        Stripe\\Stripe::setApiKey(Config::get('services.stripe.secret'));\r\n        Stripe\\Coupon::retrieve($coupon);\r\n      }\r\n      $user  = App\\User::create($data);\r\n      // etc.\r\n    }\r\n\r\nTo throw the exception and catch it in `app/Exceptions/Handler.php` before the user is created. As you can see this means you have to include a bunch of Stripe specific classes for what I'd guess people have in their Controller.\r\n\r\nIt seems like the only way around this would be to create a helper function such as `isCouponValid($coupon)`.\r\n\r\nIf there's a better way I'm all ears but this seems like being able to feed this back to the user is quite important."},"comment":{"url":"https://api.github.com/repos/laravel/cashier/issues/comments/104595467","html_url":"https://github.com/laravel/cashier/issues/167#issuecomment-104595467","issue_url":"https://api.github.com/repos/laravel/cashier/issues/167","id":104595467,"user":{"login":"mikefrancis","id":1672610,"avatar_url":"https://avatars.githubusercontent.com/u/1672610?v=3","gravatar_id":"","url":"https://api.github.com/users/mikefrancis","html_url":"https://github.com/mikefrancis","followers_url":"https://api.github.com/users/mikefrancis/followers","following_url":"https://api.github.com/users/mikefrancis/following{/other_user}","gists_url":"https://api.github.com/users/mikefrancis/gists{/gist_id}","starred_url":"https://api.github.com/users/mikefrancis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikefrancis/subscriptions","organizations_url":"https://api.github.com/users/mikefrancis/orgs","repos_url":"https://api.github.com/users/mikefrancis/repos","events_url":"https://api.github.com/users/mikefrancis/events{/privacy}","received_events_url":"https://api.github.com/users/mikefrancis/received_events","type":"User","site_admin":false},"created_at":"2015-05-22T09:37:51Z","updated_at":"2015-05-22T09:37:51Z","body":"@shabushabu This was one of the possible solutions we looked at, but dismissed because it why should the database care if a coupon code is invalid?\r\n\r\nIMHO the validation should take place at the same time (or just after) the form request validation as that's exactly what it does - perform validation which stops the request and feeds back to the user."}}