This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathConnection.php
More file actions
executable file
·204 lines (177 loc) · 7.09 KB
/
Copy pathConnection.php
File metadata and controls
executable file
·204 lines (177 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Worldpay;
use yii\helpers\VarDumper;
class Connection {
private $service_key = "";
private $timeout = 65;
private $ssl_check = true;
private $endpoint = 'https://api.worldpay.com/v1/';
private $client_user_agent = "";
private function __construct()
{
}
/**
* Call this method to get singleton
*
* @return Connection
*/
public static function getInstance()
{
if (!function_exists("curl_init")) {
Error::throwError("cine");
}
static $inst = null;
if ($inst === null) {
$inst = new Connection();
$inst->client_user_agent = $inst->getBaseClientUserAgent();
}
return $inst;
}
public function setServiceKey($serviceKey)
{
$this->service_key = $serviceKey;
}
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
}
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
public function setSSLCheck($ssl)
{
$this->ssl_check = $ssl;
}
private function getBaseClientUserAgent()
{
$arch = (bool)((1<<32)-1) ? 'x64' : 'x86';
$clientUA = 'os.name=' . php_uname('s') . ';os.version=' . php_uname('r') . ';os.arch=' .
$arch . ';lang.version='. phpversion() . ';lib.version=2.1.0;' . 'api.version=v1;lang=php;owner=worldpay';
return $clientUA;
}
public function setClientUserAgentWithPluginData($pluginName, $pluginVersion)
{
$this->client_user_agent = $this->getBaseClientUserAgent();
if ($pluginName) {
$this->client_user_agent .= ';plugin.name=' . $pluginName;
}
if ($pluginVersion) {
$this->client_user_agent .= ';plugin.version=' . $pluginVersion;
}
}
/**
* Sends request to Worldpay API
* @param string $action
* @param string $json
* @param bool $expectResponse
* @param string $method
* @return string JSON string from Worldpay
* */
public function sendRequest($action, $json = false, $expectResponse = false, $method = 'POST', $debug=false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->endpoint.$action);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
"Authorization: $this->service_key",
"Content-Type: application/json",
"X-wp-client-user-agent: $this->client_user_agent",
"Content-Length: " . strlen($json)
)
);
// Disabling SSL used for localhost testing
if ($this->ssl_check === false) {
if (substr($this->service_key, 0, 1) != 'T') {
Error::throwError('ssl');
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$result = curl_exec($ch);
$info = curl_getinfo($ch);;
$err = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
$transactionId = self::handleResponse($json);
$transactionId = isset($transactionId['customerOrderCode']) ? $transactionId['customerOrderCode'] : '00';
$file = (\Yii::$app->basePath . '/archivos/');
if($debug){
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($result, true));
}
// Curl error
if ($result === false) {
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($err, true));
if ($errno === 60) {
Error::throwError('sslerror', false, $errno, null, $err);
} elseif ($errno === 28) {
Error::throwError('timeouterror', false, $errno, null, $err);
} else {
Error::throwError('uanv', false, $errno, null, $err);
}
}
if (substr($result, -1) != '}') {
$result = substr($result, 0, -1);
}
// Decode JSON
$response = self::handleResponse($result);
// Check JSON has decoded correctly
if ($expectResponse && ($response === null || $response === false )) {
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($err, true));
Error::throwError('uanv', Error::$errors['json'], 503);
}
// Check the status code exists
if (isset($response["httpStatusCode"])) {
if ($response["httpStatusCode"] != 200) {
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($response, true));
Error::throwError(
false,
$response["message"],
$info['http_code'],
$response['httpStatusCode'],
$response['description'],
$response['customCode']
);
}
} elseif ($expectResponse && $info['http_code'] != 200) {
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($info, true));
// If we expect a result and we have an error
Error::throwError('uanv', Error::$errors['json'], 503);
} elseif (!$expectResponse) {
if ($info['http_code'] != 200) {
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($result, true));
Error::throwError('apierror', $result, $info['http_code']);
} else {
$response = true;
}
}
if ($response['paymentStatus'] != 'SUCCESS' && $response['paymentStatus'] != 'AUTHORIZED') {
$file = (\Yii::$app->basePath . '/archivos/');
file_put_contents($file . "Request-$transactionId-'worldpay.json", print_r($json, true));
file_put_contents($file . "ResponseH-$transactionId-worlpay.json", print_r($result, true));
}
return $response;
}
/**
* Handle response object
* @param string $response
* */
private static function handleResponse($response)
{
return json_decode($response, true);
}
}