Stripe Webhook times out half way through

67
May 14, 2022, at 07:10 AM

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.

From what I can tell, it times out after the $sql = "INSERT INTO pending_test (userid, sessionid, sessionpi) VALUES (?, ?, ?);"; statement runs, as that insert is working fine.

I need the entire script to run, not only a section of it.

Initially, I thought it was because the script sleeps for 30 seconds, but that isn't the case, because the first insert works fine.

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 = "SELECT * FROM pending WHERE customerid = ?;";
  $stmt = mysqli_stmt_init($conn);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../error.php?session=invalid&error=sql");
    exit();
  }else {
    mysqli_stmt_bind_param($stmt, "s", $sessionpi);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    if ($row = mysqli_fetch_assoc($result)) {
      $userid = $row['userid'];
      $sessionid = $row['sessionid'];
    }
  }

  $sql = "INSERT INTO pending_test (userid, sessionid, sessionpi) VALUES (?, ?, ?);";
  $stmt = mysqli_stmt_init($conn);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../error.php?session=invalid&error=sql");
    exit();
  }else {
    mysqli_stmt_bind_param($stmt, "sss", $userid, $sessionid, $sessionpi);
    mysqli_stmt_execute($stmt);
  }

**THIS IS WHERE IT TIMES OUT**
  if (!$userid && !$sessionid){
    try {
        $checkout_session = \Stripe\Checkout\Session::retrieve($sessionid);
    } catch(Exception $e) {
        $api_error = $e->getMessage();
    }
    try {
        $paymentIntent = \Stripe\PaymentIntent::retrieve($checkout_session->payment_intent);
    } catch (\Stripe\Exception\ApiErrorException $e) {
        $api_error = $e->getMessage();
    }
    $sql = "SELECT * FROM accounts WHERE id = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      header("Location: ../error.php?session=invalid&error=sql");
      exit();
    }else {
      mysqli_stmt_bind_param($stmt, "s", $userid);
      mysqli_stmt_execute($stmt);
      $result = mysqli_stmt_get_result($stmt);
      if ($row = mysqli_fetch_assoc($result)) {
        $originalpoints = $row['points'];
      }
    }
    $sql = "UPDATE accounts SET points = ? WHERE id = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
      $output_array = array();
        $response["error"] = TRUE;
      exit();
    }else {
      $newpoints = (($paymentIntent->amount) + $originalpoints);
      mysqli_stmt_bind_param($stmt, "ss", $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->type) {
  case 'checkout.session.completed':
    $session = $event->data->object;
    $sessionpi = $event->data->object->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->payment_status == 'paid') {
      // Fulfill the purchase
      fulfill_order($sessionpi);
    }
    break;
  case 'checkout.session.async_payment_succeeded':
    $session = $event->data->object;
    $sessionpi = $event->data->object->payment_intent;
    // Fulfill the purchase
    fulfill_order($sessionpi);
    break;
  case 'checkout.session.async_payment_failed':
    $session = $event->data->object;
    // Send an email to the customer asking them to retry their order
    email_customer_about_failed_payment($session);
    break;
}
http_response_code(200);```
Rent Charter Buses Company
READ ALSO
php json_decode function doesn't work with me

php json_decode function doesn't work with me

when decoding this json file 'https://2geekproarblogspot

146
How do I give specific users access to specific websites in PHP?

How do I give specific users access to specific websites in PHP?

I'm building a login systemMy database looks like this Lets say, after loggin in, the code recognizes who is currently logged in and sends them to a specific website, based on the content in their KURS field

108
Email verification with PHPMailer without sending link?

Email verification with PHPMailer without sending link?

I'm currently writing a chat program in PHP and AJAX and have PHPMailer installed and in use

109
What is a single pipe equivalent in dart?

What is a single pipe equivalent in dart?

I'm attempting to convert a piece of PHP code to dart and I came across a few things I've simply never used before, one of which is a single pipe |Im not sure what the exact dart equivalent is, if there even is one

117