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
|
|
|
* Function to initialize Http Client. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
protected function setClient() |
18
|
|
|
{ |
19
|
|
|
$this->client = new HttpClient([ |
|
|
|
|
20
|
|
|
'curl' => $this->httpClientConfig, |
|
|
|
|
21
|
|
|
]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Function to set Http Client configuration. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
protected function setHttpClientConfiguration() |
30
|
|
|
{ |
31
|
|
|
$this->httpClientConfig = [ |
|
|
|
|
32
|
|
|
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, |
33
|
|
|
CURLOPT_SSL_VERIFYPEER => $this->validateSSL, |
|
|
|
|
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
if (!empty($this->certificate)) { |
37
|
|
|
$this->httpClientConfig[CURLOPT_SSLCERT] = $this->certificate; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Initialize Http Client |
41
|
|
|
$this->setClient(); |
42
|
|
|
|
43
|
|
|
// Set default values. |
44
|
|
|
$this->setDefaultValues(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// Set PayPal API Endpoint. |
47
|
|
|
$this->apiUrl = $this->config['api_url']; |
|
|
|
|
48
|
|
|
|
49
|
|
|
// Set PayPal IPN Notification URL |
50
|
|
|
$this->notifyUrl = $this->config['notify_url']; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Perform PayPal API request & return response. |
55
|
|
|
* |
56
|
|
|
* @throws \Exception |
57
|
|
|
* |
58
|
|
|
* @return \Psr\Http\Message\StreamInterface |
59
|
|
|
*/ |
60
|
|
|
private function makeHttpRequest() |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
return $this->client->post($this->apiUrl, [ |
64
|
|
|
$this->httpBodyParam => $this->post->toArray(), |
|
|
|
|
65
|
|
|
])->getBody(); |
66
|
|
|
} catch (HttpClientException $e) { |
67
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
68
|
|
|
} catch (HttpServerException $e) { |
69
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
70
|
|
|
} catch (HttpBadResponseException $e) { |
71
|
|
|
throw new \Exception($e->getRequest().' '.$e->getResponse()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Function To Perform PayPal API Request. |
77
|
|
|
* |
78
|
|
|
* @param string $method |
79
|
|
|
* |
80
|
|
|
* @throws \Exception |
81
|
|
|
* |
82
|
|
|
* @return array|\Psr\Http\Message\StreamInterface |
83
|
|
|
*/ |
84
|
|
|
private function doPayPalRequest($method) |
85
|
|
|
{ |
86
|
|
|
// Setup PayPal API Request Payload |
87
|
|
|
$this->createRequestPayload($method); |
|
|
|
|
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
// Perform PayPal HTTP API request. |
91
|
|
|
$response = $this->makeHttpRequest(); |
92
|
|
|
|
93
|
|
|
return $this->retrieveData($method, $response); |
|
|
|
|
94
|
|
|
} catch (\Exception $e) { |
95
|
|
|
$message = collect($e->getTrace())->implode('\n'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
'type' => 'error', |
100
|
|
|
'message' => $message, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
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: