skip to Main Content

How to retrieve the identifier of an existing customer via his email address or create the customer if he does not hesitate when creating a payment with API Stripe?

I searched in the Stripe documentation but couldn’t find the answer.

require "Stripe/vendor/autoload.php";

// This is your test secret API key.
StripeStripe::setApiKey("sk_test_XXX");

header("Content-Type: application/json");

try {
    // retrieve JSON from POST body
    $jsonStr = file_get_contents("php://input");
    $jsonObj = json_decode($jsonStr);

    // get customer if exist
    $query = StripeCustomer::search([
        "query" => 'email:'.'.$user['email'].'.'', 
    ]);
    
    if ($query->id) {
        $customer_ID = $query->id;
    } else {
        $customer = StripeCustomer::create([
            "email" => $user["email"],
            "description" => 'VIP plan',
        ]);
        
        $customer_ID = $customer->id;
    }



    // Create a PaymentIntent with amount and currency
    $paymentIntent = StripePaymentIntent::create([
        "customer" => $customer_ID,
        "amount" => 1400,
        "currency" => "usd",
        "automatic_payment_methods" => [
            "enabled" => true,
        ],
    ]);

    $output = [
        "clientSecret" => $paymentIntent->client_secret,
    ];

    echo json_encode($output);
} catch (Error $e) {
    http_response_code(500);
    echo json_encode(["error" => $e->getMessage()]);
}

2

Answers


  1. Chosen as BEST ANSWER

    Query fields for customers source: https://stripe.com/docs/search#query-fields-for-customers

    FIELD       USAGE                       TYPE (TOKEN, STRING, NUMERIC)
    -------     ----------------------      -----------------------------
    created     created>1620310503          numeric
    email       email~"amyt"                string
    metadata    metadata["key"]:"value"     token
    name        name~"amy"                  string
    phone       phone:"+19999999999"        string
    

    Here is the properly formatted query.

    "query" => 'email~"'.$user['email'].'"', "limit" => 1
    

  2. Your search query is not a simple object but a multidimentional object.

    ‘data’ is missing on your object request :

    $query->data[0]->id
    

    You can’t have access to datas so you might use a for loop as :

    if(sizeof($query->data) !== 0)
    {
       for($i=0;$i<sizeof($query->data);$i++)
       {
          $customer_ID = $query->data[$i]->id;
       }
    }
    else
    {
       // Create customer
    }
    

    If you’re sure to have only one customer, you need to add a limit to the Stripe search query and so, you’ll don’t need to have for loop :

    $query = StripeCustomer::search([
        "query" => 'email:'.'.$user['email'].'.'', 
        "limit" => 1,
    ]);
    
    if(sizeof($query->data) !== 0)
    {
       $customer_ID = $query->data[0]->id;
    }
    else
    {
       // create customer
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search