1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusQuadPayPlugin\Client; |
14
|
|
|
|
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
|
17
|
|
|
class QuadPayApiClient implements QuadPayApiClientInterface |
18
|
|
|
{ |
19
|
|
|
/** @var ClientInterface */ |
20
|
|
|
protected $apiClient; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $clientId; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $clientSecret; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $apiEndpoint; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $authTokenEndpoint; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $apiAudience; |
36
|
|
|
|
37
|
|
|
public function __construct(ClientInterface $client) |
38
|
|
|
{ |
39
|
|
|
$this->apiClient = $client; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setConfig( |
43
|
|
|
string $clientId, |
44
|
|
|
string $clientSecret, |
45
|
|
|
string $apiEndpoint, |
46
|
|
|
string $authTokenEndpoint, |
47
|
|
|
string $apiAudience |
48
|
|
|
): void { |
49
|
|
|
$this->clientId = $clientId; |
50
|
|
|
$this->clientSecret = $clientSecret; |
51
|
|
|
$this->apiEndpoint = $apiEndpoint; |
52
|
|
|
$this->authTokenEndpoint = $authTokenEndpoint; |
53
|
|
|
$this->apiAudience = $apiAudience; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getOrderUrl(?string $orderId = null, ?string $orderToken = null): string |
57
|
|
|
{ |
58
|
|
|
$url = sprintf('https://%s/order', parse_url($this->apiEndpoint)['host']); |
59
|
|
|
|
60
|
|
|
if (null !== $orderToken) { |
61
|
|
|
return sprintf('%s?token=%s', $url, $orderToken); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (null !== $orderId) { |
65
|
|
|
return sprintf('%s/%s', $url, $orderId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $url; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getRefundUrl(string $orderId): string |
72
|
|
|
{ |
73
|
|
|
return sprintf('https://%s/order/%s/refund', parse_url($this->apiEndpoint)['host'], $orderId); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getOauthTokenUrl(): string |
77
|
|
|
{ |
78
|
|
|
return sprintf('https://%s/oauth/token', parse_url($this->authTokenEndpoint)['host']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function createAccessToken(): array |
82
|
|
|
{ |
83
|
|
|
return $this->request('POST', $this->getOauthTokenUrl(), [ |
84
|
|
|
'client_id' => $this->clientId, |
85
|
|
|
'client_secret' => $this->clientSecret, |
86
|
|
|
'audience' => $this->apiAudience, |
87
|
|
|
'grant_type' => 'client_credentials', |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function createOrder(array $data): array |
92
|
|
|
{ |
93
|
|
|
return $this->request('POST', $this->getOrderUrl(), $data, $this->createAccessToken()['access_token']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getOrderByToken(string $orderToken): array |
97
|
|
|
{ |
98
|
|
|
return $this->request('GET', $this->getOrderUrl(null, $orderToken), [], $this->createAccessToken()['access_token']); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getOrderById(string $orderId): array |
102
|
|
|
{ |
103
|
|
|
return $this->request('GET', $this->getOrderUrl($orderId), [], $this->createAccessToken()['access_token']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function refund( |
107
|
|
|
float $amount, |
108
|
|
|
string $merchantRefundReference, |
109
|
|
|
string $orderToken, |
110
|
|
|
?string $orderId = null |
111
|
|
|
): array { |
112
|
|
|
if (null === $orderId) { |
113
|
|
|
$orderId = $this->getOrderByToken($orderToken)['orderId']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$data = [ |
117
|
|
|
'amount' => $amount, |
118
|
|
|
'merchantRefundReference' => $merchantRefundReference, |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
return $this->request('POST', $this->getRefundUrl($orderId), $data, $this->createAccessToken()['access_token']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getHeaders(?string $accessToken = null): array |
125
|
|
|
{ |
126
|
|
|
$headers = [ |
127
|
|
|
'Content-Type' => 'application/json', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
if (null !== $accessToken) { |
131
|
|
|
$headers['Authorization'] = sprintf('Bearer %s', $accessToken); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $headers; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function request(string $method, string $url, array $data = [], ?string $accessToken = null): array |
138
|
|
|
{ |
139
|
|
|
$options = [ |
140
|
|
|
'json' => $data, |
141
|
|
|
'headers' => $this->getHeaders($accessToken), |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$result = $this->apiClient->request($method, $url, $options); |
145
|
|
|
|
146
|
|
|
return \GuzzleHttp\json_decode((string) $result->getBody(), true); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|