Reputation: 83
im trying to add a new card to an existing customer and getting an error that i dont understand why it happens. here is a sample of the code:
$strp_customer = \Stripe\Customer::retrieve($customer['stripe_id']);
echo $strp_customer->id;
echo "<br>";
echo $_POST['stripeToken'];
echo "<br>";
echo $strp_customer->default_source;
echo "<br>";
//save card to the customer in stripe
$strp_customer->source->create(array("source" => $_POST['stripeToken']));
echo $strp_customer->default_source;
the line with source create give me "Fatal error: Call to a member function create() on null"
but all echo return proper information. so the customer is retrieved and stripeToken is there. what can possibly cause that error?
Upvotes: 1
Views: 703
Reputation: 729
Another solution with card details.
try {
$customer = \Stripe\Customer::retrieve('cus_xxxxxxxxxxx');
$card = $customer->sources->create(
array(
"source" => [
"object" => "card",
"name" => 'card holder name',
"number" => 'card number',
"cvc" => 'cvc',
"exp_month" => 'exp month',// E.g: 01,02...12
"exp_year" => 'exp year' // exp year
]
)
);
echo ('Your card has been added successfully!');
} catch (Exception $exception) {
echo ('Unable to add new card, please enter valid card details.');
}
Upvotes: 0
Reputation: 25652
This issue is a typo. The code should be
$strp_customer->sources->create(array("source" => $_POST['stripeToken']));
It's sources
and not source
. You also need to make sure that you're on a recent version of the PHP library to support this call.
This will add a new card to the customer but won't set it as the default source. You're also not retrieving the customer again so you'd print the same value as before.
Upvotes: 1