1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\Exception\ClientException as HttpClientException; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
trait PayPalHttpClient |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Http Client class object. |
14
|
|
|
* |
15
|
|
|
* @var HttpClient |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Http Client configuration. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $httpClientConfig; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PayPal API Endpoint. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $apiUrl; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PayPal API Endpoint. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $apiEndPoint; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* IPN notification url for PayPal. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $notifyUrl; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Http Client request body parameter name. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $httpBodyParam; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Default payment action for PayPal. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $paymentAction; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Default locale for PayPal. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $locale; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate SSL details when creating HTTP client. |
70
|
|
|
* |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
private $validateSSL; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Request type. |
77
|
|
|
* |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
protected $verb = 'post'; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set curl constants if not defined. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function setCurlConstants() |
88
|
|
|
{ |
89
|
|
|
$constants = collect([ |
90
|
|
|
'CURLOPT_SSLVERSION' => 32, |
91
|
|
|
'CURL_SSLVERSION_TLSv1_2' => 6, |
92
|
|
|
'CURLOPT_SSL_VERIFYPEER' => 64, |
93
|
|
|
'CURLOPT_SSLCERT' => 10025, |
94
|
|
|
]; |
|
|
|
|
95
|
|
|
|
96
|
|
|
foreach ($constants as $key => $item) { |
97
|
|
|
$constant = defined($key) ? true : define($key, $item); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Function to initialize/override Http Client. |
103
|
|
|
* |
104
|
|
|
* @param \GuzzleHttp\Client|null $client |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setClient(HttpClient $client = null) |
109
|
|
|
{ |
110
|
|
|
if ($client instanceof HttpClient) { |
111
|
|
|
$this->client = $client; |
112
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->client = new HttpClient([ |
117
|
|
|
'curl' => $this->httpClientConfig, |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Function to set Http Client configuration. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function setHttpClientConfiguration() |
127
|
|
|
{ |
128
|
|
|
$this->setCurlConstants(); |
129
|
|
|
|
130
|
|
|
$this->httpClientConfig = [ |
131
|
|
|
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, |
132
|
|
|
CURLOPT_SSL_VERIFYPEER => $this->validateSSL, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
// Initialize Http Client |
136
|
|
|
$this->setClient(); |
137
|
|
|
|
138
|
|
|
// Set default values. |
139
|
|
|
$this->setDefaultValues(); |
140
|
|
|
|
141
|
|
|
// Set PayPal IPN Notification URL |
142
|
|
|
$this->notifyUrl = $this->config['notify_url']; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set default values for configuration. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
private function setDefaultValues() |
151
|
|
|
{ |
152
|
|
|
$paymentAction = empty($this->paymentAction) ? 'Sale' : $this->paymentAction; |
153
|
|
|
$this->paymentAction = $paymentAction; |
154
|
|
|
|
155
|
|
|
$locale = empty($this->locale) ? 'en_US' : $this->locale; |
156
|
|
|
$this->locale = $locale; |
157
|
|
|
|
158
|
|
|
$validateSSL = empty($this->validateSSL) ? true : $this->validateSSL; |
159
|
|
|
$this->validateSSL = $validateSSL; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Perform PayPal API request & return response. |
164
|
|
|
* |
165
|
|
|
* @throws \Throwable |
166
|
|
|
* |
167
|
|
|
* @return StreamInterface |
168
|
|
|
*/ |
169
|
|
|
private function makeHttpRequest(): StreamInterface |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
|
|
return $this->client->{$this->verb}( |
173
|
|
|
$this->apiUrl, |
174
|
|
|
$this->options |
175
|
|
|
)->getBody(); |
176
|
|
|
} catch (HttpClientException $e) { |
177
|
|
|
throw new RuntimeException($e->getRequest()->getBody().' '.$e->getResponse()->getBody()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Function To Perform PayPal API Request. |
183
|
|
|
* |
184
|
|
|
* @param bool $decode |
185
|
|
|
* |
186
|
|
|
* @throws \Throwable |
187
|
|
|
* |
188
|
|
|
* @return array|StreamInterface|string |
189
|
|
|
*/ |
190
|
|
|
private function doPayPalRequest(bool $decode = true) |
191
|
|
|
{ |
192
|
|
|
try { |
193
|
|
|
$this->apiUrl = collect([$this->config['api_url'], $this->apiEndPoint])->implode('/'); |
194
|
|
|
|
195
|
|
|
// Perform PayPal HTTP API request. |
196
|
|
|
$response = $this->makeHttpRequest(); |
197
|
|
|
|
198
|
|
|
return ($decode === false) ? $response->getContents() : \GuzzleHttp\json_decode($response, true); |
199
|
|
|
} catch (RuntimeException $t) { |
200
|
|
|
$message = collect($t->getMessage())->implode('\n'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return [ |
204
|
|
|
'type' => 'error', |
205
|
|
|
'message' => $message, |
206
|
|
|
]; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|