Reputation: 11
I'm not doing very well implementing the Stripe API for some reason. I have been working with creating customers and charges fine, but when I tried to add a new card to an existing customer I just cannot get it to work.
Here is the basic implementation from the API (simple enough) but it keeps providing errors:
$customer = Stripe_Customer::retrieve($stripe_customer_id);
$customer->sources->create(array("card" => $token));
https://stripe.com/docs/api#create_card
Error Log:
PHP Fatal error: Uncaught exception 'Stripe_InvalidRequestError' with message 'Missing required param: source' in / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php:142
Stack trace:
0 / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php(254): Stripe_ApiRequestor->handleApiError('{\n "error": {\n...', 400, Array)
1 / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php(104): Stripe_ApiRequestor->_interpretResponse('{\n "error": {\n...', 400)
2 / ... /scripts/stripe/stripe_library/lib/Stripe/List.php(19): Stripe_ApiRequestor->request('post', '/v1/customers/c...', Array)
3 / ... /join/update-card-stripe.php(34): Stripe_List->create(Array)
4 {main} thrown in / ... /scripts/stripe/stripe_library/lib/Stripe/ApiRequestor.php on line 142
Any help would be appreciated. Thanks!
Upvotes: 1
Views: 2344
Reputation: 381
I added a new card to the existing customer using the following code:
$customer_stripe_data = \Stripe\Customer::retrieve('customer stripe id');
$createdCard = $customer_stripe_data->sources->create(array("source" => $this->input->post('stripe_token')));
$customer_stripe_data->save();
I got the $this->input->post('stripe_token')
by including stripe js file in my html file.
Upvotes: 1
Reputation: 11212
I've created by customer and attempted to register the card, using the source token
// create a new customer if our current user doesn't have one
$stripe_customer = \Stripe\Customer::create(array(
'source' => $token,
'description' => $displayname,
'metadata' => array('BHAA_ID'=>$bhaa_id),
'email' => $email
)
);
$stripe_customer_id=$stripe_customer->id;
update_user_meta( $bhaa_id, 'stripe_customer_id', $stripe_customer_id );
error_log('$stripe_customer_id '.$stripe_customer_id);
// http://stackoverflow.com/questions/29393338/php-issue-with-stripe-api-create-card
$customer = \Stripe\Customer::retrieve($stripe_customer_id);
$card = $customer->sources->create(array("source" => $token));
error_log('card '.$card->id);
but i get the same error
[16-Dec-2015 18:54:12 UTC] PHP Fatal error: Uncaught exception 'Stripe\Error\InvalidRequest' with message 'Missing required param: source.' in /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php:103 from API request 'req_7Xq54M2AlKDijE'
Stack trace:
#0 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php(217): Stripe\ApiRequestor->handleApiError('{? "error": {?...', 400, Array, Array)
#1 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php(60): Stripe\ApiRequestor->_interpretResponse('{? "error": {?...', 400, Array)
#2 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiResource.php(108): Stripe\ApiRequestor->request('post', '/v1/customers/c...', Array, Array)
#3 /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiResource.php(99): Stripe\Ap in /var/www/wp-content/plugins/events-manager-pro-stripe-token-gateway-master/vendor/stripe/stripe-php/lib/ApiRequestor.php on line 103
I suspect the issue may be in the Stripe PHP API
Upvotes: 0
Reputation: 3257
I think stripes example documentation is out of date or incorrect. Looking at the docs under "Definition" it should be:
$customer->sources->create(array("source" => $token));
Under "Example request" it does indeed have it exactly as you are doing it, but looking at the arguments documented to the left, it should actually be "source" not "card"
https://stripe.com/docs/api/php#create_card
Upvotes: 1