Making Paypal payment using REST API in PHP

14,721

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.

Share:
14,721
Guesser
Author by

Guesser

Updated on June 19, 2022

Comments

  • Guesser
    Guesser almost 2 years

    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);
    }
    
  • RevNoah
    RevNoah over 10 years
    Is this a complete solution? I'm not getting a response. I changed the authorization to the type and access token retrieved from code very similar to that in the first example.
  • Jackson
    Jackson over 7 years
    It's giving Error: No response.
  • Shalini
    Shalini over 7 years
    It's giving only 0 in response...please suggest for correct code
  • Emastmagy MastMagy
    Emastmagy MastMagy over 7 years
    Result '0' means you're good to go. Check your sandbox, you should see a transaction posted. You can also use curl_exec and get the result. You can access the following RESULT,PNREF,RESPMSG,AUTHCODE
  • Catluc
    Catluc over 7 years
    For those who are getting error: No response. If u are using and old php version it wont work because of the OpenSSL handshake problems. I just test with php 5.3.x and it wont work switched to PHP Version 5.6.28 and it works bacause it has a more recent OpenSSL version........Even if u use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); will still not work
  • Jackson
    Jackson about 7 years
    Works for me. Thanks
  • IanS
    IanS about 7 years
    Using this example, I got a weird error (NSS: client certificate not found (nickname not specified), as though the access token was missing. Removing the Content-length header fixed if for me.