title: Paypal payout get transaction id
body: <p>i use curl to transfer money from business to several personal user . </p>
  
  <p>With a loop i find user that must receive the payment and with a curl POST request and php i do a payment .</p>
  
  <p>The payment return a response with a payout_batch_id and with this i do a curl GET request to get transaction id , but if i dont sleep php for almost 5 seconds i have the response pending instead success . how can i be sure if the payment are really do ?</p>
  
  <p>This is my code :</p>
  
  <pre><code>###########################################################################################
  #                      PAYOUT FROM BUSINESS TO PERSONAL                                   #
  ###########################################################################################
  
  
  
  
  
  private function  curl_request($url, $method, $headers = [], $data = [], $curl_options = []){
  
          $curl = curl_init();
  
          curl_setopt($curl, CURLOPT_URL, $url);
          curl_setopt($curl, CURLOPT_TIMEOUT, 0);
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  
          //--- If any headers set add them to curl request
          if(!empty($headers)){
              curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
          }
  
          //--- Set the request type , GET, POST, PUT or DELETE
          switch($method){
              case "POST":
              curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
              break;
          case "PUT":
              curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
              break;
          case "DELETE":
              curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
              break;
          default:
              curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
              break;
          }
  
          //--- If any data is supposed to be send along with request add it to curl request
          if($data){
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
          }
          //--- Any extra curl options to add in curl object
          if($curl_options){
              foreach($curl_options as $option_key =&gt; $option_value){
                  curl_setopt($curl, $option_key, $option_value);
              }
          }
  
          $response = curl_exec($curl);
          $error = curl_error($curl);
          curl_close($curl);
  
          //--- If curl request returned any error return the error
          if ($error) {
              return "CURL Error: $error";
          }
          //--- Return response received from call
          return $response;
  }
  
  
  public function get_access_token()
  {
  
      //--- Headers for our token request
      $headers[] = "Accept: application/json";
      $headers[] = "Content-Type: application/x-www-form-urlencoded";
  
      //--- Data field for our token request
      $data = "grant_type=client_credentials";
  
      //--- Pass client id &amp; client secrent for authorization
      $curl_options[CURLOPT_USERPWD] = $this-&gt;CI-&gt;config-&gt;item('client_id') . ":" . $this-&gt;CI-&gt;config-&gt;item('client_secret');
  
      $token_request = $this-&gt;curl_request($this-&gt;CI-&gt;config-&gt;item('PAYPAL_TOKEN_URL'), "POST", $headers, $data, $curl_options);
      $token_request = json_decode($token_request);
      if(isset($token_request-&gt;error)){
  
          $result['no_error'] = FALSE;
  
          $result['message_error'] =  $token_request-&gt;error_description;
  
  
  
      }else{
  
          $result['no_error'] = TRUE;
  
          $result['token_request'] = $token_request ;
  
      } 
  
      return $result;
  
  }
  
  
  function transfer_money_from_business_to_personal($email_receiver,$cifra)
  {   
  
  
      $this-&gt;log_ipn_test('########################## NUOVO TENTATIVO DI PAGAMENTO ############################## ');
  
  
      if ($email_receiver=='') {
  
        $this-&gt;log_ipn_test('Mail vuota impossibile pagare');
        die();
      }
  
      //restituisce un array per poter inserire nel caso gli errori
      $result= $this-&gt;get_access_token();
  
  
  
      if($result['no_error']){
  
  
          $token_request = $result['token_request'];
  
          $this-&gt;log_ipn_test('Token Ok');
  
          $headers = $data = [];
          //--- Headers for payout request
          $headers[] = "Content-Type: application/json";
          $headers[] = "Authorization: Bearer $token_request-&gt;access_token";
  
          $time = time();
          //--- Prepare sender batch header
          $sender_batch_header["sender_batch_id"] = $time;
          $sender_batch_header["email_subject"]   = "Payout Received";
          $sender_batch_header["email_message"]   = "You have received a payout, Thank you for using our services";
  
          //--- First receiver
          $receiver["recipient_type"] = "EMAIL";
          $receiver["note"] = "Thank you for your services";
          $receiver["sender_item_id"] = $time++;
          $receiver["receiver"] = $email_receiver;
          $receiver["amount"]["value"] = $cifra;
          $receiver["amount"]["currency"] = "EUR";
          $items[] = $receiver;
          /*
          //--- Second receiver
          $receiver["recipient_type"] = "EMAIL";
          $receiver["note"] = "You received a payout for your services";
          $receiver["sender_item_id"] = $time++;
          $receiver["receiver"] = "buyer2@example.com";
          $receiver["amount"]["value"] = 15.00;
          $receiver["amount"]["currency"] = "USD";
          $items[] = $receiver;
          */
          $data["sender_batch_header"] = $sender_batch_header;
          $data["items"] = $items;
  
          //--- Send payout request
          $payout = $this-&gt;curl_request($this-&gt;CI-&gt;config-&gt;item('PAYPAL_PAYOUTS_URL'), "POST", $headers, json_encode($data));
  
          $payout = json_decode($payout);
          //$this-&gt;log_ipn_test("|2|$payout[0]['batch_header']");
          //$this-&gt;log_ipn_test("|2|$payout[0]['batch_header']['payout_batch_id']");
  
  
  
          $headers2 = $data2 = [];
          //--- Headers for payment description
          $headers2[] = "Content-Type: application/json";
          $headers2[] = "Authorization: Bearer $token_request-&gt;access_token";
  
          $link = $payout-&gt;links[0]-&gt;href;
  
          $this-&gt;log_ipn_test($link);
  
          sleep(3); //sleep to prevent PENDING
  
          $dati_pagamento = $this-&gt;curl_request($link, "GET", $headers2);
  
          $this-&gt;log_ipn_test(json_encode($dati_pagamento));
  
          var_dump($dati_pagamento);
  
          if($payout){
  
              return TRUE;
  
          }else{
  
              return FALSE;
  
          }
  
  
      }else{
  
          $this-&gt;log_ipn_test($result['message_error']);
  
          return FALSE;
  
  
      }
  
  } //fine transfer_money_from_business_to_personal
  
  
  ###########################################################################################
  #                       END TRANSFER FROM BUSINESS TO PERSONAL                            #
  ###########################################################################################
  </code></pre>