Login with PayPal Using PHP

Login with PayPal Using PHP

Dec 25, 2018 / PHP /

One of the most important tasks you needs to do PayPal Auth using php. I am going to show you how you can easily implement PayPal login in PHP using PayPal sandbox account.

Step 1:

Find PayPal app ID and secret key from PayPal valid account.

Login to you PayPal account ( https://developer.paypal.com/ )

 

Step 2: Click on create App button

 

Step 3: Now add App name and also select your business account and click on Create App button.
 

 

Step 4: App Id or Client Id and App Secret will appear like this 

 

 

Step 5: Click on checkbox button for login with PayPal

 

Step 6: Click on all checkbox button for get all scopes -  login with PayPal

Step 7: Get the sandbox account for testing

 

Step 8: Create PayPal login button

 


<div id="paypal_button">
<span id="lippButton"></span>
</div>

 


// add scrpt
https://www.paypalobjects.com/js/external/api.js
paypal.use(['login'], function (login) {
  login.render({
  "appid": "your-client-id",
  "authend": "sandbox",
  "scopes": "openid",
  "containerid": "lippButton",
  "locale": "en-us",
  "returnurl": "http://localhost/projects/demo/paypalAuth/result.php"
  });
});

Step 9: Implement PHP Code

 


header('Content-Type: application/json');
$ch = curl_init();
$code = $_GET['code'];
 
$access = "your-client-id:your-secret-key";
$ac = base64_encode($access);
 
 
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code;=$code");
curl_setopt($ch, CURLOPT_POST, 1);
 
$headers = array();
$headers[] = "Authorization: Basic $ac";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
$result = curl_exec($ch);
//print_r($result);
$resultss = json_decode($result);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
 
 
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token/userinfo?schema=openid&scope=email");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
 
 
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer $resultss->access_token";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
exit;

Comments

Sujan Shrestha ( May 6, 2019 04:36:19 )

Hi, I am testing on localhost and am facing this error:- "Error:Protocol "ttps" not supported or disabled in libcurl"

Mohammad ( Jan 13, 2019 05:12:20 )

Thank for save my time...

Would you like to share your thoughts?

Your email address will not be published. Required field are marked*

Copyright 2018. All rights are reserved Significant Techno Web Design & Web Development By Significant Techno