With the Stripe API, I’m trying to retrieve the default payment method of a customer via first via the subscription, then via the customer profile. Like so:
try {
$subscription = \Stripe\Subscription::retrieve($subscriptionId);
$defaultPaymentMethodId = $subscription->default_payment_method;
if (empty($defaultPaymentMethodId)) {
$customer = \Stripe\Customer::retrieve($customerId);
$defaultPaymentMethodId = $customer->invoice_settings->default_payment_method;
}
if (!empty($defaultPaymentMethodId)) {
$paymentMethod = \Stripe\PaymentMethod::retrieve($defaultPaymentMethodId);
$last4 = $paymentMethod->card->last4;
$exp_year = $paymentMethod->card->exp_year;
$exp_month = sprintf("%02d", $paymentMethod->card->exp_month);
$brand = ucwords($paymentMethod->card->brand);
$has_payment_method = true; // Set to true if a payment method is found
} else {
$last4 = $exp_year = $exp_month = $brand = null; // Set these to null to avoid potential issues
}
} catch (Exception $e) {
logError('Stripe API error trying to get payment method details: ' . $e->getMessage());
}
In both cases, I'm getting a 'null' result, even though I can see in the Stripe dashboard that the customer has, in fact, a default payment method set: (I only accept cards, so it has to be a card)
I have tried all sort of trouble shooting. API works fine for other work, I can retrieve all the other info of customers, just not the default payment method.
Any ideas what else I could try?
If you see the default
label on a Payment Method in the dashboard, that Payment Method can be stored in two places:
invoice_settings.default_payment_method
default_source
(used for some old integrations)So make sure you check these two places.