Paypal.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. /**
  11. * PayPal service.
  12. *
  13. * @author Flávio Heleno <flaviohbatista@gmail.com>
  14. * @link https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/
  15. */
  16. class Paypal extends AbstractService
  17. {
  18. /**
  19. * Defined scopes
  20. * @link https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/
  21. * @see #attributes
  22. */
  23. const SCOPE_OPENID = 'openid';
  24. const SCOPE_PROFILE = 'profile';
  25. const SCOPE_PAYPALATTRIBUTES = 'https://uri.paypal.com/services/paypalattributes';
  26. const SCOPE_EMAIL = 'email';
  27. const SCOPE_ADDRESS = 'address';
  28. const SCOPE_PHONE = 'phone';
  29. const SCOPE_EXPRESSCHECKOUT = 'https://uri.paypal.com/services/expresscheckout';
  30. public function __construct(
  31. CredentialsInterface $credentials,
  32. ClientInterface $httpClient,
  33. TokenStorageInterface $storage,
  34. $scopes = array(),
  35. UriInterface $baseApiUri = null
  36. ) {
  37. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  38. if (null === $baseApiUri) {
  39. $this->baseApiUri = new Uri('https://api.paypal.com/v1/');
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getAuthorizationEndpoint()
  46. {
  47. return new Uri('https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize');
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getAccessTokenEndpoint()
  53. {
  54. return new Uri('https://api.paypal.com/v1/identity/openidconnect/tokenservice');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function getAuthorizationMethod()
  60. {
  61. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function parseAccessTokenResponse($responseBody)
  67. {
  68. $data = json_decode($responseBody, true);
  69. if (null === $data || !is_array($data)) {
  70. throw new TokenResponseException('Unable to parse response.');
  71. } elseif (isset($data['message'])) {
  72. throw new TokenResponseException('Error in retrieving token: "' . $data['message'] . '"');
  73. } elseif (isset($data['name'])) {
  74. throw new TokenResponseException('Error in retrieving token: "' . $data['name'] . '"');
  75. }
  76. $token = new StdOAuth2Token();
  77. $token->setAccessToken($data['access_token']);
  78. $token->setLifeTime($data['expires_in']);
  79. if (isset($data['refresh_token'])) {
  80. $token->setRefreshToken($data['refresh_token']);
  81. unset($data['refresh_token']);
  82. }
  83. unset($data['access_token']);
  84. unset($data['expires_in']);
  85. $token->setExtraParams($data);
  86. return $token;
  87. }
  88. }