DC-9
DC-9

Reputation: 11

How Do I Access The API Response From Stripe, When Creating A Customer

I am a total newbie. I understand some PHP. I have been able to create a customer in Stripe.
I need to save the customer id (cus_123..) in my database.

The code I used to successfully create a non paying customer in Stripe:

include ('config.php'); // this will fetch my secret keys

$fullname = 'James Doe'; // passing the name variable
$email = '[email protected]'; // passing the email variable

// create Stripe Customer                   
\Stripe\Customer::create([
    'description' => 'Guest',
    'name'=> $fullname,
    'email'=> $email
]);

Upvotes: 0

Views: 1065

Answers (2)

DC-9
DC-9

Reputation: 11

Thank you Darren, I spent several days looking for a way to do this. The final code below echoed the customer id.

                $fullname = 'James Doe';
        $email = '[email protected]';

try{    // create Stripe Customer                   
        $customer = \Stripe\Customer::create([
        'description' => 'Guest',
        'name'=> $fullname,
        'email'=> $email
        ]);

        echo $customer -> id;
        }

        catch (Exception $e)
            { print_r($e); };

Upvotes: 1

Darren
Darren

Reputation: 13128

As noted in the Stripe API Documention: Customer Creation, you can access the data in the return response from your API call.

$customer = \Stripe\Customer::create([
    'description' => 'Guest',
    'name'=> $fullname,
    'email'=> $email
]);
// Customer ID
echo $customer->id;

You should wrap this call in a try {} catch( .. ){ } call as it may throw an error, as-per documentation.

Upvotes: 1

Related Questions