3
votes

I'm looking to do the following sequence (using the Stripe API) when a customer submits a credit card:

  1. Check if the user has a stripe customer id in their meta
  2. If they don't, create a new customer, save the entered card to that user
  3. If the user already has a customer id, check if the entered card is already one of their saved cards.
  4. If it is, charge that card
  5. 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.

1
I would double check the $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 thisavip
Good thought. Source can also take a card ID if a customer is also provided (stripe.com/docs/api#create_charge). It looks like I was getting the card object (not the ID) in one spot, so I updated that, but still no luck.Eckstein
Ah, I see. Well, another thought: if you log into your Stripe dashboard, under API there's a tab for logs, it should contain the exact source of the issue - at least as far as the Stripe back end sees it.avip
That's certainly helpful! I didn't realize those logs existed.Eckstein
Awesome, glad it helped! :)avip

1 Answers

2
votes

The issue turned out to be this section:

if ($card['fingerprint'] == $thisCard['fingerprint']) {
    $source = $thisCard['id'];
}

If a fingerprint match is successful, I need to grab the id of the card already in the users's meta, not the matching card that was inputted. So, this works:

if ($card['fingerprint'] == $thisCard['fingerprint']) {
    $source = $card['id'];
}