I would like to loop through all the payment methods and determine which is the default.
Getting all can be done using $_paymentMethods = $user->paymentMethods();
But there is no key showing which is default. Not sure if I'm missing something. I have to do something like:
$paymentMethods = [];
foreach($_paymentMethods as $_paymentMethod) {
$method = [
'id' => $_paymentMethod->id,
'card' => [
'last4' => $_paymentMethod->card->last4,
'exp_year' => $_paymentMethod->card->exp_year,
'exp_month' => $_paymentMethod->card->exp_month,
'brand' => $_paymentMethod->card->brand
]
];
if ($user->hasDefaultPaymentMethod()) {
$method['default'] = ( $_paymentMethod->id == $user->defaultPaymentMethod()->id );
}
array_push($paymentMethods, $method);
}
// default at the top
usort ($paymentMethods, function ($left, $right) {
return $right['default'] - $left['default'];
});
// to object
$paymentMethods = json_decode(json_encode($paymentMethods), FALSE);
This causes considerable page load time. is there a better way to determine which is the default?