1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package MerchantSafeUnipay\SDK |
5
|
|
|
* @author Mehmet Korkmaz <[email protected]> |
6
|
|
|
* @licence https://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* Documentation can be found at https://merchantsafeunipay.com/msu/api/v2/doc |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MerchantSafeUnipay\SDK; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp; |
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
15
|
|
|
use MerchantSafeUnipay\SDK\Environment\EnvironmentInterface as Environment; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use MerchantSafeUnipay\SDK\Action\ActionInterface; |
19
|
|
|
use Psr7\Http\Message\RequestInterface; |
20
|
|
|
use MerchantSafeUnipay\SDK\Exception\InvalidArgumentException; |
21
|
|
|
use MerchantSafeUnipay\SDK\Exception\BadMethodCallException; |
22
|
|
|
use MerchantSafeUnipay\SDK\Exception\RequestException; |
23
|
|
|
|
24
|
|
|
class Client |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
const MSU_API_VERSION = 2; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $validActions = [ |
35
|
|
|
|
36
|
|
|
'session', |
37
|
|
|
'financialTransactions', |
38
|
|
|
'approveActions', |
39
|
|
|
'rejectActions', |
40
|
|
|
'dealer', |
41
|
|
|
'dealerPST', |
42
|
|
|
'dealerType', |
43
|
|
|
'eWallet', |
44
|
|
|
'merchant', |
45
|
|
|
'merchantUser', |
46
|
|
|
'merchantContent', |
47
|
|
|
'messageContent', |
48
|
|
|
'payByLinkPayment', |
49
|
|
|
'paymentPolicy', |
50
|
|
|
'paymentSystem', |
51
|
|
|
'paymentType', |
52
|
|
|
'recurringPayment', |
53
|
|
|
'recurringPlan', |
54
|
|
|
'recurringPlanCard', |
55
|
|
|
'query', |
56
|
|
|
|
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Environment |
61
|
|
|
*/ |
62
|
|
|
private $environment; |
63
|
|
|
/** |
64
|
|
|
* @var GuzzleClient |
65
|
|
|
*/ |
66
|
|
|
private $guzzleClient; |
67
|
|
|
|
68
|
|
|
private $logger; |
69
|
|
|
|
70
|
|
|
private static $headers = [ |
71
|
|
|
'User-Agent' => 'MerchantSafeUnipayPhpSDK/1.0', |
72
|
|
|
'Accept' => 'application/json' |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
private static $merchantParams = [ |
|
|
|
|
76
|
|
|
'MERCHANT' => null, |
77
|
|
|
'MERCHANTUSER' => null, |
78
|
|
|
'MERCHANTPASSWORD' => null |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private static $possibleResponses = [ |
|
|
|
|
85
|
|
|
'00' => 'Approved', |
86
|
|
|
'01' => 'Waiting for Approval', |
87
|
|
|
'98' => 'General Error', |
88
|
|
|
'99' => 'Declined' |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Client constructor. |
93
|
|
|
* @param Environment $environment |
94
|
|
|
* @param GuzzleClient $guzzleClient |
95
|
|
|
* @param LoggerInterface $logger |
96
|
|
|
*/ |
97
|
|
|
public function __construct(Environment $environment, GuzzleClient $guzzleClient, LoggerInterface $logger) |
98
|
|
|
{ |
99
|
|
|
$this->environment = $environment; |
100
|
|
|
$this->guzzleClient = $guzzleClient; |
101
|
|
|
$this->logger = $logger; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $name |
106
|
|
|
* @param $arguments |
107
|
|
|
* @throws BadMethodCallException |
108
|
|
|
* @throws RequestException |
109
|
|
|
* @throws InvalidArgumentException |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function __call(string $name, array $arguments) |
113
|
|
|
{ |
114
|
|
|
$action = $this->getAction($name, $arguments); |
115
|
|
|
$headers = array_merge(self::$headers, $action->getHeaders()); |
116
|
|
|
$response = $this->httpRequest($action->getAction(), $headers, $action->getQueryParams()); |
117
|
|
|
|
118
|
|
|
return [ |
119
|
|
|
'status' => $response->getStatusCode(), |
120
|
|
|
'reason' => $response->getReasonPhrase(), |
121
|
|
|
'headers' => $response->getHeaders(), |
122
|
|
|
'data' => json_decode((string) $response->getBody(), true) |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @param array $arguments |
129
|
|
|
* @return ActionInterface |
130
|
|
|
* @throws BadMethodCallException |
131
|
|
|
* @throws InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
private function getAction(string $name, array $arguments) |
134
|
|
|
{ |
135
|
|
|
$actionClass = '\\MerchantSafeUnipay\\SDK\\Action\\'. ucfirst($name); |
136
|
|
|
|
137
|
|
|
if (!in_array($name, self::$validActions, true) || !class_exists($actionClass)) { |
138
|
|
|
$message = sprintf('%s is not valid MerchantSafeUnipay API action.', $name); |
139
|
|
|
throw new BadMethodCallException($message); |
140
|
|
|
} |
141
|
|
|
$actionName = $arguments[0]; |
142
|
|
|
$actionObject = new $actionClass($this->environment->getMerchantData()); |
143
|
|
|
if (!method_exists($actionObject, $actionName)) { |
144
|
|
|
$message = sprintf( |
145
|
|
|
'%s/%s is not valid MerchantSafeUnipay API action.', |
146
|
|
|
ucfirst($name), |
147
|
|
|
ucfirst($actionName) |
148
|
|
|
); |
149
|
|
|
throw new BadMethodCallException($message); |
150
|
|
|
} |
151
|
|
|
try { |
152
|
|
|
$actionObject->$actionName($arguments[1]); |
153
|
|
|
return $actionObject; |
154
|
|
|
} catch (TypeError $e) { |
|
|
|
|
155
|
|
|
$message = 'This action needs arguments, no argument provided.'; |
156
|
|
|
throw new InvalidArgumentException($message); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $actionName |
162
|
|
|
* @param array $headers |
163
|
|
|
* @param array $queryParams |
164
|
|
|
* @throws RequestException |
165
|
|
|
* @return ResponseInterface |
166
|
|
|
*/ |
167
|
|
|
private function httpRequest(string $actionName, array $headers, array $queryParams) |
168
|
|
|
{ |
169
|
|
|
$uri = $this->environment->getUrl(); |
170
|
|
|
$queryParams['ACTION'] = $actionName; |
171
|
|
|
$options = [ |
172
|
|
|
'headers' => $headers, |
173
|
|
|
'form_params' => $queryParams |
174
|
|
|
]; |
175
|
|
|
try { |
176
|
|
|
return $this->guzzleClient->post($uri, $options); |
177
|
|
|
} catch (Exception $e) { |
|
|
|
|
178
|
|
|
$message = $e->getMessage(); |
179
|
|
|
} |
180
|
|
|
$message = sprintf('MerchantSafe Unipay API Request Error:% s', $message); |
181
|
|
|
throw new RequestException($message); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.