stadem /
viva-payments
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Stadem\VivaPayments\Services; |
||||
| 4 | use Stadem\VivaPayments\Config\Config; |
||||
| 5 | use Stadem\VivaPayments\Services\CurlWrapper; |
||||
| 6 | |||||
| 7 | class AccessToken |
||||
| 8 | { |
||||
| 9 | |||||
| 10 | private $accessToken; |
||||
| 11 | private $environment; |
||||
| 12 | private $statusCode; |
||||
| 13 | public Config|Array $configObject; |
||||
| 14 | |||||
| 15 | public function __construct(Config|array $configObject){ |
||||
| 16 | |||||
| 17 | |||||
| 18 | $this->configObject=$configObject; |
||||
| 19 | $configArray = $configObject->getConfig(); |
||||
| 20 | $environment= $configArray['defaultProvider']; |
||||
| 21 | |||||
| 22 | if ($environment !== 'vivaDEMO' && $environment !== 'vivaPROD') { |
||||
| 23 | throw new \InvalidArgumentException('Invalid environment value {'.$environment.'}. It should be either "vivaDEMO" or "vivaPROD".'); |
||||
| 24 | } |
||||
| 25 | $this->environment = $environment; |
||||
| 26 | |||||
| 27 | } |
||||
| 28 | |||||
| 29 | public function getAccessToken(): string |
||||
| 30 | { |
||||
| 31 | |||||
| 32 | $config = $this->configObject; |
||||
| 33 | $url = $config->getEnvConfig('VIVA_ACCOUNT_URL'); |
||||
| 34 | $curl = new CurlWrapper($url.'/connect/token'); |
||||
| 35 | $curl->addHeader('Content-Type: application/x-www-form-urlencoded'); |
||||
| 36 | $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
||||
| 37 | $curl->addHeader('Accept: */*'); |
||||
| 38 | $curl->setBasicAuth($config->getEnvConfig('VIVA_CLIENT_ID'),$config->getEnvConfig('VIVA_CLIENT_SECRET')); |
||||
| 39 | $response = $curl->post(array('grant_type' => 'client_credentials')); |
||||
| 40 | $this->statusCode = $curl->getStatusCode(); |
||||
| 41 | $response = json_decode($response,true); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 42 | |||||
| 43 | $this->accessToken = $response['access_token']; |
||||
| 44 | return $this->accessToken; |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | public function getWebhookValidationToken() : array |
||||
| 48 | { |
||||
| 49 | $config = $this->configObject; |
||||
| 50 | $url = $config->getEnvConfig('VIVA_URL'); |
||||
| 51 | $curl = new CurlWrapper($url.'/api/messages/config/token'); |
||||
| 52 | $curl->addHeader('Content-Type: application/x-www-form-urlencoded'); |
||||
| 53 | $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
||||
| 54 | $curl->addHeader('Accept: */*'); |
||||
| 55 | $curl->setBasicAuth($config->getEnvConfig('VIVA_MERCHANT_ID'),$config->getEnvConfig('VIVA_API_KEY')); |
||||
| 56 | |||||
| 57 | $response = $curl->get(); |
||||
| 58 | |||||
| 59 | $response = json_decode($response,true); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 60 | return $response; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | |||||
| 64 | |||||
| 65 | public function getEnvironment(): string { |
||||
| 66 | return $this->environment; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | |||||
| 70 | public function getToken(): string { |
||||
| 71 | return $this->accessToken; |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | |||||
| 75 | public function getStatusCode():int { |
||||
| 76 | return $this->statusCode; |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | public function refresh(): self |
||||
| 80 | { |
||||
| 81 | return $this; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | |||||
| 85 | } |
||||
| 86 |