wontone_boys
wontone_boys

Reputation: 77

stripe subscription code not giving error when card having insufficient fund

i am facing a issue with custom stripe subscription code. My issue is like when a card having insufficient fund then stripe subscription code is not giving me any error.

My code is this.

Stripe\Charge::create ([
            "amount" => $amt * 100,
            "currency" => "usd",
            "source" => $request->stripeToken,
            "description" => $description

    ]);
        $customer = \Stripe\Customer::create(array(
            'email' => $emailid,
            'source'  => $request->stripeToken,
           // 'description' =>  $description
        ));

        $plan = \Stripe\Plan::retrieve(
            $priceid,
            []
        );

        \Stripe\Subscription::create(array(
            "customer" => $customer->id,
            "items" => array(
                array(
                    "plan" => $plan->id,
                ),
            ),
        ));

Upvotes: 0

Views: 598

Answers (3)

you should use exception

you can catch all exceptions using Throwable interface


try
{
 //your code
}catch(\Throwable $exception)
{

 $error=$exception->getError();
}

If you are going to use multiple exceptions, you can write them separately with the pipe operator at once, example below

try {

} catch (ApiErrorException|HttpException|HttpHeaderException $exception) {
    
}

Upvotes: 0

wontone_boys
wontone_boys

Reputation: 77

i change my code little bit. And atlast its working the way i want it.

 $subscription = \Stripe\Subscription::create(array( 
            "customer" => $customer->id, 
            //'default_payment_method' => $pmt_method->id,
            "items" => array( 
                array( 
                    "plan" => $plan->id, 
                ), 
            ), 
        )); 

if($subscription->status == "active"):

// process things

else:
// error return back to user if subscription payment not process
endif

Upvotes: 1

Alper
Alper

Reputation: 160

from stripe docs

if you want to catch errors, you can use try-catch block.

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

Upvotes: 0

Related Questions