|
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
|
|
|
|
|
14
|
|
|
public function __construct(string $environment = null){ |
|
15
|
|
|
|
|
16
|
|
|
if($environment===null){ |
|
17
|
|
|
$config = new Config(); |
|
18
|
|
|
$environment = $config->getEnvConfig('defaultProvider'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
if ($environment !== 'vivaDEMO' && $environment !== 'vivaPROD') { |
|
22
|
|
|
throw new \InvalidArgumentException('Invalid environment value {'.$environment.'}. It should be either "vivaDEMO" or "vivaPROD".'); |
|
23
|
|
|
} |
|
24
|
|
|
$this->environment = $environment; |
|
25
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getAccessToken(): string |
|
29
|
|
|
{ |
|
30
|
|
|
$config = new Config($this->environment); |
|
31
|
|
|
$url = $config->getEnvConfig('VIVA_ACCOUNT_URL'); |
|
32
|
|
|
$curl = new CurlWrapper($url.'/connect/token'); |
|
33
|
|
|
$curl->addHeader('Content-Type: application/x-www-form-urlencoded'); |
|
34
|
|
|
$curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
|
35
|
|
|
$curl->addHeader('Accept: */*'); |
|
36
|
|
|
$curl->setBasicAuth($config->getEnvConfig('VIVA_CLIENT_ID'),$config->getEnvConfig('VIVA_CLIENT_SECRET')); |
|
37
|
|
|
$response = $curl->post(array('grant_type' => 'client_credentials')); |
|
38
|
|
|
$this->statusCode = $curl->getStatusCode(); |
|
39
|
|
|
$response = json_decode($response,true); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->accessToken = $response['access_token']; |
|
42
|
|
|
return $this->accessToken; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getWebhookValidationToken() : array |
|
46
|
|
|
{ |
|
47
|
|
|
$config = new Config($this->environment); |
|
48
|
|
|
$url = $config->getEnvConfig('VIVA_URL'); |
|
49
|
|
|
$curl = new CurlWrapper($url.'/api/messages/config/token'); |
|
50
|
|
|
$curl->addHeader('Content-Type: application/x-www-form-urlencoded'); |
|
51
|
|
|
$curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
|
52
|
|
|
$curl->addHeader('Accept: */*'); |
|
53
|
|
|
$curl->setBasicAuth($config->getEnvConfig('VIVA_MERCHANT_ID'),$config->getEnvConfig('VIVA_API_KEY')); |
|
54
|
|
|
|
|
55
|
|
|
$response = $curl->get(); |
|
56
|
|
|
|
|
57
|
|
|
$response = json_decode($response,true); |
|
|
|
|
|
|
58
|
|
|
return $response; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function getEnvironment(): string { |
|
64
|
|
|
return $this->environment; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function getToken(): string { |
|
69
|
|
|
return $this->accessToken; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
public function getStatusCode():int { |
|
74
|
|
|
return $this->statusCode; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function refresh(): self |
|
78
|
|
|
{ |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|