Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Paypal payment using REST API in PHP

Tags:

php

curl

paypal

I'm not clear how to put this code example into PHP's CURL structure, specifically, the -d handle.

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK' \
-d '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://<return URL here>",
    "cancel_url":"http://<cancel URL here>"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      },
      "description":"This is the payment transaction description."
    }
  ]
}'

I've used the following test call ok, but don't know how to extend that to the example above.

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json->access_token);
}
like image 577
Guesser Avatar asked Apr 30 '13 23:04

Guesser


People also ask

How do I set up PayPal REST API?

To get started with the PayPal REST API, first create a developer account on the Developer Dashboard. From there you can generate your credentials, authentication token and sandbox accounts.

How do I get my PayPal REST API access token?

Your access token authorizes you to use the PayPal REST API server. To call a REST API in your integration, you must exchange your client ID and secret for an access token. You can find your client ID and secret by logging in to the Developer Dashboard. You can make the API call in any programming language.


1 Answers

Try something like this, -d is the data you want to post. custom headers are set with CURLOPT_HTTPHEADER.

$data = '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://<return URL here>",
    "cancel_url":"http://<cancel URL here>"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      },
      "description":"This is the payment transaction description."
    }
  ]
}';

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Authorization: Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK", 
  "Content-length: ".strlen($data))
);

Have a look at the options here, http://php.net/manual/en/function.curl-setopt.php Set curl_setopt($ch, CURLOPT_HEADER, false); for example if you dont need the headers returned.

like image 87
shapeshifter Avatar answered Oct 18 '22 18:10

shapeshifter