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