PHP PayPal Catalog REST API will not display new product

170 Views Asked by At

Since adding a couple of new sandbox Products using the Catalog API with the PHP curl object, this is what has happened:

  1. The two products are visible using the REST API.
  2. But I can only edit the second one using PATCH. The calls to update the other one do not error, they just don't do anything.
  3. I can add new Products using the API, again with no errors but they do not appear in the list API response afterwards.
  4. When trying to add a new Product with the same ID as one I apparently added before but which is not visible, there is a DUPLICATE_RESOURCE_IDENTIFIER error in the response array, confirming that it is in there somewhere.

What on earth is going on?

Here is the code:

ADD PRODUCT

        $ch = curl_init();
        
        $authorization="Authorization: Bearer ".$authorization;
        
        $url=$sbx=='1'?$url='https://api.sandbox.paypal.com/v1/catalogs/products':'https://api.paypal.com/v1/catalogs/products';
        
        echo $url."<br><br>";
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "xxxxxx","description": "App 005", "type": "SERVICE", "category": "SOFTWARE", "id": "RBW00005"}');
        curl_setopt($ch, CURLOPT_POST, 1);
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = $authorization;
        $headers[] = 'Paypal-Request-Id: FGAS005';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        
        $response = json_decode($result);

        print_r($response);
        echo "<br><br>";
        print_r(curl_getinfo($ch));

        $response="";
        
        curl_close($ch);

LIST PRODUCTS

    $ch = curl_init();
    
    $authorization="Authorization: Bearer ".$authorization;
    
    $url=$sbx=='1'?$url='https://api.sandbox.paypal.com/v1/catalogs/products?page_size=2&page=1&total_required=true':
        'https://api.paypal.com/v1/catalogs/products?page_size=2&page=1&total_required=true';
        
    echo $url."<br><br>";
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = $authorization;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    
    $response = json_decode($result);
    
    print_r($response);
    echo "<br><br>";
    print_r(curl_getinfo($ch));
    
    $response="";
    
    curl_close($ch);

And it's not missing square brackets from around the post string.

If I put them in I get a 'malformed request' error. No square brackets, no error but no visible Product either.

Thanks for reading.

1

There are 1 best solutions below

1
On

Just noticed you seem to be hardcoding a Request ID header:

$headers[] = 'Paypal-Request-Id: FGAS005';

Ensure you never do this; the value of that header ought to be unique for each independent request, or more simply not set at all (comment this line out)