// session_start(); // ### User Consent Response // PayPal would redirect the user to the redirect_uri mentioned when creating the consent URL. // The user would then able to retrieve the access token by getting the code, which is returned as a GET parameter. if (isset($_GET['code'])) { $code = $_GET['code']; try { // Obtain Authorization Code from Code, Client ID and Client Secret $accessToken = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $code), null, null, $paypalExtension->getApiContext()); } catch (PayPalConnectionException $ex) { // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY // ResultPrinter::printError("Obtained Access Token", "Access Token", null, $_GET['code'], $ex); var_dump($ex); exit(1); } $cookie = new Cookie('paypal', TWO_WEEKS, __SYM_COOKIE_PATH__, null, true); $cookie->set('token', $accessToken->getAccessToken()); $cookie->set('refresh-token', $accessToken->getRefreshToken()); $params = array('access_token' => $accessToken->getAccessToken()); $userInfo = OpenIdUserinfo::getUserinfo($params, $paypalExtension->getApiContext()); $userAddress = $userInfo->getAddress(); $address = array('street' => $userAddress->getStreetAddress(), 'locality' => $userAddress->getLocality(), 'region' => $userAddress->getRegion(), 'country' => $userAddress->getCountry(), 'postal-code' => $userAddress->getPostalCode()); $user = array('id' => $userInfo->getUserId(), 'name' => $userInfo->getGivenName(), 'surname' => $userInfo->getFamilyName(), 'email' => $userInfo->getEmail(), 'date-of-birth' => $userInfo->getBirthday(), 'mobile' => $userInfo->getPhoneNumber(), 'address' => $address); $cookie->set('user', $user); $cookie->set('address', $address); //TODO Consider if it's worth inserting the data into the database at this stage / updating existing records // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY // ResultPrinter::printResult("Obtained Access Token", "Access Token", $accessToken->getAccessToken(), $_GET['code'], $accessToken); } echo "<script>\n\twindow.opener.location.reload();\n\twindow.close();\n</script>"; die;
public function showPaypal() { $api_context = PayPalHelper::getApiContext(); try { $params = array('access_token' => PayPalHelper::generateAccessTokenFromRefreshToken(Auth::user()->paypal_key)); $user = OpenIdUserinfo::getUserinfo($params, $api_context); } catch (Exception $ex) { print "no pp key"; } /* // Create a new instance of Plan object $plan = new Plan(); // # Basic Information // Fill up the basic information that is required for the plan $plan->setName('Welltakeyourmoney') ->setDescription('If you register we can take all your money.') ->setType('fixed'); $paymentDefinition = new PaymentDefinition(); // The possible values for such setters are mentioned in the setter method documentation. // Just open the class file. e.g. lib/PayPal/Api/PaymentDefinition.php and look for setFrequency method. // You should be able to see the acceptable values in the comments. $paymentDefinition->setName('Regular Payments') ->setType('REGULAR') ->setFrequency('Month') ->setFrequencyInterval("5") ->setCycles("12") ->setAmount(new Currency(array('value' => 999, 'currency' => 'USD'))); // ### Create Plan $merchantPreferences = new MerchantPreferences(); $merchantPreferences->setReturnUrl(route('auth.dashboard')) ->setCancelUrl(route('auth.dashboard')) ->setAutoBillAmount("yes") ->setInitialFailAmountAction("CONTINUE") ->setMaxFailAttempts("0") ->setSetupFee(new Currency(array('value' => 9999, 'currency' => 'USD'))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences); $request = clone $plan; try { $output = $plan->create($api_context); } catch (PayPal\Exception\PPConnectionException $ex) { echo '<pre>';print_r(json_decode($ex->getData())); exit(1); } */ try { $params = array('page_size' => '20'); $planList = Plan::all($params, $api_context); } catch (PayPal\Exception\ConnectionException $ex) { echo '<pre>'; print_r(json_decode($ex->getData())); exit(1); } Log::info($planList); try { $plan = Plan::get("P-0TA38541GG196850X3XYQ2KI", $api_context); } catch (PayPal\Exception\ConnectionException $ex) { echo '<pre>'; print_r(json_decode($ex->getData())); exit(1); } return View::make('dev.paypal', array('output' => $plan)); }
<?php // ### Obtain Access Token From Refresh Token require __DIR__ . '/../bootstrap.php'; use PayPal\Api\OpenIdUserinfo; use PayPal\Api\OpenIdTokeninfo; // To obtain User Info, you have to follow three steps in general. // First, you need to obtain user's consent to retrieve the information you want. // This is explained in the example "ObtainUserConsent.php". // Once you get the user's consent, the end result would be long lived refresh token. // This refresh token should be stored in a permanent storage for later use. // Lastly, when you need to retrieve the user information, you need to generate the short lived access token // to retreive the information. The short lived access token can be retrieved using the example shown in // "GenerateAccessTokenFromRefreshToken.php", or as shown below // You can retrieve the refresh token by executing ObtainUserConsent.php and store the refresh token $refreshToken = 'W1JmxG-Cogm-4aSc5Vlen37XaQTj74aQcQiTtXax5UgY7M_AJ--kLX8xNVk8LtCpmueFfcYlRK6UgQLJ-XHsxpw6kZzPpKKccRQeC4z2ldTMfXdIWajZ6CHuebs'; try { $tokenInfo = new OpenIdTokeninfo(); $tokenInfo = $tokenInfo->createFromRefreshToken(array('refresh_token' => $refreshToken), $apiContext); $params = array('access_token' => $tokenInfo->getAccessToken()); $userInfo = OpenIdUserinfo::getUserinfo($params, $apiContext); } catch (Exception $ex) { ResultPrinter::printError("User Information", "User Info", null, $params, $ex); exit(1); } ResultPrinter::printResult("User Information", "User Info", $userInfo->getUserId(), $params, $userInfo);
private function refreshToken() { $refreshToken = 'W1JmxG-Cogm-4aSc5Vlen37XaQTj74aQcQiTtXax5UgY7M_AJ--kLX8xNVk8LtCpmueFfcYlRK6UgQLJ-XHsxpw6kZzPpKKccRQeC4z2ldTMfXdIWajZ6CHuebs'; try { $tokenInfo = new OpenIdTokeninfo(); $tokenInfo = $tokenInfo->createFromRefreshToken(array('refresh_token' => $refreshToken), $apiContext); $params = array('access_token' => $tokenInfo->getAccessToken()); $userInfo = OpenIdUserinfo::getUserinfo($params, $apiContext); } catch (Exception $ex) { ResultPrinter::printError("User Information", "User Info", null, $params, $ex); exit(1); } }
$scope = array('openid', 'email'); #More info: https://developer.paypal.com/docs/integration/direct/identity/attributes/ ####################################################### error_reporting(E_ALL); ini_set('display_errors', 1); require_once dirname(__FILE__) . '/paypal-sdk/autoload.php'; use PayPal\Api\OpenIdSession; use PayPal\Api\OpenIdTokeninfo; use PayPal\Api\OpenIdUserinfo; use PayPal\Rest\ApiContext; use PayPal\Auth\OAuthTokenCredential; ####################################################### #Step 1: display link to paypal if (!isset($_GET['code']) and !isset($_GET['error'])) { $openidurl = OpenIdSession::getAuthorizationUrl($app_return_url, $scope, $paypal_client_id); echo 'Click here to <a href="https://www.paypal.com/webapps/auth/protocol/openidconnect' . $openidurl . '">log in with paypal</a>!'; die; } ####################################################### #Check return data for errors if (isset($_GET['error'])) { die('<h1>Error:</h1><pre>' . print_r($_GET, TRUE)); } ####################################################### #Step 2: get user data $api_context = new ApiContext(new OAuthTokenCredential($paypal_client_id, $paypal_secret)); $api_context->setConfig(array('mode' => 'live')); $token = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $_GET['code']), $paypal_client_id, $paypal_secret, $api_context); $user = OpenIdUserinfo::getUserinfo(json_decode($token, true), $api_context); $user = json_decode($user, true); die('<h1>Success!</h1><p>User data:</p><pre>' . print_r($user, true) . '</pre>');
/** * @test */ public function testInvalidParamUserInfoCall() { $this->setExpectedException('PayPal\\Exception\\PayPalConnectionException'); OpenIdUserinfo::getUserinfo(array('access_token' => 'accessToken')); }