Completed
Branch v2.0 (1ad81b)
by Raza
07:29
created

PayPalHttpClient   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 2.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 130
ccs 1
cts 39
cp 0.0256
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpClientConfiguration() 0 20 1
A setCurlConstants() 0 16 5
A setClient() 0 4 1
A makeHttpRequest() 0 9 2
A doPayPalRequest() 0 14 2
A setDefaultValues() 0 15 4
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Psr\Http\Message\StreamInterface;
7
use RuntimeException;
8
use Throwable;
9
10
trait PayPalHttpClient
11
{
12
    protected $verb = 'post';
13
14
    /**
15
     * Set curl constants if not defined.
16
     *
17
     * @return void
18
     */
19
    protected function setCurlConstants()
20
    {
21
        if (!defined('CURLOPT_SSLVERSION')) {
22
            define('CURLOPT_SSLVERSION', 32);
23
        }
24
25
        if (!defined('CURL_SSLVERSION_TLSv1_2')) {
26
            define('CURL_SSLVERSION_TLSv1_2', 6);
27
        }
28
29
        if (!defined('CURLOPT_SSL_VERIFYPEER')) {
30
            define('CURLOPT_SSL_VERIFYPEER', 64);
31
        }
32
33
        if (!defined('CURLOPT_SSLCERT')) {
34
            define('CURLOPT_SSLCERT', 10025);
35
        }
36
    }
37
38
    /**
39
     * Function to initialize Http Client.
40
     *
41
     * @return void
42
     */
43
    protected function setClient()
44
    {
45
        $this->client = new HttpClient([
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
            'curl' => $this->httpClientConfig,
47
        ]);
48
    }
49
50
    /**
51
     * Function to set Http Client configuration.
52
     *
53
     * @return void
54
     */
55
    protected function setHttpClientConfiguration()
56
    {
57
        $this->setCurlConstants();
58
59
        $this->httpClientConfig = [
0 ignored issues
show
Bug Best Practice introduced by
The property httpClientConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
61
            CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
62
        ];
63
64
        // Initialize Http Client
65
        $this->setClient();
66
67
        // Set default values.
68
        $this->setDefaultValues();
69
70
        // Set PayPal API Endpoint.
71
        $this->apiUrl = $this->config['api_url'];
1 ignored issue
show
Bug Best Practice introduced by
The property apiUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
73
        // Set PayPal IPN Notification URL
74
        $this->notifyUrl = $this->config['notify_url'];
0 ignored issues
show
Bug Best Practice introduced by
The property notifyUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
    }
76
77
    /**
78
     * Set default values for configuration.
79
     *
80
     * @return void
81
     */
82
    private function setDefaultValues()
83
    {
84
        // Set default payment action.
85
        if (empty($this->paymentAction)) {
86
            $this->paymentAction = 'Sale';
0 ignored issues
show
Bug Best Practice introduced by
The property paymentAction does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
        }
88
89
        // Set default locale.
90
        if (empty($this->locale)) {
91
            $this->locale = 'en_US';
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
        }
93
94
        // Set default value for SSL validation.
95
        if (empty($this->validateSSL)) {
96
            $this->validateSSL = false;
0 ignored issues
show
Bug Best Practice introduced by
The property validateSSL does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
        }
98
    }
99
100
    /**
101
     * Perform PayPal API request & return response.
102
     *
103
     * @throws \Throwable
104
     *
105
     * @return StreamInterface
106
     */
107
    private function makeHttpRequest()
108
    {
109
        try {
110
            return $this->client->{$this->verb}(
111
                $this->apiUrl,
112
                $this->options
113
            )->getBody();
114
        } catch (Throwable $t) {
115
            throw new RuntimeException($t->getRequest().' '.$t->getResponse());
2 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as GuzzleHttp\Exception\RequestException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            throw new RuntimeException($t->/** @scrutinizer ignore-call */ getRequest().' '.$t->getResponse());
Loading history...
Bug introduced by
The method getResponse() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as GuzzleHttp\Exception\RequestException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            throw new RuntimeException($t->getRequest().' '.$t->/** @scrutinizer ignore-call */ getResponse());
Loading history...
116
        }
117
    }
118
119
    /**
120 1
     * Function To Perform PayPal API Request.
121
     *
122
     * @throws \Throwable
123
     *
124
     * @return array|StreamInterface
125
     */
126
    private function doPayPalRequest()
127
    {
128
        try {
129
            // Perform PayPal HTTP API request.
130
            $response = $this->makeHttpRequest();
131
132
            return \GuzzleHttp\json_decode($response, true);
133
        } catch (Throwable $t) {
134
            $message = collect($t->getTrace())->implode('\n');
135
        }
136
137
        return [
138
            'type'    => 'error',
139
            'message' => $message,
140
        ];
141
    }
142
}
143