Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Joe Bruno
Joe Bruno
35,909 Points

Stripe and Handling 503 Error

Hello, I am working on a Stripe plugin for WordPress, and I am attempting to handle a possible exception. The plugin stores the Customer's Stripe customer ID in the users meta data. Its possible that a customer may have a Stripe Customer ID stored, but that Stripe Customer ID does not exists - such as if the admin deleted the data from their Stripe Account or switched to another Stripe account. The code is below with my problem stated in the comments. Thanks in advance!

    //Get Stripe Customer ID Stored In Wordpress User Meta
        $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true );
            error_log("Stripe customer id initially reads as:" . $customer_id);

        if ($customer_id) {

            error_log("A Stripe Customer ID was found in user meta:" . $customer_id);

            //Get Customer From Stripe

//Need help here. I have set up the conditions so that a 503 //error occurs because Stripe is not able to retrieve the //Customer - because the Customer does not exist; however, I //need help handling the error. The program simply stops if it is //unable to retrieve the customer instead of continuing down //to the elseif statement below.

            $customer = Stripe_Customer::retrieve( $customer_id );

            $customer_id = $customer->id; 
                error_log("Retreived Stripe Customer id:" . $customer_id);

            //Check whether Customer was deleted from Stripe
            $customer_deleted = $customer->deleted;
                error_log("Was the customer deleted?" . $customer_deleted);

            if ($customer_deleted !== true) {

                //Get card count from Stripe.////??/
                $customer_card_count = $customer->cards->count;
                    error_log("Number of cards on file in Stripe DB is = " . $customer_card_count);

                //If no Cards, Create One
                if($customer_card_count == 0) {

                    $new_card = $customer->cards->create(array("card" => $card));
                    error_log("Looks like there were no cards on file so we created one:" . $new_card);

        elseif (!$customer_id || $customer_id = '' || $customer_deleted == true) {