1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bank Account class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Omnipay\Spreedly; |
7
|
|
|
|
8
|
|
|
use Omnipay\Common\Helper; |
9
|
|
|
use Omnipay\Spreedly\Concerns\HasOwnerData; |
10
|
|
|
use Omnipay\Spreedly\Exception\InvalidPaymentMethodException; |
11
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Bank Account class |
15
|
|
|
* |
16
|
|
|
* This class defines and abstracts all of the bank account types used |
17
|
|
|
* throughout the Omnipay Spreedly system. |
18
|
|
|
* |
19
|
|
|
* Example: |
20
|
|
|
* |
21
|
|
|
* <code> |
22
|
|
|
* // Define bank account parameters, which should look like this |
23
|
|
|
* $parameters = array( |
24
|
|
|
* 'firstName' => 'Bobby', |
25
|
|
|
* 'lastName' => 'Tables', |
26
|
|
|
* 'number' => '9876543210', |
27
|
|
|
* 'routingNumber' => '021000021', |
28
|
|
|
* 'type' => 'checking', |
29
|
|
|
* 'holderType' => 'personal', |
30
|
|
|
* ); |
31
|
|
|
* |
32
|
|
|
* // Create a bank account object |
33
|
|
|
* $bankAccount = new BankAcount($parameters); |
34
|
|
|
* </code> |
35
|
|
|
* |
36
|
|
|
* The full list of bank account attributes that may be set via the parameter to |
37
|
|
|
* *new* is as follows: |
38
|
|
|
* |
39
|
|
|
* * firstName |
40
|
|
|
* * lastName |
41
|
|
|
* * number |
42
|
|
|
* * routingNumber |
43
|
|
|
* * type |
44
|
|
|
* * holderType |
45
|
|
|
* |
46
|
|
|
* If any unknown parameters are passed in, they will be ignored. No error is thrown. |
47
|
|
|
*/ |
48
|
|
|
class BankAccount |
49
|
|
|
{ |
50
|
|
|
use HasOwnerData; |
51
|
|
|
|
52
|
|
|
const TYPE_CHECKING = 'checking'; |
53
|
|
|
|
54
|
|
|
const HOLDER_TYPE_PERSONAL = 'personal'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Internal storage of all of the bank account parameters. |
58
|
|
|
* |
59
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
60
|
|
|
*/ |
61
|
|
|
protected $parameters; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new BankAccount object using the specified parameters |
65
|
|
|
* |
66
|
|
|
* @param array $parameters An array of parameters to set on the new object |
67
|
|
|
*/ |
68
|
6 |
|
public function __construct($parameters = null) |
69
|
|
|
{ |
70
|
6 |
|
$this->initialize($parameters); |
71
|
6 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Initialize the object with parameters. |
75
|
|
|
* |
76
|
|
|
* If any unknown parameters passed, they will be ignored. |
77
|
|
|
* |
78
|
|
|
* @param array $parameters An associative array of parameters |
79
|
|
|
* @return BankAccount provides a fluent interface. |
80
|
|
|
*/ |
81
|
6 |
|
public function initialize(array $parameters = null) |
82
|
|
|
{ |
83
|
6 |
|
$this->parameters = new ParameterBag; |
84
|
|
|
|
85
|
6 |
|
Helper::initialize($this, $parameters); |
|
|
|
|
86
|
|
|
|
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get all parameters. |
92
|
|
|
* |
93
|
|
|
* @return array An associative array of parameters. |
94
|
|
|
*/ |
95
|
|
|
public function getParameters() |
96
|
|
|
{ |
97
|
|
|
return $this->parameters->all(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get one parameter. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @return mixed A single parameter value. |
105
|
|
|
*/ |
106
|
6 |
|
protected function getParameter($key) |
107
|
|
|
{ |
108
|
6 |
|
return $this->parameters->get($key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set one parameter. |
113
|
|
|
* |
114
|
|
|
* @param string $key Parameter key |
115
|
|
|
* @param mixed $value Parameter value |
116
|
|
|
* @return BankAccount provides a fluent interface. |
117
|
|
|
*/ |
118
|
6 |
|
protected function setParameter($key, $value) |
119
|
|
|
{ |
120
|
6 |
|
$this->parameters->set($key, $value); |
121
|
|
|
|
122
|
6 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Validate this bank account. If the bank account is invalid, InvalidBankAccountException is thrown. |
127
|
|
|
* |
128
|
|
|
* This method is called internally by gateways to avoid wasting time with an API call |
129
|
|
|
* when the bank account is clearly invalid. |
130
|
|
|
* |
131
|
|
|
* Generally if you want to validate the bank account yourself with custom error |
132
|
|
|
* messages, you should use your framework's validation library, not this method. |
133
|
|
|
* |
134
|
|
|
* @throws InvalidPaymentMethodException |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
6 |
|
public function validate() |
138
|
|
|
{ |
139
|
|
|
$requiredParameters = array( |
140
|
6 |
|
'first_name' => 'bank account first name', |
141
|
6 |
|
'last_name' => 'bank account last name', |
142
|
6 |
|
'number' => 'bank account number', |
143
|
6 |
|
'routing_number' => 'bank account routing number', |
144
|
6 |
|
); |
145
|
|
|
|
146
|
6 |
|
foreach ($requiredParameters as $key => $val) { |
147
|
6 |
|
if (!$this->getParameter($key)) { |
148
|
|
|
throw new InvalidPaymentMethodException("The $val is required"); |
149
|
|
|
} |
150
|
6 |
|
} |
151
|
6 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get Bank Account Number. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
6 |
|
public function getNumber() |
159
|
|
|
{ |
160
|
6 |
|
return $this->getParameter('number'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set Bank Account Number |
165
|
|
|
* |
166
|
|
|
* Non-numeric characters are stripped out of the bank account number, so |
167
|
|
|
* it's safe to pass in strings such as "4444-3333 2222 1111" etc. |
168
|
|
|
* |
169
|
|
|
* @param string $value Parameter value |
170
|
|
|
* @return BankAccount provides a fluent interface. |
171
|
|
|
*/ |
172
|
6 |
|
public function setNumber($value) |
173
|
|
|
{ |
174
|
|
|
// strip non-numeric characters |
175
|
6 |
|
return $this->setParameter('number', preg_replace('/\D/', '', $value)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get Bank Account Routing Number. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
6 |
|
public function getRoutingNumber() |
184
|
|
|
{ |
185
|
6 |
|
return $this->getParameter('routing_number'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set Bank Account Routing Number |
190
|
|
|
* |
191
|
|
|
* Non-numeric characters are stripped out of the bank account number, so |
192
|
|
|
* it's safe to pass in strings such as "4444-3333 2222 1111" etc. |
193
|
|
|
* |
194
|
|
|
* @param string $value Parameter value |
195
|
|
|
* @return BankAccount provides a fluent interface. |
196
|
|
|
*/ |
197
|
6 |
|
public function setRoutingNumber($value) |
198
|
|
|
{ |
199
|
|
|
// strip non-numeric characters |
200
|
6 |
|
return $this->setParameter('routing_number', preg_replace('/\D/', '', $value)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get Bank Account Type. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
6 |
|
public function getType() |
209
|
|
|
{ |
210
|
6 |
|
return $this->getParameter('type'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set Bank Account Type. |
215
|
|
|
* |
216
|
|
|
* @param string $value Parameter value |
217
|
|
|
* @return BankAccount provides a fluent interface. |
218
|
|
|
*/ |
219
|
6 |
|
public function setType($value) |
220
|
|
|
{ |
221
|
6 |
|
return $this->setParameter('type', $value); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get Bank Account Holder Type. |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
6 |
|
public function getHolderType() |
230
|
|
|
{ |
231
|
6 |
|
return $this->getParameter('holder_type'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Set Bank Account Holder Type. |
236
|
|
|
* |
237
|
|
|
* @param string $value Parameter value |
238
|
|
|
* @return BankAccount provides a fluent interface. |
239
|
|
|
*/ |
240
|
6 |
|
public function setHolderType($value) |
241
|
|
|
{ |
242
|
6 |
|
return $this->setParameter('holder_type', $value); |
243
|
|
|
} |
244
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.