Home > Enterprise >  Laravel 8 \Stripe\Exception\CardException when entered card declined not working
Laravel 8 \Stripe\Exception\CardException when entered card declined not working

Time:11-11

I am using Laravel Cashier but this time I am calling the StripeClient. I am trying to display the CardException errors when a user entered expired card number. But it is just returning a response of paymentMethods->create instead CardException errors.

Here is the code

try{
        $stripe = new \Stripe\StripeClient(config('app.stripe_secret'));
   
        echo $stripe->paymentMethods->create([
          'type' => 'card',
          'billing_details' => [
              'name' => '123',
              'email' => '[email protected]',
          ],
          'card' => [
            'number' => '4000000000000002',
            'exp_month' => 11,
            'exp_year' => 2023,
            'cvc' => '314',
          ],
        ]);
    }  catch(\Stripe\Exception\CardException $e){

        echo 'Status is:' . $e->getHttpStatus() . '\n';
        echo 'Type is:' . $e->getError()->type . '\n';
        echo 'Code is:' . $e->getError()->code . '\n';
        echo 'Param is:' . $e->getError()->param . '\n';
        echo 'Message is:' . $e->getError()->message . '\n';
    }

CodePudding user response:

inside try first check stripe token create or not

 $token = $stripe->tokens()->create(

                    [

                        'card' => [

                            'number'    => $request->get('card_no'),

                            'exp_month' => $request->get('ccExpiryMonth'),

                            'exp_year'  => $request->get('ccExpiryYear'),

                            'cvc'       => $request->get('cvvNumber'),

                        ],

                    ]

                );

    if (!isset($token['id'])) {

                    // Session::flash('error', 'The Stripe Token was not generated correctly');

                    // return Redirect::back();

                    $json['type'] = 'error';

                    $json['message'] = 'The Stripe Token was not generated correctly';

                    return $json;

                }
  • Related