Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nickgraffis's avatar

Redirect if customer has no cards on file

Hello,

I want to redirect the user to an add a card page if they don't have any cards on stripe.

public function showCurrentCards ($id) { $cus = Auth::user()->stripe;

    Stripe::setApiKey("XXXXX");

    $cu = Customer::Retrieve(array("id" => $cus, "expand" => array("default_source")));

    if(! empty($cu)) {
        return redirect()->route('showCard')->with('error', 'You need to add a card');
    } 

    $cards = Customer::retrieve($cus)->sources->all(array('object' => 'card'));

    return view('show-payment', ['cards' => $cards, 'cu' => $cu]);
}
0 likes
4 replies
topvillas's avatar

if(! empty($cu)) might be your problem. You're redirecting is if $cu is not empty.

1 like
eklundkristoffer's avatar
Level 1

You could try something like:

$cu = Customer::retrieve(array("id" => $cus, "expand" => array("default_source")));
$cardId = $cu->default_source;

if(! isset($cardId)) {
    return redirect()->route('showCard')->with('error', 'You need to add a card');
}

source

nickgraffis's avatar

@topvillas Thanks you. So I should be using if(empty($cu))? This seems to still not work properly. An error occurs because it is trying to display the list of cards on file, and there are none. As if it is not catching the fact that the $cu variable is empty.

Please or to participate in this conversation.