#Nick137645
1 messages Β· Page 1 of 1 (latest)
Stripe provides Search Customers API: https://stripe.com/docs/api/customers/search which you can perform a search with email address
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
yes
$customer_search = \Stripe\Customer::search([
'email' => $_SESSION['user_email'],
'limit' => 1,
]); ```
Instead of searching, I'd recommend storing the customer ID and email address mapping in your database, so that you don't have to make additional request to stripe
You should use query parameter on the search. There is no email field on Search Customer API
Yes I already do.
The customer logs in to his free account and upgrades to the premium version.
I need to attach his payment to the email address of his user account on my website.
What is the issue here?
"query" => 'email~"'.$_SESSION['email'].'"',
"limit" => 1,
]); ```
Can you share the request ID (req_xxx) which you perform the search with above code? Hereβs how you can find it: https://support.stripe.com/questions/finding-the-id-for-an-api-request
ok
I can't find it. I think the request is not reaching the api stripe server. the last is req_Hv0PqcjLInU5nd
Do you get any response from the request? The customer search should look like:
$stripe->customers->search([
'query' => 'email:\'test@test.com\'',
]);
no, I receive an error 500
I will modify the code with your proposal
I followed your advice, the search request works but it creates me a customer without his email
The above API is only for search. Can you share the request ID (req_xxx) where you created the customer?
In https://dashboard.stripe.com/test/logs/req_OLhtAy8wTw9rAG, you didn't set the email address when creating the customer
The code to create a customer will be:
$stripe->customers->create([
'email' => 'test@test.com',
]);
I had indicated $_SESSION['user_mail'] instead of $_SESSION['user_email'].
Searching for the user via his email address still doesn't work, I'll try to improve my request. For the moment I have clients that are created in duplicate.
It finally works
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
$user_email = $_SESSION['user_email'];
$user_username = $_SESSION['username'];
$user_id = $_SESSION['user_id'];
// Search for the customer by email
$customer_search = \Stripe\Customer::search([
//'query' => 'email:" '. $user_email .'"',
"query" => 'email~"'.$user_email.'"',
"limit" => 1,
]);
// If customer not found, create a new customer
if (count($customer_search->data) === 0) {
$customer = \Stripe\Customer::create([
'email' => $user_email,
'metadata' => [
'user_id' => $user_email,
'username' => $user_username,
],
]);
} else {
// Update existing customer
$customer = $customer_search->data[0];
\Stripe\Customer::update($customer->id, [
'metadata' => [
'user_id' => $user_id,
'username' => $user_username,
],
]);
}
// Create a PaymentIntent with amount and currency
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($jsonObj->items),
'customer' => $customer->id,
'currency' => 'usd',
'automatic_payment_methods' => [
'enabled' => false,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
thank for you help river π