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