Skip to content

Instantly share code, notes, and snippets.

@aligajani
Created August 31, 2017 00:10
Show Gist options
  • Save aligajani/0342c6a57c520d05c2b1ee795c649ab2 to your computer and use it in GitHub Desktop.
Save aligajani/0342c6a57c520d05c2b1ee795c649ab2 to your computer and use it in GitHub Desktop.
Retrieve Stripe Customers via email
<?php
$email = "your_email_query_here";
try {
$client = $client = new \GuzzleHttp\Client();
$client->setDefaultOption('verify', true);
$httpResource = 'https://api.stripe.com/v1/search?query="'.$email.'"&prefix=false';
$request = $client->get($httpResource, [
'headers' => ["Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Bearer ".$stripeApiKey]
]);
$response = json_decode($request->getBody());
if ($response->count > 0) {
foreach ($response->data as $customer) {
if (strlen($customer->id) == 18) {
echo $customer->id . PHP_EOL;
}
}
} else {
return 'No match found';
}
} catch (ClientException $e) {
if ($e->hasResponse()) {
Log::info('ClientException: RequestService::get() =>' .print_r($email, true) . '=>' . $e->getMessage());
return;
}
} catch (TransferException $e) {
Log::info('TransferException: RequestService::get() =>' .print_r($email, true) . '=>' . $e->getMessage());
return;
}
@ibraheem-ghazi
Copy link

using this will return every object related to this email, it's better to restrict the search by object type customer
so the URL:
$httpResource = 'https://api.stripe.com/v1/search?query=is:customer email:'.$email.'&prefix=false';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment