James G.
James G.

Reputation: 2904

Paypal API, Curl not returning any output

I'm trying to figure out the Paypal API, and I have the following code, which should make a call, get an access token, and then make the API call. The first part works (up until the $accesstoken line), and returns the access token properly, but the second part doesn't return anything. The code this is supposed to mimic can be found here: Make Your First Call

$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$headers = array(
    'Accept' => 'application/json',
    'Accept-Language' => 'en_US',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret);
$curl = curl_exec($ch);

$x = json_decode($curl, TRUE);
print_r($x);
$accesstoken = $x['access_token'];


$headers2 = array(
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer' . $accesstoken
);

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer" => array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(
        "transactions" => array(
            "total" => ".99",
            "currency" => "USD"
        )
    )
);

$saleurl = "https://api.sandbox.paypal.com/v1/payments/payment";

$sale = curl_init();
curl_setopt($sale, CURLOPT_URL, $saleurl);
curl_setopt($sale, CURLOPT_VERBOSE, TRUE);
curl_setopt($sale, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($sale, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($sale, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($sale, CURLOPT_HTTPHEADER, $headers2);
$finalsale = curl_exec($sale);

$verb = json_decode($finalsale, TRUE);
print_r($verb);

Curl doesn't make complete sense to me, any help would be appreciated.

UPDATE: I changed the format of the headers to:

$headers2 = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accesstoken
);

as per one of the answers. Now it is displaying:

[name] => MALFORMED_REQUEST
[message] => Incoming JSON request does not map to API request
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST
[debug_id] => f53a882702a04

Upvotes: 1

Views: 2407

Answers (3)

hellozt
hellozt

Reputation: 1

Change curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));

to curl_setopt($sale, CURLOPT_POSTFIELDS, $data);

The json encode function breaks it

Upvotes: 0

cmorrissey
cmorrissey

Reputation: 8583

You are not setting your headers correctly ...

$headers = array(
    'Accept: application/json',
    'Accept-Language: en_US'
);

and

$headers2 = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accesstoken
);

Is the correct format.

Also note the (space) after Bearer inbetween your $accesstoken

Edit: Update for your JSON ( i think this is right but echo it out and check it against the reference, I might have one to many array()

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer" => array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(array(
            "amount" => array(
                "total" => ".99",
                "currency" => "USD"
            )
        )
    )
);

Upvotes: 2

Machavity
Machavity

Reputation: 31644

You need a space here

$headers2 = array(
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . $accesstoken // Added a space after Bearer
);

See if it works now

Upvotes: 1

Related Questions