#Nick137645

1 messages Β· Page 1 of 1 (latest)

lucid quarryBOT
sudden sparrow
supple geyser
#

yes

#
    $customer_search = \Stripe\Customer::search([
        'email' => $_SESSION['user_email'],
        'limit' => 1,
    ]); ```
sudden sparrow
#

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

supple geyser
#

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.

sudden sparrow
#

What is the issue here?

supple geyser
#
            "query" => 'email~"'.$_SESSION['email'].'"',
            "limit" => 1,
        ]); ```
sudden sparrow
supple geyser
#

ok

#

I can't find it. I think the request is not reaching the api stripe server. the last is req_Hv0PqcjLInU5nd

sudden sparrow
#

Do you get any response from the request? The customer search should look like:

$stripe->customers->search([
  'query' => 'email:\'test@test.com\'',
]);
supple geyser
#

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

sudden sparrow
#

The above API is only for search. Can you share the request ID (req_xxx) where you created the customer?

supple geyser
#

yes

#

req_YgjJEG8ZCjso9y

#

oups: req_OLhtAy8wTw9rAG

sudden sparrow
#

The code to create a customer will be:

$stripe->customers->create([
  'email' => 'test@test.com',
]);
supple geyser
#

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 πŸ˜‰

sudden sparrow
#

Great to hear that it's working!

#

No problem! Happy to help πŸ˜„

supple geyser
#

I'm going to bed, I've seen the clock make two turns of the dial and my heart is pumping caffeine

#

😊