phpstripe-payments

PHP Stripe customers->search not finding accurate results


I'm having difficulty with STRIPE customer->search returning incorrect results sometimes.

If I search for a customer, and if it does not exist then add it, then repeat - I can find it adds the customer a second/third/fourth/etc times - for a while. Meanwhile on the stripe dashboard, I can see the customers appearing [multiple times].

If I waited a few minutes between initial refreshes - it seems ok.

Perhaps there's a cache, or a lag or something between adding a customer and the search finding it but this is not very helpful. Am I wrong - or is there something I'm not doing?

The relevant PHP code is here:

$stripe = new \Stripe\StripeClient($stripe_secretkey);


echo "<LI>Searching for $customerNm...</LI>";
$json = $stripe->customers->search(['query' => 'name:\'' . $customerNm . '\'']);
foreach ($json->data as $key) {
    echo "<LI>Found id:[" . $key->id . "]";
    if (strcmp($key->name, $customerNm) == 0) {
        $customerId = $key->id;
        $email = $key->email;
        break;
    }
 }
 echo "<hr>";


 if (strcmp($customerId, "") == 0) {

     echo "<LI>Result to search is <PRE style='margin-left:20px'>$json</PRE>";
     $email = str_replace(" ", "", str_replace(" ", "", "abc@$customerNm.com"));
     $json = $stripe->customers->create(
          [
             'email' => $email,
             'name'  => $customerNm,
          ]);
          $customerId = $json->id;
          echo "<UL>Created New STRIPE customer id: [$customerId]</UL>\n";

     }
 }

This code can be found running on a sandbox here** where if you were to run the page multiple times - it will keep creating customers [for a while].

(Note, this page will create a cookie "posterUID" which is used as the customer name; once it's created then it stays until expiry or manual deletion).

** The full source to this can be seen here


Solution

  • This is expected behavior per Stripe's Search documentation:

    Don’t use search for read-after-write flows (for example, searching immediately after a charge is made) because the data won’t be immediately available to search. Under normal operating conditions, data is searchable in under 1 minute. Propagation of new or updated data could be more delayed during an outage.