0

I'm trying to integrate Stripe’s Checkout and PHP into a simple module and I've got the following:

Stripe::setApiKey($stripe['secret_key']);

if (!empty($form_state['input'])) {
  $input = $form_state['input'];
  // Create a Customer
  $customer = Stripe_Customer::create(array(
    "card" => $input['stripeToken'],
    "email" => $input['stripeEmail'],
  ));

  // Charge the Customer instead of the card
  $charge = Stripe_Charge::create(array(
    "amount" => 4900,
    "currency" => "GBP",
    "customer" => $customer->id,
  ));
}
else {
  $data = array(
    "data-key" => $stripe['publishable_key'],
    "data-name" => variable_get("site_name", ""),
    "data-amount" => 4900,
    "data-currency" => "GBP",
  );

  $form['stripe'] = array(
    '#markup' => '<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" ' . join(' ', array_map(function($sKey) use ($data) {
      if (is_bool($data[$sKey])) {
        return $data[$sKey] ? $sKey : '';
      }
      return $sKey . '="' . $data[$sKey] . '"';
    }, array_keys($data))) . '></script>'
  );
}

Everything is working great so far apart from when I call $charge = Stripe_Charge::create I get an error returned Stripe_InvalidRequestError: You cannot use a Stripe token more than once but I don't understand how my code would be using the token more than once.

Although I get this error, in my dashboard at Stripe the payment is successfully processed, I'm using the Stripe API Client Library for PHP

1 Answer 1

0

OK I have figured it out, when the form code is running, it runs multiple times, to stop it I added a variable to $form_state so the top of my code above becomes:

Stripe::setApiKey($stripe['secret_key']);

if (!empty($form_state['input']) && !isset($form_state['processed'])) {
  $form_state['processed'] = TRUE;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.