I'm looking to do the following sequence (using the Stripe API) when a customer submits a credit card:
- Check if the user has a stripe customer id in their meta
- If they don't, create a new customer, save the entered card to that user
- If the user already has a customer id, check if the entered card is already one of their saved cards.
- If it is, charge that card
- If it's not, add the new card to the customer object, and then charge that card.
In my current code, Stripe is returning an invalid_request error upon trying to create the charge. Here's the section of code that relates:
//See if our user already has a customer ID, if not create one
$stripeCustID = get_user_meta($current_user->ID, 'stripeCustID', true);
if (!empty($stripeCustID)) {
$customer = \Stripe\Customer::retrieve($stripeCustID);
} else {
// Create a Customer:
$customer = \Stripe\Customer::create(array(
'email' => $current_user->user_email,
'source' => $token,
));
$stripeCustID = $customer->id;
//Add to user's meta
update_user_meta($current_user->ID, 'stripeCustID', $stripeCustID);
}
//Figure out if the user is using a stored card or a new card by comparing card fingerprints
$tokenData = \Stripe\Token::retrieve($token);
$thisCard = $tokenData['card'];
$custCards = $customer['sources']['data'];
foreach ($custCards as $card) {
if ($card['fingerprint'] == $thisCard['fingerprint']) {
$source = $thisCard['id'];
}
}
//If this card is not an existing one, we'll add it
if ($source == false) {
$newSource = $customer->sources->create(array('source' => $token));
$source=$newSource['id'];
}
// Try to authorize the card
$chargeArgs = array(
'amount' => $cartTotal,
'currency' => 'usd',
'description' => 'TPS Space Rental',
'customer' => $stripeCustID,
'source' => $source,
'capture' => false, //this pre-authorizes the card for 7 days instead of charging it immedietely
);
try {
$charge = \Stripe\Charge::create($chargeArgs);
Any help is appreciated.
$source
in$chargeArgs
. If I recall correctly, it expects a string representing a token (eg: 'tok_189fx52eZvKYlo2CrxQsq5zF'). Verify that both$source = $customer->sources->create(array('source' => $token));
and$source = $thisCard['id'];
return this – avip