Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

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