|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use GuzzleHttp\Exception\BadResponseException as HttpBadResponseException; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException as HttpClientException; |
|
8
|
|
|
use GuzzleHttp\Exception\ServerException as HttpServerException; |
|
9
|
|
|
|
|
10
|
|
|
trait PayPalHttpClient |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Set curl constants if not defined. |
|
14
|
|
|
* |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function setCurlConstants() |
|
18
|
|
|
{ |
|
19
|
|
|
if (!defined('CURLOPT_SSLVERSION')) { |
|
20
|
|
|
define('CURLOPT_SSLVERSION', 32); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if (!defined('CURL_SSLVERSION_TLSv1_2')) { |
|
24
|
|
|
define('CURL_SSLVERSION_TLSv1_2', 6); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (!defined('CURLOPT_SSL_VERIFYPEER')) { |
|
28
|
|
|
define('CURLOPT_SSL_VERIFYPEER', 64); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!defined('CURLOPT_SSLCERT')) { |
|
32
|
|
|
define('CURLOPT_SSLCERT', 10025); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Function to initialize Http Client. |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function setClient() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->client = new HttpClient([ |
|
|
|
|
|
|
44
|
|
|
'curl' => $this->httpClientConfig, |
|
|
|
|
|
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Function to set Http Client configuration. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function setHttpClientConfiguration() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->defineCurlConstants(); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->httpClientConfig = [ |
|
|
|
|
|
|
58
|
|
|
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, |
|
59
|
|
|
CURLOPT_SSL_VERIFYPEER => $this->validateSSL, |
|
|
|
|
|
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
if (!empty($this->certificate)) { |
|
63
|
|
|
$this->httpClientConfig[CURLOPT_SSLCERT] = $this->certificate; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Initialize Http Client |
|
67
|
|
|
$this->setClient(); |
|
68
|
|
|
|
|
69
|
|
|
// Set default values. |
|
70
|
|
|
$this->setDefaultValues(); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
// Set PayPal API Endpoint. |
|
73
|
|
|
$this->apiUrl = $this->config['api_url']; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
// Set PayPal IPN Notification URL |
|
76
|
|
|
$this->notifyUrl = $this->config['notify_url']; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Perform PayPal API request & return response. |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Exception |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
private function makeHttpRequest() |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
return $this->client->post($this->apiUrl, [ |
|
90
|
|
|
$this->httpBodyParam => $this->post->toArray(), |
|
|
|
|
|
|
91
|
|
|
])->getBody(); |
|
92
|
|
|
} catch (HttpClientException $e) { |
|
93
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
|
94
|
|
|
} catch (HttpServerException $e) { |
|
95
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
|
96
|
|
|
} catch (HttpBadResponseException $e) { |
|
97
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Function To Perform PayPal API Request. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $method |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
* |
|
108
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
|
109
|
|
|
*/ |
|
110
|
|
|
private function doPayPalRequest($method) |
|
111
|
|
|
{ |
|
112
|
|
|
// Setup PayPal API Request Payload |
|
113
|
|
|
$this->createRequestPayload($method); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
try { |
|
116
|
|
|
// Perform PayPal HTTP API request. |
|
117
|
|
|
$response = $this->makeHttpRequest(); |
|
118
|
|
|
|
|
119
|
|
|
return $this->retrieveData($method, $response); |
|
|
|
|
|
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
$message = collect($e->getTrace())->implode('\n'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return [ |
|
125
|
|
|
'type' => 'error', |
|
126
|
|
|
'message' => $message, |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: