Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the charge ID from Stripe using PHP?

First of all, I am aware that this question has been asked but they didn't explain how to get the information using

 charge.succeeded

I am creating an auth and capture charge using Stripe. I need the charge ID to confirm/capture the charge.

The following code creates the charge but doesn't charge until it's been approved.

\Stripe\Charge::create(array(
    "amount" => $price,
    "currency" => "usd",
    "customer" => $custID, // obtained with Stripe.js
    "description" => "Charge for [email protected]",         
    "capture" => false
));

I assumed that I could get the charge ID by doing:

$chargeID = $charge->id;

because that's how I get the customer ID when I create a customer. But I get nothing when I echo out the variable, which doesn't help since I want to save it in a database. I've looked everywhere but can't find anything concrete or recent.

Then to complete the charge. I have to capture it this way:

$ch = \Stripe\Charge::retrieve("$chargeID");
$ch->capture();

This is all from the Stripe API Reference but it doesn't explain how to get the charge ID. It doesn't even tell you how to get the customer ID, luckily Stack Overflow came through with that one.

This is all done using PHP.

like image 353
Maria Avatar asked Mar 11 '23 17:03

Maria


1 Answers

According to Stripe's Documentation, when creating a new charge you get Stripe\Charge JSON object in return (or an exception if something went wrong).

You can var_dump the object you just created to see what your object looks like, but according the the API you can use the id of the returned object:

$charge = \Stripe\Charge::create(array(
    "amount" => $price,
    "currency" => "usd",
    "customer" => $custID, // obtained with Stripe.js
    "description" => "Charge for [email protected]",         
    "capture" => false
));

// you can use the var_dump function to see what's inside the $charge object
// var_dump($charge);

$chargeID = $charge->id;

(And you don't need to call the Charge::retrieve again, you can use the current $charge object).

like image 126
Dekel Avatar answered Mar 20 '23 12:03

Dekel