Look over the development tools page, a suggestion. https://docs.stripe.com/development
Using Stripe-php can't get customer's subscription info from Stripe
I have a Laravel 6 app that uses Stripe-php and Spatie's laravel-stripe-webhooks. I cloned a copy for local dev and I have it working where I can use a Stripe-provided link to send the user to the Stripe online checkout and come back to my user's account. I'm getting a full house of 200 response on all webhooks and no errors, and in my webhook job I add the new Stripe customer ID to my user record. So far so good.
However, i need to then get subscription info for my user, mainly the subscription status, and the Stripe customer object doesn't contain any reference to the subscription. Here's a dump of the customer object:
#_values: array:22 [▼
"id" => "cus_QKuN1spKxxxxxx"
"object" => "customer"
"address" => Stripe\StripeObject {#1961 ▶}
"balance" => 0
"created" => 1719003590
"currency" => "usd"
"default_source" => null
"delinquent" => false
"description" => null
"discount" => null
"email" => "[email protected]"
"invoice_prefix" => "60482C73"
"invoice_settings" => Stripe\StripeObject {#1962 ▶}
"livemode" => false
"metadata" => Stripe\StripeObject {#1966 ▶}
"name" => "Don Beardsley"
"next_invoice_sequence" => 2
"phone" => "+12098745778"
"preferred_locales" => array:1 [▶]
"shipping" => null
"tax_exempt" => "none"
"test_clock" => null
I can view my customer record in Stripe's dashboard and see their paid subscription and all the subscription details, but how do I access that info in code? I need to check what a customer's subscription details are. A workaround would be in the webhook job to store the subscription ID in my user's record. The problem there is I want to verify the customer's subscription and status, and by storing and using the subscription ID I verify the subscription info independently of the customer. I'm afraid of my code showing the two independent datasets as hard-coded connected when Stripe for some reason decouples them, because I can't check the subscription through the customer ID.
The code that works in the original app is:
Stripe::setApiKey(env('STRIPE_API_KEY'));
if(Auth::user()->stripe_customer_id){
$cid = Auth::user()->stripe_customer_id;
$customer = Customer::retrieve($cid);
$subscription_info = $customer->subscriptions->data;
$subscription_info ? $status = $subscription_info[0]['status'] : $status = "No Subscription";
}else{
$status ="No Subscription";
}
but in my cloned app the line $subscription_info = $customer->subscriptions->data; crashes as there's no subscriptions element in the customer object.
After literally days of working on this I can't determine what the difference is between this working or not and why it works in the original app.
Fundamentally, the Stripe customer object doesn't provide subscriptions data, so how do I get from here to there? Is there a way I'm missing to get subscription info for a customer from Stripe using the customer ID?
Please or to participate in this conversation.