Login with PayPal Using PHP

Dec 25, 2018

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>

// add scrpt
paypal.use(['login'], function (login) {
  "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);
$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);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
curl_close ($ch);

Copyright 2018. All rights are reserved