方形付款表格php检查目录中是否存在客户并进行付款

问题描述

是使用PHP的Square Payment Form的新手,并尝试执行以下步骤

  1. 检查客户是否存在于目录中
  2. 如果客户不存在-创建新客户并收集CUSTOMER_ID
  3. 付款并获得付款ID

此脚本花费的时间开始超过30秒的最大执行时间,并在目录中创建了多个客户条目,并且付款失败

PS:正在使用GOTO循环和ENV文件获取凭据

任何帮助将被提前感谢

print_r($_POST); 
        $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__);
        $dotenv->load();

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

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

        //print_r($access_token);
         //echo '<br><br><br>';

        $client = new SquareClient([
          'accesstoken' => $access_token,'environment' => getenv('ENVIRONMENT')
        ]);


        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
          error_log('Received a non-POST request');
          echo 'Request not allowed';
          http_response_code(405);
          return;
        }

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



            searchCustomers: ///search customers

            $phone_number = new \Square\Models\CustomerTextFilter();
            $phone_number->setFuzzy($cphone);

            $filter = new \Square\Models\CustomerFilter();
            $filter->setPhoneNumber($phone_number);

            $query = new \Square\Models\CustomerQuery();
            $query->setFilter($filter);

            $body = new \Square\Models\SearchCustomersRequest();
            $body->setQuery($query);

            $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();
                    $body1->setIdempotencyKey($idem);
                    $body1->setGivenname($sname0);
                    $body1->setFamilyName($sname1);
                    $body1->setEmailAddress($cemailid);
                    $body1->setPhoneNumber($cphone);

                    $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;


            makePayment:

            $amount_money = new \Square\Models\Money();
            $amount_money->setAmount($ifare);
            $amount_money->setCurrency('USD');

            $body = new \Square\Models\CreatePaymentRequest(
                    'cnon:card-nonce-ok',$idem,$amount_money
            );
            $body->setCustomerId($cust_id);
            $body->setNote($xnote);

            $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>";
                //print_r($srt);
                
                goto end;

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

            goto end;

            end:

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)