首页 > 解决方案 > 方形付款表格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:

标签: phpformspaymentsquare

解决方案


推荐阅读