forked from yabacon/paystack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
140 lines (126 loc) · 4.61 KB
/
Request.php
File metadata and controls
140 lines (126 loc) · 4.61 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
<?php
namespace Yabacon\Paystack\Http;
use \Yabacon\Paystack\Contracts\RouteInterface;
class Request
{
public $method;
public $endpoint;
public $body = '';
public $headers = [];
protected $response;
protected $paystackObj;
public function __construct($paystackObj = null)
{
$this->response = new Response();
$this->paystackObj = $paystackObj;
$this->response->forApi = !is_null($paystackObj);
if ($this->response->forApi) {
$this->headers['Content-Type'] = 'application/json';
}
}
public function getResponse()
{
return $this->response;
}
public function flattenedHeaders()
{
$_ = [];
foreach ($this->headers as $key => $value) {
$_[] = $key . ": " . $value;
}
return $_;
}
public function send()
{
$this->attemptGuzzle();
if (!$this->response->okay) {
$this->attemptCurl();
}
if (!$this->response->okay) {
$this->attemptFileGetContents();
}
return $this->response;
}
public function attemptGuzzle()
{
if (isset($this->paystackObj) && !$this->paystackObj->use_guzzle) {
$this->response->okay = false;
return;
}
if (class_exists('\\GuzzleHttp\\Exception\\BadResponseException')
&& class_exists('\\GuzzleHttp\\Exception\\ClientException')
&& class_exists('\\GuzzleHttp\\Exception\\ConnectException')
&& class_exists('\\GuzzleHttp\\Exception\\RequestException')
&& class_exists('\\GuzzleHttp\\Exception\\ServerException')
&& class_exists('\\GuzzleHttp\\Client')
&& class_exists('\\GuzzleHttp\\Psr7\\Request')
) {
$request = new \GuzzleHttp\Psr7\Request(
strtoupper($this->method),
$this->endpoint,
$this->headers,
$this->body
);
$client = new \GuzzleHttp\Client();
try {
$psr7response = $client->send($request);
$this->response->body = $psr7response->getBody()->getContents();
$this->response->okay = true;
} catch (\Exception $e) {
if (($e instanceof \GuzzleHttp\Exception\BadResponseException
|| $e instanceof \GuzzleHttp\Exception\ClientException
|| $e instanceof \GuzzleHttp\Exception\ConnectException
|| $e instanceof \GuzzleHttp\Exception\RequestException
|| $e instanceof \GuzzleHttp\Exception\ServerException)
) {
if ($e->hasResponse()) {
$this->response->body = $e->getResponse()->getBody()->getContents();
}
$this->response->okay = true;
}
$this->response->messages[] = $e->getMessage();
}
}
}
public function attemptFileGetContents()
{
$context = stream_context_create(
[
'http'=>array(
'method'=>$this->method,
'header'=>$this->flattenedHeaders(),
'content'=>$this->body,
'ignore_errors' => true
)
]
);
$this->response->body = file_get_contents($this->endpoint, false, $context);
if ($this->response->body === false) {
$this->response->messages[] = 'file_get_contents failed with response: \'' . error_get_last() . '\'.';
} else {
$this->response->okay = true;
}
}
public function attemptCurl()
{
//open connection
$ch = \curl_init();
\curl_setopt($ch, \CURLOPT_URL, $this->endpoint);
($this->method === RouteInterface::POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true);
($this->method === RouteInterface::PUT_METHOD) && \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
if ($this->method !== RouteInterface::GET_METHOD) {
\curl_setopt($ch, \CURLOPT_POSTFIELDS, $this->body);
}
\curl_setopt($ch, \CURLOPT_HTTPHEADER, $this->flattenedHeaders());
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
$this->response->forApi && \curl_setopt($ch, \CURLOPT_SSLVERSION, 6);
$this->response->body = \curl_exec($ch);
if (\curl_errno($ch)) {
$cerr = \curl_error($ch);
$this->response->messages[] = 'Curl failed with response: \'' . $cerr . '\'.';
} else {
$this->response->okay = true;
}
\curl_close($ch);
}
}