title: Stripe Webhook times out half way through
body: <p>I have a Stripe webhook that times out half way through my request. The webhook itself is working, as I have it inserting data into my database. However, I can't seem to identify why it would be timing out half way through, preventing the rest of the query from running.</p>
  <p>From what I can tell, it times out after the <code> $sql = &quot;INSERT INTO pending_test (userid, sessionid, sessionpi) VALUES (?, ?, ?);&quot;;</code> statement runs, as that insert is working fine.</p>
  <p>I need the entire script to run, not only a section of it.</p>
  <p>Initially, I thought it was because the script sleeps for 30 seconds, but that isn't the case, because the first insert works fine.</p>
  <pre><code>require_once('stripe-php/init.php');
  
  // Set your secret key. Remember to switch to your live secret key in production.
  // See your keys here: https://dashboard.stripe.com/apikeys
  \Stripe\Stripe::setApiKey('STRIPE KEY');
  
  function print_log($val) {
    return file_put_contents('php://stderr', print_r($val, TRUE));
  }
  // You can find your endpoint's secret in your webhook settings
  $endpoint_secret = 'ENDPOINT KEY';
  
  $payload = @file_get_contents('php://input');
  $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
  $event = null;
  
  try {
    $event = \Stripe\Webhook::constructEvent(
      $payload, $sig_header, $endpoint_secret
    );
  } catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
  } catch(\Stripe\Exception\SignatureVerificationException $e) {
    // Invalid signature
    http_response_code(400);
    exit();
  }
  
  function fulfill_order($sessionpi) {
  require 'dbh.inc.php';
  require_once('stripe-php/init.php');
  \Stripe\Stripe::setApiKey('STRIPE KEY');
  
    sleep(30);
  
    $sql = &quot;SELECT * FROM pending WHERE customerid = ?;&quot;;
    $stmt = mysqli_stmt_init($conn);
  
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      header(&quot;Location: ../error.php?session=invalid&amp;error=sql&quot;);
      exit();
    }else {
      mysqli_stmt_bind_param($stmt, &quot;s&quot;, $sessionpi);
      mysqli_stmt_execute($stmt);
  
      $result = mysqli_stmt_get_result($stmt);
  
      if ($row = mysqli_fetch_assoc($result)) {
        $userid = $row['userid'];
        $sessionid = $row['sessionid'];
      }
    }
  
  
    $sql = &quot;INSERT INTO pending_test (userid, sessionid, sessionpi) VALUES (?, ?, ?);&quot;;
    $stmt = mysqli_stmt_init($conn);
  
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      header(&quot;Location: ../error.php?session=invalid&amp;error=sql&quot;);
      exit();
    }else {
      mysqli_stmt_bind_param($stmt, &quot;sss&quot;, $userid, $sessionid, $sessionpi);
      mysqli_stmt_execute($stmt);
  
    }
  
  
  **THIS IS WHERE IT TIMES OUT**
  
    if (!$userid &amp;&amp; !$sessionid){
  
      try {
          $checkout_session = \Stripe\Checkout\Session::retrieve($sessionid);
      } catch(Exception $e) {
          $api_error = $e-&gt;getMessage();
      }
  
      try {
          $paymentIntent = \Stripe\PaymentIntent::retrieve($checkout_session-&gt;payment_intent);
      } catch (\Stripe\Exception\ApiErrorException $e) {
          $api_error = $e-&gt;getMessage();
      }
  
      $sql = &quot;SELECT * FROM accounts WHERE id = ?;&quot;;
      $stmt = mysqli_stmt_init($conn);
  
      if (!mysqli_stmt_prepare($stmt, $sql)) {
        header(&quot;Location: ../error.php?session=invalid&amp;error=sql&quot;);
        exit();
      }else {
        mysqli_stmt_bind_param($stmt, &quot;s&quot;, $userid);
        mysqli_stmt_execute($stmt);
  
        $result = mysqli_stmt_get_result($stmt);
  
        if ($row = mysqli_fetch_assoc($result)) {
          $originalpoints = $row['points'];
        }
      }
  
      $sql = &quot;UPDATE accounts SET points = ? WHERE id = ?;&quot;;
      $stmt = mysqli_stmt_init($conn);
      if (!mysqli_stmt_prepare($stmt, $sql)) {
        $output_array = array();
          $response[&quot;error&quot;] = TRUE;
        exit();
      }else {
        $newpoints = (($paymentIntent-&gt;amount) + $originalpoints);
        mysqli_stmt_bind_param($stmt, &quot;ss&quot;, $newpoints, $userid);
        mysqli_stmt_execute($stmt);
      }
  
  
    }
  }
  
  function create_order($session) {
    // TODO fill me in
  }
  
  function email_customer_about_failed_payment($session) {
    // TODO fill me in
  }
  
  switch ($event-&gt;type) {
    case 'checkout.session.completed':
      $session = $event-&gt;data-&gt;object;
      $sessionpi = $event-&gt;data-&gt;object-&gt;payment_intent;
  
      // Save an order in your database, marked as 'awaiting payment'
      create_order($session);
  
      // Check if the order is paid (for example, from a card payment)
      //
      // A delayed notification payment will have an `unpaid` status, as
      // you're still waiting for funds to be transferred from the customer's
      // account.
      if ($session-&gt;payment_status == 'paid') {
        // Fulfill the purchase
        fulfill_order($sessionpi);
      }
  
      break;
  
    case 'checkout.session.async_payment_succeeded':
      $session = $event-&gt;data-&gt;object;
      $sessionpi = $event-&gt;data-&gt;object-&gt;payment_intent;
  
      // Fulfill the purchase
      fulfill_order($sessionpi);
  
      break;
  
    case 'checkout.session.async_payment_failed':
      $session = $event-&gt;data-&gt;object;
  
      // Send an email to the customer asking them to retry their order
      email_customer_about_failed_payment($session);
  
      break;
  }
  
  http_response_code(200);```
  </code></pre>