romeritoCL /
paypal-playground
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Service\Paypal; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use PayPal\Api\OpenIdTokeninfo; |
||
| 7 | use PayPal\Api\OpenIdUserinfo; |
||
| 8 | use PayPal\Auth\OAuthTokenCredential; |
||
| 9 | use PayPal\Core\PayPalConfigManager; |
||
| 10 | use Symfony\Component\HttpFoundation\Response; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class IdentityService |
||
| 14 | * @package App\Service\Paypal |
||
| 15 | */ |
||
| 16 | class IdentityService extends AbstractPaypalService |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @return string|null |
||
| 20 | */ |
||
| 21 | public function getAccessToken(): ?string |
||
| 22 | { |
||
| 23 | $credential = new OAuthTokenCredential($this->clientId, $this->clientSecret); |
||
| 24 | $config = PayPalConfigManager::getInstance()->getConfigHashmap(); |
||
| 25 | return $credential->getAccessToken($config); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * getAccessTokenFromAuthToken |
||
| 30 | * |
||
| 31 | * @param $authToken |
||
| 32 | * @return OpenIdTokeninfo|null |
||
| 33 | */ |
||
| 34 | public function getAccessTokenFromAuthToken($authToken) : ?OpenIdTokeninfo |
||
| 35 | { |
||
| 36 | try { |
||
| 37 | $accessToken = OpenIdTokeninfo::createFromAuthorizationCode( |
||
| 38 | ['code' => $authToken], |
||
| 39 | $this->clientId, |
||
| 40 | $this->clientSecret, |
||
| 41 | $this->apiContext |
||
| 42 | ); |
||
| 43 | } catch (Exception $e) { |
||
| 44 | $this->logger->error('Error on PayPal::getAccessTokenFromAuthToken = ' . $e->getMessage()); |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | return $accessToken; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * refreshToken |
||
| 52 | * |
||
| 53 | * @param string $refreshToken |
||
| 54 | * @return OpenIdTokeninfo|null |
||
| 55 | */ |
||
| 56 | public function refreshToken(string $refreshToken) : ?OpenIdTokeninfo |
||
| 57 | { |
||
| 58 | try { |
||
| 59 | $tokenInfo = new OpenIdTokeninfo(); |
||
| 60 | $tokenInfo = $tokenInfo->createFromRefreshToken(['refresh_token' => $refreshToken], $this->apiContext); |
||
| 61 | } catch (Exception $e) { |
||
| 62 | $this->logger->error('Error on PayPal::refreshToken = ' . $e->getMessage()); |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 | return $tokenInfo; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param OpenIdTokeninfo $tokenInfo |
||
| 70 | * @return OpenIdUserinfo|null |
||
| 71 | */ |
||
| 72 | public function getUserInfo(OpenIdTokeninfo $tokenInfo) : ?OpenIdUserinfo |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $params = ['access_token' => $tokenInfo->getAccessToken()]; |
||
| 76 | $userInfo = OpenIdUserinfo::getUserinfo($params, $this->apiContext); |
||
| 77 | } catch (Exception $e) { |
||
| 78 | $this->logger->error('Error on PayPal::getUserInfo = ' . $e->getMessage()); |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | return $userInfo; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $refreshToken |
||
| 86 | * @return OpenIdUserinfo|null |
||
| 87 | */ |
||
| 88 | public function getUserInfoFromRefreshToken(string $refreshToken) : ?OpenIdUserinfo |
||
| 89 | { |
||
| 90 | try { |
||
| 91 | $tokenInfo = $this->refreshToken($refreshToken); |
||
| 92 | if ($tokenInfo) { |
||
| 93 | $userInfo = $this->getUserInfo($tokenInfo); |
||
| 94 | } |
||
| 95 | } catch (Exception $e) { |
||
| 96 | $this->logger->error('Error on PayPal::getUserInfoFromRefreshToken = ' . $e->getMessage()); |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | return $userInfo ?? null; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | * Get Client Token |
||
| 105 | * |
||
| 106 | * @return string|null |
||
| 107 | * @throws Exception |
||
| 108 | * |
||
| 109 | */ |
||
| 110 | public function getClientToken(): ?string |
||
| 111 | { |
||
| 112 | $accessToken = $this->getAccessToken(); |
||
| 113 | $response = $this->paypalApiCall($accessToken, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 114 | [], |
||
| 115 | 'https://api.sandbox.paypal.com/v1/identity/generate-token' |
||
| 116 | ); |
||
| 117 | if (array_key_exists('statusCode',$response) && $response['statusCode'] == Response::HTTP_OK) { |
||
| 118 | $result = (object) json_decode($response['result'],true); |
||
| 119 | return $result->client_token; |
||
| 120 | } else { |
||
| 121 | $this->logger->error('Error on PayPal::getClientToken'); |
||
| 122 | throw new \Exception('Error on PayPal::getClientToken'); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 |