square payment form php check customer exists in directory and make payment

Am new to Square Payment Form using PHP and trying to do following steps

  1. Check If customer Exists in Directory
  2. If Customer Does not exist - create new customer and collect CUSTOMER_ID
  3. MAKE PAYMENT & get Payment ID

Couple of time this script worked by started getting Maximum execution time of 30 seconds exceeded AND created multiple customer entries in directory and payment failed

PS : am using GOTO to loop and ENV file for credentials

Any help will be appreciated in advance

booknow = _POST;

		require 'vendor/autoload.php';

		use Dotenv\Dotenv;
		use Square\Models\Money;
		use Square\Models\CreatePaymentRequest;
		use Square\Exceptions\ApiException;
		use Square\SquareClient;
		use Square\Environment;
		use SquareConnect\ApiClient;
		use Square\Models\CreateCustomerRequest;

		$idem = UUID::v4();

		$sname = explode(' ', $_POST["cname"]);
		$sname0 = $sname[0];
		$sname1 = $sname[1];

		$cphone = $_POST["cphone"];
		//$cphone = '9848848450';
		$cemailid = $_POST["cemailid"];
		$ifare = ($_POST["ifare"] * 100);
		$xnote = $_POST["note"];

		//echo '<br><br>fare : ' . $ifare . '<br><br>';

		$dotenv = Dotenv::create(__DIR__);

		$upper_case_environment = strtoupper(getenv('ENVIRONMENT'));

		$access_token =  getenv($upper_case_environment.'_ACCESS_TOKEN');   

		 //echo '<br><br><br>';

		$client = new SquareClient([
		  'accessToken' => $access_token,  
		  'environment' => getenv('ENVIRONMENT')

		  error_log('Received a non-POST request');
		  echo 'Request not allowed';

		$nonce = $_POST['nonce'];
		if (is_null($nonce)) {
		  echo 'Invalid card data';

			searchCustomers: ///search customers

			$phone_number = new \Square\Models\CustomerTextFilter();

			$filter = new \Square\Models\CustomerFilter();

			$query = new \Square\Models\CustomerQuery();

			$body = new \Square\Models\SearchCustomersRequest();

			$api_response = $client->getCustomersApi()->searchCustomers($body);

			if ($api_response->isSuccess()) {

				$rmn = $api_response->getBody();
				$stx = json_decode($rmn,true);

				echo '<br><br>';	
				echo '# of arrays : ' . count($stx);
				if(count($stx) != 0){
					//echo '<br><br>';		
					$cust_id = $stx["customers"][0]["id"];
					//echo "Customer ID : " . $cust_id;
					goto makePayment;
					//goto end;

				} else {
					//echo 'user do not exists';
					/// new customer - start

					$body1 = new \Square\Models\CreateCustomerRequest();

					$api_response = $client->getCustomersApi()->createCustomer($body1);

					if ($api_response->isSuccess()) {
					    $result = $api_response->getResult();
					     goto searchCustomers;
					} else {
					    $errors = $api_response->getErrors();

					/// new customer - end
			} else {
			   echo '<br><br>sorry not found!<bR><br>';

			goto end;


			$amount_money = new \Square\Models\Money();

			$body = new \Square\Models\CreatePaymentRequest(

			$api_response = $client->getPaymentsApi()->createPayment($body);

			if ($api_response->isSuccess()) {
			    $result = $api_response->getResult();
			    $srt = json_encode($result);
			    echo '<br><br>';
			    echo "PAYEMNT SUCCESSUFLL <BR><br><br>";
			    goto end;

			} else {
			    $errors = $api_response->getErrors();
			    echo 'payment FAILEDDDDDDDDDD';

			goto end;


Hi @bgb76hyd welcome to the forums!

I’m copying over my comment from your StackOverflow post:
I believe there can be a slight delay between creating a customer, and it being retrievable/searchable. So your loop very likely will create duplicate customer objects; you should just use the response of the customer object, rather than searching again (which would also reduce the API calls and amount of work to be done anyway). Not sure about the maximum execution time that is mentioned