Using Paypal REST api and PHP cURL to post invoice (draft) constantly returns 415 Unsupported Media Type

StackOverflow https://stackoverflow.com/questions/23300533

Question

I am using paypal's create invoice sample json from their website

I am getting a token well-enough, which I submit with the post and since the error isn't 401 Unauthorized but rather 415 Unsupported Media Type (and now recently 500 Internal Server Error) which leads me to believe that I'm likely getting that part right... so I will offer some code in hopes somebody catches something obvious that I'm missing:

createPaypalInvoice.php

function paypalCreateDraftRequest($token,$jsondraft){
    global $url;
    $sUrl = "/v1/invoicing/invoices";
    $accept = "Accept:application/json";
    $acceptlang = "Accept-Language:en_US";
    $contenttype = "Content-Type: application/json";
    $contentlen = "Content-Length: " . strlen($jsondraft);
            // $jsondraft is a string of json from paypal example
    $authorization = "Authorization: Bearer ".$token["access_token"];
    $headers = array($accept,$acceptlang,$contenttype,$contentlen,$authorization);
    $fields_string = $jsondraft;
    $ch = curl_init();
    $options = array (CURLOPT_RETURNTRANSFER => true, // return web page
        CURLOPT_FAILONERROR => true,
        CURLOPT_URL => $url.$sUrl,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $fields_string,
        CURLOPT_HEADER => false,                      // don't return headers
        CURLOPT_SSL_VERIFYPEER => false,              // TODO: fix certificate provider list problem later
        CURLOPT_CONNECTTIMEOUT => 6,                  // timeout on connect
        CURLOPT_TIMEOUT => 10  );                      // timeout on response

    curl_setopt_array ( $ch, $options );
    try {
        $result=curl_exec( $ch );
        $log->LogDebug($result);
        //$header = curl_getinfo ( $ch );
        $err = curl_errno ( $ch );
        $errmsg = curl_error ( $ch );
        curl_close($ch);
        if ($errmsg) throw new Exception($errmsg);
        return $result;
     } catch (Exception $e) {  throw $e;  }

}

example invoice from paypal:

{
    "merchant_info": {
    "email": "dennis@sample.com",
        "first_name": "Dennis",
        "last_name": "Doctor",
        "business_name": "Medical Professionals, LLC",
        "phone": {
        "country_code": "001",
            "national_number": "5032141716"
    },
    "address": {
        "line1": "1234 Main St.",
            "city": "Portland",
            "state": "OR",
            "postal_code": "97217",
            "country_code": "US"
    }
},
    "billing_info": [
    {
        "email": "example@example.com"
    }
],
    "items": [
    {
        "name": "Sutures",
        "quantity": 100,
        "unit_price": {
            "currency": "USD",
            "value": 5
        }
    }
],
    "note": "Medical Invoice 16 Jul, 2013 PST",
    "payment_term" :{
    "term_type": "NET_45"
},
    "shipping_info": {
    "first_name": "Sally",
        "last_name": "Patient",
        "business_name": "Not applicable",
        "address": {
        "line1": "1234 Broad St.",
            "city": "Portland",
            "state": "OR",
            "postal_code": "97216",
            "country_code": "US"
        }
    }
}
Était-ce utile?

La solution

I discovered the problem:

From paypal's invoice documentation:

Note: The merchant specified in an invoice must have a PayPal account in good standing.

Evidently the sample account "dennis@sample.com" should not be used. When I used my own sandbox account for merchant information everything magically worked...

Perhaps someone else might have this problem and the solution is now published. Maybe my sample code might be of assistance as well...

Autres conseils

I am unable to find examples that show the use of Accept/Accept-Language in the PayPal REST documents.

$accept = "Accept:application/json";
$acceptlang = "Accept-Language:en_US";

I would remove these from your header and see if this changes the error you are getting.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top