Stripe API get customer information

David Carr

Stripe API Nova Framework Tutorials

Getting a Stripe customers information is a common task to that then making a reusable method.

I’m using Nova Framework not setup with Nova? please read Getting Stripe API setup with Nova Framework

First, check the user is logged in if not return. Next get the customer id from the database, if it's empty then return.

Attempt to get the customer from Customer::retrieve()

Lastly, check if the user has been deleted from Strip if so save its state in the database and return the customer array

protected function getCustomer()
{
    //if not logged in return
    if (!Auth::check()) {
        return null;
    }

    //assign stripe customer from db
    $stripe_customer_id = Auth::user()->stripe_customer_id;

    //no customer id
    if ($stripe_customer_id == null) {
        return null;
    }

    //get customer record from stripe
    try {
        $customer = Customer::retrieve(
            array("id" => $stripe_customer_id, "expand" => array("default_source"))
        );
    } catch (Exception $e) {
        dd('Stripe Customer not found! Error: '.$e->getMessage());
    }

    //if stripe customer has been deleted
    if ($customer->deleted == true) {

        //remove stripe link
        $user = User::find(Auth::id());
        $user->stripe_customer_id = null;
        $user->save();

        return null;
    }

    return $customer;
}

 

Fathom Analytics $10 discount on your first invoice using this link

David Carr - Laravel Developer

Hi, I’m David Carr

A Senior Developer at Vivedia
I love to use the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Laravel Livewire)

I enjoy writing tutorials and working on Open Source packages.

I also write books. I'm writing a new book Laravel Testing Cookbook, This book focuses on testing coving both PestPHP and PHPUnit.

Sponsor me on GitHub

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Laravel Testing Cookbook

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.