Reputation: 81
I am very new to programming. This is very confusing to me. I am trying to retrieve customer information from Stripe. I can retrieve the new customer id. I am trying to also retrieve the charge details. I want to ensure that it was a successful charge before my code proceeds further. Next I will be attempting webhooks to trigger events and verify stripe signatures. (I will be back with another question, or two, I am very sure!)
I have found on this site a great example that got me to the customer id. I have been playing around with the code to see what spits out so that I can incorporate that into my database update, below:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// Retrieve the request's body and parse it as JSON:
require_once('stripe-php-6.28.0/init.php');
$input = @file_get_contents('php://input');
$event_json = json_decode($input);
// Do something with $event_json
http_response_code(200); // PHP 5.4 or greater
/*Do something with object, structure will be like:
* $object->accountId
* $object->details->items[0]['contactName']
*/
// dump to file so you can see
file_put_contents('callback.test.txt', print_r($object, true));
}
//----------------------------
//For stripe data
$stripe = [
"secret_key" => "sk_test_itsasecret",
"publishable_key" => "pk_test_notpublished",
];
\Stripe\Stripe::setApiKey($stripe['secret_key']);
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$stripecustid = $customer->id;
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
'id' => $stripecustid,
'status' => $charge, //want to get some type of charge status success here
]);
\Stripe\Subscription::create([
"customer" => $customer->id,
"items" => [
[
"plan" => $1plan,
],
[
"plan" => $2plan,
],
[
"plan" => $3plan,
],
[
"plan" => $4plan,
],
[
"plan" => $5plan,
],
],
]);
$stripecustid = $customer->id;
echo "This is the stripe customer id: " . $customer->id . "<br />";
echo "customer: " . $customer->id . "<br />";
echo "stripe: " . $stripecustid . "<br />";
echo "charge: " . $charge->id . "<br />"; //returns nothing
echo "charge result: " . $charge->paid . "<br />"; //returns nothing
echo "object charge amount test result: " . $object->charge->amount; //returns nothing
echo "object charge paid test result: " . $object->charge->paid; // returns nothing
This appears to be PDO? or OOP? I am not familiar with how to approach this. Any assistance to get me jump started would be appreciated. This is quite intimidating. Thank you.
Upvotes: 1
Views: 1788
Reputation: 25622
At the moment, your code does two things: it creates a Customer and then it creates a Subscription. The second step will associated the 5 Plans with your customer so that he gets charged automatically every week or every month. This causes a charge to be created immediately for the first billing cycle (assuming no trial period).
If the charge had failed, the subscription creation would fail. You need to make sure that your code properly catches any exception raised by the library as explained in Stripe error documentation.
If no exception is raised, then the Subscription creation succeeded and the customer's card was charged successfully. This means that an Invoice was created for the expected total amount of all your plans and that Invoice caused a Charge to be created for the same reason.
Now, in most cases you likely want to store all of that information in your database: the customer id cus_1234
you got at the first step, the Subscription id sub_2222
you got at the second step and then the Invoice id in_ABCD
and the Charge id ch_FFFF
.
For the last two, the best solution is to use the List Invoices API and pass the Customer id in the customer
parameter. Since this is the first time this Customer has an invoice, you know you'll only get one result. The code would look like this:
$invoices = \Stripe\Invoice::all(["limit" => 3]);
$invoiceId = $invoices->data[0]->id;
$chargeId = $invoices->data[0]->charge;
You can also combine this with the Expand feature so that you get the full Charge object back with more information for example on the card's last 4 digits:
$invoices = \Stripe\Invoice::all([
"limit" => 3,
"expand" => ["data.charge"],
]);
$invoiceId = $invoices->data[0]->id;
$chargeId = $invoices->data[0]->charge->id;
$cardLast4 = $invoices->data[0]->charge->source->last4;
Upvotes: 3