Reputation: 111
I'm building a web app where the customers can purchase many different plans and I'm using Stripe API for the payments. When a customer wants to buy a plan, it has to fill the credit card details and the email too. So, I get all this form data in my RegistrationController.
The thing is, I have to do many things in the post method like:
As I do have to do many steps, I decided to use a Try&Catch block and create custom Exceptions so, if something fails, I'll be able to track where the error happened. The problem is that I end with a messy method in the RegistrationController:
public function postRegistration(RegistrationRequest $request,
StripeCostumer $stripeCustomer,
StripeSubscription $stripeSubscription)
{
if ($request['training_plan'])
{
if ( ! $this->PlanExists($request['training_plan']))
{
\Log::alert('Somebody tried to hack the plan: '.
$request['email']);
return response()->json(
['error' => \Config::get('variables.104')],
Response::HTTP_NOT_FOUND);
}
}
try
{
$response = $stripeCustomer->createNewStripeCostumer($request);
$plans = $stripeSubscription->createNewStripeSubscription($response->id, $request);
$user = $this->userRepo->create($request->all());
$user->syncUserPlans($plans);
$this->userRepo->saveStripeInfo($user,$response);
}
catch(StripeCustomerNotCreated $e)
{
\Log::error('Couldn't create a new Stripe Costumer: '.
$request['email']);
return response()->json(
['error' => \Config::get('variables.106')],
Response::HTTP_PAYMENT_REQUIRED);
}
catch(StripeSubscriptionNotCreated $e)
...
catch(EloquentUserNotCreated $e)
...
catch(StripeInfoNotSaved $e)
...
event(new UserRegistration($user));
\Auth::login($user);
return redirect('/');
}
I didn't wrote every Catch block (I currently have 4-5) but everytime I throw an exception, I have to:
This is the example of a method from a service class to manage the Stripe Customers:
public function createNewStripeCustomer($request)
{
$response = Customer::create(array(
"description" => "Customer for [email protected]",
"source" => $request->stripeToken,
"email" => $request->email,
));
if(true)
{
return $response;
}
throw new StripeCustomerNotCreated();
}
*If there's any error, I return a JSON like an API.
*I have "variables.php" file in the /Config directory where I save all the error messages.
I tried to save the logic of every Exception in the Handler.php file (using a switch loop) but It doesn't work as I expect. Other option is replacing the try&catch blocks for many if&else or nested try&catch blocks but it's still messy.
What should be the best approach to make this work efficiently?
Upvotes: 3
Views: 4973
Reputation: 111
I finally got a solution handling the Stripe Exceptions (not my custom ones) in the Handler.php. I found this post that may help somebody.
Upvotes: 4