Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i charge with card id or customer id using stripe php?

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 .

like image 445
Hashaam zahid Avatar asked Oct 15 '25 03:10

Hashaam zahid


1 Answers

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

like image 117
postmoderngres Avatar answered Oct 17 '25 07:10

postmoderngres