3
votes

I store card information in database e.g card-id card_***** and customer id cus_**** on customer first payment for use it later on. user choose his card e.g in form of radio button visa****4242 and charge takes with card id or customer id. now i am using stripe modal box for payment. every payment new customer and card id create . how do i charge with card id or customer id on second payment ? here is my stripe code

$customer =Stripe_Customer::create(array(
'email' => $_SESSION['userEmail'],));
$charge = Stripe_Charge::create(array(       
"amount" => $amount_cents,"currency" => "usd","source" => 
$_POST['stripeToken'],"description" => $description,      
));

Possible this question may duplicate.But i can not find best answer .

2

2 Answers

8
votes

Once you create a customer you can charge their default card with:

\Stripe\Charge::create(array(
  "amount" => $amount_cents,
  "currency" => "usd",
  "customer" => $customer
));

If the customer has more than one card and you want to charge a card that isn't the default card, you have to pass in the customer and card id (when the card has already been saved to the customer):

\Stripe\Charge::create(array(
  "amount" => $amount_cents,
  "currency" => "usd",
  "customer" => $customer,
  "source" => $card_id
));

You can read more about this in Stripe's API docs:

https://stripe.com/docs/api/php#create_charge-source

1
votes

Line 1, you are creating a new customer every time.

You should store the identifier of the customer and source and bind it onto your local customer account. Next time you can pass those two into Charge.