1

I have a page where a user enters a cc and get's charged.

I create a card token using js

Stripe.card.createToken(ccData, function stripeResponseHandler(status, response) { 
    var token = response.id;

    // add the cc info to the user using
    // charge the cc for an amount
});

To add the cc I'm using php

$stripeResp = Stripe_Customer::retrieve($stripeUserId);
$stripeResp->sources->create(['source' => $cardToken]);

To charge the cc I'm using php as well

$stripeCharge = Stripe_Charge::create([
    'source'      => $token,
    'amount'      => $amount
]);

Doing all this i get You cannot use a Stripe token more than once.

Any ideas how can i save the cc to this user $stripeUserId and charge it.

PHP is welcome, but js is great too.

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

1 Answers1

0

https://stripe.com/docs/tutorials/charges

Saving credit card details for later

Stripe tokens can only be used once, but that doesn't mean you have to request your customer's card details for every payment. Stripe provides a Customer object type that makes it easy to save this—and other—information for later use.

Instead of charging the card immediately, create a new Customer, saving the token on the Customer in the process. This will let you charge the customer at any point in the future:

(example follows in multiple languages). PHP version:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("yourkey");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "Example customer")
);

// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);

// YOUR CODE: Save the customer ID and other info in a database for later!

// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

\Stripe\Charge::create(array(
  "amount"   => 1500, // $15.00 this time
  "currency" => "usd",
  "customer" => $customerId // Previously stored, then retrieved
  ));

After creating a customer in Stripe with a stored payment method, you can charge that customer at any point in time by passing the customer ID—instead of a card representation—in the charge request. Be certain to store the customer ID on your side for later use.

More info at https://stripe.com/docs/api#create_charge-customer

Stripe have excellent documentation, please read it!

jcaron
  • 17,302
  • 6
  • 32
  • 46