|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
trait PayPalRequest |
|
8
|
|
|
{ |
|
9
|
|
|
use PayPalHttpClient; |
|
10
|
|
|
use PayPalAPI; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* PayPal API mode to be used. |
|
14
|
1 |
|
* |
|
15
|
1 |
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
public $mode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PayPal access token. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $access_token; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* PayPal API configuration. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $config; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Default currency for PayPal. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $currency; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Additional options for PayPal API request. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $options; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set PayPal API Credentials. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $credentials |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \RuntimeException |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setApiCredentials($credentials) |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($credentials)) { |
|
59
|
|
|
throw new RuntimeException('Empty configuration provided. Please provide valid configuration for Express Checkout API.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Setting Default PayPal Mode If not set |
|
63
|
|
|
$this->setApiEnvironment($credentials); |
|
64
|
|
|
|
|
65
|
|
|
// Set API configuration for the PayPal provider |
|
66
|
|
|
$this->setApiProviderConfiguration($credentials); |
|
67
|
|
|
|
|
68
|
|
|
// Set default currency. |
|
69
|
|
|
$this->setCurrency($credentials['currency']); |
|
70
|
|
|
|
|
71
|
|
|
// Set Http Client configuration. |
|
72
|
|
|
$this->setHttpClientConfiguration(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Function to set currency. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $currency |
|
79
|
|
|
* |
|
80
|
|
|
* @throws \RuntimeException |
|
81
|
|
|
* |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setCurrency($currency = 'USD') |
|
85
|
|
|
{ |
|
86
|
|
|
$allowedCurrencies = ['AUD', 'BRL', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'INR', 'JPY', 'MYR', 'MXN', 'NOK', 'NZD', 'PHP', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'TWD', 'THB', 'USD', 'RUB', 'CNY']; |
|
87
|
|
|
|
|
88
|
|
|
// Check if provided currency is valid. |
|
89
|
|
|
if (!in_array($currency, $allowedCurrencies, true)) { |
|
90
|
|
|
throw new RuntimeException('Currency is not supported by PayPal.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->currency = $currency; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the set currency. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getCurrency() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->currency; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Function to add request header. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param string $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setRequestHeader($key, $value) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->options['headers'][$key] = $value; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Return request options header. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $key |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \RuntimeException |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getRequestHeader($key) |
|
133
|
|
|
{ |
|
134
|
|
|
if (isset($this->options['headers'][$key])) { |
|
135
|
|
|
return $this->options['headers'][$key]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
throw new RuntimeException('Options header is not set.'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Function To Set PayPal API Configuration. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $config |
|
145
|
|
|
* |
|
146
|
|
|
* @throws Exception |
|
147
|
|
|
*/ |
|
148
|
|
|
private function setConfig(array $config = []) |
|
149
|
|
|
{ |
|
150
|
|
|
$api_config = function_exists('config') ? config('paypal') : $config; |
|
151
|
|
|
|
|
152
|
|
|
// Set Api Credentials |
|
153
|
|
|
$this->setApiCredentials($api_config); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Set API environment to be used by PayPal. |
|
158
|
|
|
* |
|
159
|
|
|
* @param array $credentials |
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
private function setApiEnvironment($credentials) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->mode = 'live'; |
|
166
|
|
|
|
|
167
|
|
|
if (!empty($credentials['mode'])) { |
|
168
|
|
|
$this->setValidApiEnvironment($credentials['mode']); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Validate & set the environment to be used by PayPal. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $mode |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setValidApiEnvironment($mode) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->mode = !in_array($mode, ['sandbox', 'live']) ? 'live' : $mode; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Set configuration details for the provider. |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $credentials |
|
188
|
|
|
* |
|
189
|
|
|
* @throws Exception |
|
190
|
|
|
* |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
|
|
private function setApiProviderConfiguration($credentials) |
|
194
|
|
|
{ |
|
195
|
|
|
// Setting PayPal API Credentials |
|
196
|
|
|
collect($credentials[$this->mode])->map(function ($value, $key) { |
|
197
|
|
|
$this->config[$key] = $value; |
|
198
|
|
|
}); |
|
199
|
|
|
|
|
200
|
|
|
$this->paymentAction = $credentials['payment_action']; |
|
201
|
|
|
|
|
202
|
|
|
$this->locale = $credentials['locale']; |
|
203
|
|
|
|
|
204
|
|
|
$this->validateSSL = $credentials['validate_ssl']; |
|
205
|
|
|
|
|
206
|
|
|
$this->setOptions($credentials); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|