#dev123-checkout
1 messages · Page 1 of 1 (latest)
@stray plinth yep, that would be expected. To re-use an existing customer you have to pass their Stripe ID cus_xxxx to the customer field to Session::create
usually you'd have that Stripe Customer ID in your database, saved from when you created it or their first payment, or you can try to look it up in the API with https://stripe.com/docs/api/customers/list#list_customers-email
Hello and thanks for trying to help me. I am using $ stripe-> customers-> all () to retrieve the list of customers. If it does not exist I create it, or it is created during the first payment. But when I make a payment the customer is created again in stripe, I do send the client_reference_id (Customer ID) parameter in the checkout call. Yet each time the same client is recreated
And the stripe create a new client with the same mail and an different ID
yes that's expected!
client_reference_id or customer_email doesn't make us re-use an existing customer
the only thing that does is passing the ID of an existing Customer object to the customer field : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer
so, I went in the logs. I check for payment_methods log. I can see in the response that "customer": null, whats that's mean ? Did I send a null id ?
probably! hard for me to say without seeing the code you're using
Yes ok, I will have a check of that. I will pass the customer_id in the session to be sure, let's me try
Could you help me to investigate on the req_Ar6nDcuDFEmY9B (POST
on /v1/checkout/sessions) ? I send an existing customer_id, but always create a new customer. What I am missing ? thank
I will purge all the double client to make a new test,
you are not passing the customer field on that request. You pass a customer_email but again, it's specifically customer that you have to pass if you want to re-use a Customer. If you're trying to pass it, probably the variable you use is null which is why it doesn't show up in the request that gets received by us.
what's the exact PHP code you're using?
please wait a little moment, thank
yes, i understand, I retried with hard coded customer_id, but the same. I send you my code :
first page :
//---------------------------------------------------
// Verifier si le client exist
//---------------------------------------------------
header('Content-Type: application/json');
$stripe = new \Stripe\StripeClient($privateSecretStripe);
$response = $stripe->customers->all();
// echo $response;
$response=str_replace ( 'Stripe\Collection JSON:','', $response);
// $response = html_entity_decode($response);
$tabResp=json_decode($response,true);
// echo ' <br>tableau => '.print_r($tabResp) ;
$stripeData=$tabResp['data'];
// echo 'stripeData='.$j.' = '.print_r($stripeData[$j]);
$trouve=false;
$debug=0;
for($c=0;$c<count($stripeData);$c++){
if($debug==1) echo '$c='.$c;
$stripeInfos=$stripeData[$c];
$nom=$stripeInfos['name'];
$id=$stripeInfos['id'];
$email=$stripeInfos['email'];
if($debug==1) echo ' id => '.$id;
if($debug==1) echo ' email => '.$email;
if($debug==1) echo ' nom => '.$nom;
if($debug==1) echo ' - - - - - - ';
if($email==$_SESSION['idMail'] && $trouve==false) {
$custId=$id;
//$custId='cus_Kegpp2CubRK0Lx';
$trouve=true;
}
}
//---------------------------------------------------
// Creer le client si existe pas
//---------------------------------------------------
if($trouve==false){
$debug=0;
$stripe = new \Stripe\StripeClient($privateSecretStripe);
$response = $stripe->customers->create([
'description' => 'Client crée par abonnement a 123Ventes ',
'email' => $_SESSION['idMail'],
]);
// echo $response;
$response=str_replace ( 'Stripe\Customer JSON:','', $response);
$tabResp=json_decode($response,true);
$custId=$tabResp['id'];
if($debug==1) echo ' id => '.$custId;
}
//$custId='cus_Kegpp2CubRK0Lx';
$_SESSION['custId']=$custId;
and then the checkout paiement :
//---------------------------------------------------
//on demande et recupere le token
//---------------------------------------------------
\Stripe\Stripe::setApiKey($privateSecretStripe);
//---------------------------------------------------
//on prepare le post
//---------------------------------------------------
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' =>$successUrl,
'cancel_url' => $errorUrl,
'payment_method_types' => ["card"],
'mode' => 'subscription',
'client_reference_id' => $_SESSION['custId'],
'customer_email' => $_SESSION['idMail'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'locale' => $locale,
]);
//---------------------------------------------------
//on envoi le post
//---------------------------------------------------
// header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);
for what it's worth you really don't need all that stuff like $response=str_replace ( 'Stripe\Collection JSON:','', $response);
like you can just access $response->data; for the list of the customers; or $response->data[0]->id; to get the ID of the first customer in the list that was returned, you never need to mess around with this JSON decoding or replacing you're doing
the library returns a normal PHP object that you can access fields of, I'd suggest experimenting with it a bit
anyway
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' =>$successUrl,
'cancel_url' => $errorUrl,
'payment_method_types' => ["card"],
'mode' => 'subscription',
'client_reference_id' => $_SESSION['custId'],
'customer_email' => $_SESSION['idMail'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'locale' => $locale,
]);
you don't pass customer here, as I said
you need something more like
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' =>$successUrl,
'cancel_url' => $errorUrl,
'payment_method_types' => ["card"],
'mode' => 'subscription',
'client_reference_id' => $_SESSION['custId'],
'customer' => $_SESSION['custId'], // <--------- "cus_xxx"
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'locale' => $locale,
]);
or along those lines.
yes, thank you, this was because I was facing pb to read response and format to array. But you give me a light👍 , I understand your code more efficient :$response->data[0]->id;
seem the was a confusing between client_reference_id and customer ! I wil test with what your said. Let me a moment,
👍 ok, thanks it works now; I had to remove 'customer_email' from the call because it conflicts with the customer (You may only specify one of these parameters: customer, customer_email.). It was my mistake, I thought the email was equivalent to the id_customer as a unique identifier, but no, I understand that the email allows to create the email account when needed but is not a login. So that's pretty clear to me now. I will resume the code as you indicated to me to read the json more efficiently. Thank you very much !