Search code examples
phpcurlpaypalpayment

PayPal + PHP - Fetch incoming payments (start_time / end_time)


My goal is to list up all incoming(!) PayPal payments from a certain date (e.g. all received payments from March 09th 2021) with PHP/curl. Reading PayPal's GetStarted section I also recognized that there was a API version change from V1 to V2: (PayPal's V1 deprecation note)

Trying V1: For V1 some query parameters are explained that would exactly fit my needs. For example: start_time + end_time. (V1-Parameters) Following the documentation I managed to fetch some payments with V1 but they do not fit the given date. They are from somewhen of year 2018 - although the paypal account was created many years before. So the results seems to be somewhat random style and I guess V1 doesn't work anymore for my needs.

$live_url = "https://api-m.paypal.com/v1/payments/payment";
$myStart_time = date("Y-m-d")."T00:00:00Z";                                 // e.g. 2021-03-09T00:00:00Z
$myEnd_time = date("Y-d-m", time()) ."T". date("H:m:s", time()) . "Z";      // e.g. 2021-03-09T14:21:00Z

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $live_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$paypal_secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "start_time=".$myStart_time);
curl_setopt($ch, CURLOPT_POSTFIELDS, "end_time=".$myEnd_time);
curl_setopt($ch, CURLOPT_POSTFIELDS, "total_count_required=true");
curl_setopt($ch, CURLOPT_POSTFIELDS, "start_index=0");
curl_setopt($ch, CURLOPT_POSTFIELDS, "sort_by=update_time");
curl_setopt($ch, CURLOPT_POSTFIELDS, "sort_order=desc");
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json", "Accept-Language: en_US", 'Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);

Trying V2: I also managed it to fetch PayPal payment details from a certain payment using its transaction code with V2. But for this method I need to know the transaction code before I can list the specific payment. But I do not know the transaction codes before I know what payments have come in.

$payments_url = "https://api.paypal.com/v2/payments/captures/$transaction_code";

(This URL above for V2 is used because "https://api-m.paypal.com/v2/payments/payment" does not exist: Returns HTML 404.)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $payments_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$paypal_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $access_token,
    'Accept: application/json',
    'Content-Type: application/json'
));
$result = curl_exec($ch);

So anyone can suggest how I could manage that? (Maybe I'm just on a wrong way as I did not really find any helpful answers besides very old ones relating to API V1)


Solution

  •     echo "<br>REPORT TRANSACTIONS<br>";
        $live_url = "https://api-m.paypal.com/v1/reporting/transactions";
    
        $transactions_url = "?start_date=2021-03-17T00:00:00Zend_date=2021-03-18T13:50:59Z&fields=all";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $live_url . $transactions_url);
    
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    //  curl_setopt($ch, CURLOPT_POSTFIELDS, "start_date=".($myStart_time));
    //  curl_setopt($ch, CURLOPT_POSTFIELDS, "end_date=".($myEnd_time));
        curl_setopt($ch, CURLOPT_POSTFIELDS, "fields=all");
        curl_setopt($ch, CURLOPT_POSTFIELDS, "page_size=10");
        curl_setopt($ch, CURLOPT_POSTFIELDS, "page=1");
        curl_setopt($ch, CURLOPT_POSTFIELDS, "sync_mode=false");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$paypal_secret);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Accept: application/json", 
            "Accept-Language: en_US", 
            "Authorization: Bearer " . $access_token,
            "Content-Type: application/json"
        ));
    
        $result = curl_exec($ch);
        $info = curl_getinfo($ch);      
        $err = curl_error($ch);
    
        echo "<hr>";
    
        if ($err) {
          echo "<br>cURL Error: $err <br>Info: $info <br>Result: $result";
        }
        else
        {
            echo "<pre>";
            print_r($info);
            print_r($r = json_decode($result)); 
        }