samedi 14 septembre 2019

Handle Try catch block in laravel

I have try catch block for stripe payment to handle error responses, Now I am using laravel to code.

But after catching it gives me 500 internal server error, instead of storing exception message, it catches error with 500 Internal server.?

Here is my code:

try
        {
            $customer = \Stripe\Customer::create([
                'email' => $email,
                'source' => $token,
            ]);
            //echo '<pre>'; print_r($customer); die;
            $customer_id = $customer->id;
            $subscription = \Stripe\Subscription::create([
                'customer' => $customer_id,
                'items' => [['plan' => 'plan_id']], //plan-id is static as there is single plan
            ]);

            //echo '<pre>'; print_r($subscription); die;
            $chargeJson = $subscription->jsonSerialize(); 
            // echo '<pre>';
            // print_r($chargeJson); die;
            //die;
        }
        catch(Stripe_CardError $e) {
          $error = $e->getMessage();
        } catch (Stripe_InvalidRequestError $e) {
          // Invalid parameters were supplied to Stripe's API
          $error = $e->getMessage();
        } catch (Stripe_AuthenticationError $e) {
          // Authentication with Stripe's API failed
          $error = $e->getMessage();
        } catch (Stripe_ApiConnectionError $e) {
          // Network communication with Stripe failed
          $error = $e->getMessage();
        } catch (Stripe_Error $e) {
          // Display a very generic error to the user, and maybe send
          // yourself an email
          $error = $e->getMessage();
        } catch (Exception $e) {
          // Something else happened, completely unrelated to Stripe
          $error = $e->getMessage();
        }

        $resp = array();
        if(isset($chargeJson) && !empty($chargeJson['id']))
        {
            $resp['status'] = 1;
            $resp['transactions_log'] = $chargeJson;
            $resp['current_period_end'] = $chargeJson['current_period_end'];
            $resp['transaction_id'] = $chargeJson['id']; // which is actually subcsription id
        }
        else
        {
            $resp['status'] = 0;
            $resp['error'] = $error;
        }
        return $resp;

I am not sure how to handle it and store it. I am pretty much sure I have catched the exception but I cant store it and further use it.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30k2QD8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire