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