| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\Provider;
|
|
|
4 |
|
|
|
5 |
use Faker\Calculator\Iban;
|
|
|
6 |
use Faker\Calculator\Luhn;
|
|
|
7 |
|
|
|
8 |
class Payment extends Base
|
|
|
9 |
{
|
|
|
10 |
public static $expirationDateFormat = 'm/y';
|
|
|
11 |
|
|
|
12 |
protected static $cardVendors = [
|
|
|
13 |
'Visa', 'Visa', 'Visa', 'Visa', 'Visa',
|
|
|
14 |
'MasterCard', 'MasterCard', 'MasterCard', 'MasterCard', 'MasterCard',
|
|
|
15 |
'American Express', 'Discover Card', 'Visa Retired', 'JCB',
|
|
|
16 |
];
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* @var array List of card brand masks for generating valid credit card numbers
|
|
|
20 |
*
|
|
|
21 |
* @see https://en.wikipedia.org/wiki/Payment_card_number Reference for existing prefixes
|
|
|
22 |
* @see https://www.mastercard.us/en-us/issuers/get-support/2-series-bin-expansion.html MasterCard 2017 2-Series BIN Expansion
|
|
|
23 |
*/
|
|
|
24 |
protected static $cardParams = [
|
|
|
25 |
'Visa' => [
|
|
|
26 |
'4539###########',
|
|
|
27 |
'4556###########',
|
|
|
28 |
'4916###########',
|
|
|
29 |
'4532###########',
|
|
|
30 |
'4929###########',
|
|
|
31 |
'40240071#######',
|
|
|
32 |
'4485###########',
|
|
|
33 |
'4716###########',
|
|
|
34 |
'4##############',
|
|
|
35 |
],
|
|
|
36 |
'Visa Retired' => [
|
|
|
37 |
'4539########',
|
|
|
38 |
'4556########',
|
|
|
39 |
'4916########',
|
|
|
40 |
'4532########',
|
|
|
41 |
'4929########',
|
|
|
42 |
'40240071####',
|
|
|
43 |
'4485########',
|
|
|
44 |
'4716########',
|
|
|
45 |
'4###########',
|
|
|
46 |
],
|
|
|
47 |
'MasterCard' => [
|
|
|
48 |
'2221###########',
|
|
|
49 |
'23#############',
|
|
|
50 |
'24#############',
|
|
|
51 |
'25#############',
|
|
|
52 |
'26#############',
|
|
|
53 |
'2720###########',
|
|
|
54 |
'51#############',
|
|
|
55 |
'52#############',
|
|
|
56 |
'53#############',
|
|
|
57 |
'54#############',
|
|
|
58 |
'55#############',
|
|
|
59 |
],
|
|
|
60 |
'American Express' => [
|
|
|
61 |
'34############',
|
|
|
62 |
'37############',
|
|
|
63 |
],
|
|
|
64 |
'Discover Card' => [
|
|
|
65 |
'6011###########',
|
|
|
66 |
],
|
|
|
67 |
'JCB' => [
|
|
|
68 |
'3528###########',
|
|
|
69 |
'3589###########',
|
|
|
70 |
],
|
|
|
71 |
];
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @var array list of IBAN formats, source: @see https://www.swift.com/standards/data-standards/iban
|
|
|
75 |
*/
|
|
|
76 |
protected static $ibanFormats = [
|
|
|
77 |
'AD' => [['n', 4], ['n', 4], ['c', 12]],
|
|
|
78 |
'AE' => [['n', 3], ['n', 16]],
|
|
|
79 |
'AL' => [['n', 8], ['c', 16]],
|
|
|
80 |
'AT' => [['n', 5], ['n', 11]],
|
|
|
81 |
'AZ' => [['a', 4], ['c', 20]],
|
|
|
82 |
'BA' => [['n', 3], ['n', 3], ['n', 8], ['n', 2]],
|
|
|
83 |
'BE' => [['n', 3], ['n', 7], ['n', 2]],
|
|
|
84 |
'BG' => [['a', 4], ['n', 4], ['n', 2], ['c', 8]],
|
|
|
85 |
'BH' => [['a', 4], ['c', 14]],
|
|
|
86 |
'BR' => [['n', 8], ['n', 5], ['n', 10], ['a', 1], ['c', 1]],
|
|
|
87 |
'CH' => [['n', 5], ['c', 12]],
|
|
|
88 |
'CR' => [['n', 4], ['n', 14]],
|
|
|
89 |
'CY' => [['n', 3], ['n', 5], ['c', 16]],
|
|
|
90 |
'CZ' => [['n', 4], ['n', 6], ['n', 10]],
|
|
|
91 |
'DE' => [['n', 8], ['n', 10]],
|
|
|
92 |
'DK' => [['n', 4], ['n', 9], ['n', 1]],
|
|
|
93 |
'DO' => [['c', 4], ['n', 20]],
|
|
|
94 |
'EE' => [['n', 2], ['n', 2], ['n', 11], ['n', 1]],
|
|
|
95 |
'EG' => [['n', 4], ['n', 4], ['n', 17]],
|
|
|
96 |
'ES' => [['n', 4], ['n', 4], ['n', 1], ['n', 1], ['n', 10]],
|
|
|
97 |
'FI' => [['n', 6], ['n', 7], ['n', 1]],
|
|
|
98 |
'FR' => [['n', 5], ['n', 5], ['c', 11], ['n', 2]],
|
|
|
99 |
'GB' => [['a', 4], ['n', 6], ['n', 8]],
|
|
|
100 |
'GE' => [['a', 2], ['n', 16]],
|
|
|
101 |
'GI' => [['a', 4], ['c', 15]],
|
|
|
102 |
'GR' => [['n', 3], ['n', 4], ['c', 16]],
|
|
|
103 |
'GT' => [['c', 4], ['c', 20]],
|
|
|
104 |
'HR' => [['n', 7], ['n', 10]],
|
|
|
105 |
'HU' => [['n', 3], ['n', 4], ['n', 1], ['n', 15], ['n', 1]],
|
|
|
106 |
'IE' => [['a', 4], ['n', 6], ['n', 8]],
|
|
|
107 |
'IL' => [['n', 3], ['n', 3], ['n', 13]],
|
|
|
108 |
'IS' => [['n', 4], ['n', 2], ['n', 6], ['n', 10]],
|
|
|
109 |
'IT' => [['a', 1], ['n', 5], ['n', 5], ['c', 12]],
|
|
|
110 |
'KW' => [['a', 4], ['n', 22]],
|
|
|
111 |
'KZ' => [['n', 3], ['c', 13]],
|
|
|
112 |
'LB' => [['n', 4], ['c', 20]],
|
|
|
113 |
'LI' => [['n', 5], ['c', 12]],
|
|
|
114 |
'LT' => [['n', 5], ['n', 11]],
|
|
|
115 |
'LU' => [['n', 3], ['c', 13]],
|
|
|
116 |
'LV' => [['a', 4], ['c', 13]],
|
|
|
117 |
'MC' => [['n', 5], ['n', 5], ['c', 11], ['n', 2]],
|
|
|
118 |
'MD' => [['c', 2], ['c', 18]],
|
|
|
119 |
'ME' => [['n', 3], ['n', 13], ['n', 2]],
|
|
|
120 |
'MK' => [['n', 3], ['c', 10], ['n', 2]],
|
|
|
121 |
'MR' => [['n', 5], ['n', 5], ['n', 11], ['n', 2]],
|
|
|
122 |
'MT' => [['a', 4], ['n', 5], ['c', 18]],
|
|
|
123 |
'MU' => [['a', 4], ['n', 2], ['n', 2], ['n', 12], ['n', 3], ['a', 3]],
|
|
|
124 |
'NL' => [['a', 4], ['n', 10]],
|
|
|
125 |
'NO' => [['n', 4], ['n', 6], ['n', 1]],
|
|
|
126 |
'PK' => [['a', 4], ['c', 16]],
|
|
|
127 |
'PL' => [['n', 8], ['n', 16]],
|
|
|
128 |
'PS' => [['a', 4], ['c', 21]],
|
|
|
129 |
'PT' => [['n', 4], ['n', 4], ['n', 11], ['n', 2]],
|
|
|
130 |
'RO' => [['a', 4], ['c', 16]],
|
|
|
131 |
'RS' => [['n', 3], ['n', 13], ['n', 2]],
|
|
|
132 |
'SA' => [['n', 2], ['c', 18]],
|
|
|
133 |
'SE' => [['n', 3], ['n', 16], ['n', 1]],
|
|
|
134 |
'SI' => [['n', 5], ['n', 8], ['n', 2]],
|
|
|
135 |
'SK' => [['n', 4], ['n', 6], ['n', 10]],
|
|
|
136 |
'SM' => [['a', 1], ['n', 5], ['n', 5], ['c', 12]],
|
|
|
137 |
'TN' => [['n', 2], ['n', 3], ['n', 13], ['n', 2]],
|
|
|
138 |
'TR' => [['n', 5], ['n', 1], ['c', 16]],
|
|
|
139 |
'VG' => [['a', 4], ['n', 16]],
|
|
|
140 |
];
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* @return string Returns a credit card vendor name
|
|
|
144 |
*
|
|
|
145 |
* @example 'MasterCard'
|
|
|
146 |
*/
|
|
|
147 |
public static function creditCardType()
|
|
|
148 |
{
|
|
|
149 |
return static::randomElement(static::$cardVendors);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Returns the String of a credit card number.
|
|
|
154 |
*
|
|
|
155 |
* @param string $type Supporting any of 'Visa', 'MasterCard', 'American Express', 'Discover' and 'JCB'
|
|
|
156 |
* @param bool $formatted Set to true if the output string should contain one separator every 4 digits
|
|
|
157 |
* @param string $separator Separator string for formatting card number. Defaults to dash (-).
|
|
|
158 |
*
|
|
|
159 |
* @return string
|
|
|
160 |
*
|
|
|
161 |
* @example '4485480221084675'
|
|
|
162 |
*/
|
|
|
163 |
public static function creditCardNumber($type = null, $formatted = false, $separator = '-')
|
|
|
164 |
{
|
|
|
165 |
if (null === $type) {
|
|
|
166 |
$type = static::creditCardType();
|
|
|
167 |
}
|
|
|
168 |
$mask = static::randomElement(static::$cardParams[$type]);
|
|
|
169 |
|
|
|
170 |
$number = static::numerify($mask);
|
|
|
171 |
$number .= Luhn::computeCheckDigit($number);
|
|
|
172 |
|
|
|
173 |
if ($formatted) {
|
|
|
174 |
$p1 = substr($number, 0, 4);
|
|
|
175 |
$p2 = substr($number, 4, 4);
|
|
|
176 |
$p3 = substr($number, 8, 4);
|
|
|
177 |
$p4 = substr($number, 12);
|
|
|
178 |
$number = $p1 . $separator . $p2 . $separator . $p3 . $separator . $p4;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return $number;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* @param bool $valid True (by default) to get a valid expiration date, false to get a maybe valid date
|
|
|
186 |
*
|
|
|
187 |
* @return \DateTime
|
|
|
188 |
*
|
|
|
189 |
* @example 04/13
|
|
|
190 |
*/
|
|
|
191 |
public function creditCardExpirationDate($valid = true)
|
|
|
192 |
{
|
|
|
193 |
if ($valid) {
|
|
|
194 |
return $this->generator->dateTimeBetween('now', '36 months');
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
return $this->generator->dateTimeBetween('-36 months', '36 months');
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* @param bool $valid True (by default) to get a valid expiration date, false to get a maybe valid date
|
|
|
202 |
* @param string $expirationDateFormat
|
|
|
203 |
*
|
|
|
204 |
* @return string
|
|
|
205 |
*
|
|
|
206 |
* @example '04/13'
|
|
|
207 |
*/
|
|
|
208 |
public function creditCardExpirationDateString($valid = true, $expirationDateFormat = null)
|
|
|
209 |
{
|
|
|
210 |
return $this->creditCardExpirationDate($valid)->format(null === $expirationDateFormat ? static::$expirationDateFormat : $expirationDateFormat);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* @param bool $valid True (by default) to get a valid expiration date, false to get a maybe valid date
|
|
|
215 |
*
|
|
|
216 |
* @return array
|
|
|
217 |
*/
|
|
|
218 |
public function creditCardDetails($valid = true)
|
|
|
219 |
{
|
|
|
220 |
$type = static::creditCardType();
|
|
|
221 |
|
|
|
222 |
return [
|
|
|
223 |
'type' => $type,
|
|
|
224 |
'number' => static::creditCardNumber($type),
|
|
|
225 |
'name' => $this->generator->name(),
|
|
|
226 |
'expirationDate' => $this->creditCardExpirationDateString($valid),
|
|
|
227 |
];
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* International Bank Account Number (IBAN)
|
|
|
232 |
*
|
|
|
233 |
* @see http://en.wikipedia.org/wiki/International_Bank_Account_Number
|
|
|
234 |
*
|
|
|
235 |
* @param string $countryCode ISO 3166-1 alpha-2 country code
|
|
|
236 |
* @param string $prefix for generating bank account number of a specific bank
|
|
|
237 |
* @param int $length total length without country code and 2 check digits
|
|
|
238 |
*
|
|
|
239 |
* @return string
|
|
|
240 |
*/
|
|
|
241 |
public static function iban($countryCode = null, $prefix = '', $length = null)
|
|
|
242 |
{
|
|
|
243 |
$countryCode = null === $countryCode ? self::randomKey(self::$ibanFormats) : strtoupper($countryCode);
|
|
|
244 |
|
|
|
245 |
$format = !isset(static::$ibanFormats[$countryCode]) ? null : static::$ibanFormats[$countryCode];
|
|
|
246 |
|
|
|
247 |
if ($length === null) {
|
|
|
248 |
if ($format === null) {
|
|
|
249 |
$length = 24;
|
|
|
250 |
} else {
|
|
|
251 |
$length = 0;
|
|
|
252 |
|
|
|
253 |
foreach ($format as $part) {
|
|
|
254 |
[$class, $groupCount] = $part;
|
|
|
255 |
$length += $groupCount;
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
if ($format === null) {
|
|
|
261 |
$format = [['n', $length]];
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
$expandedFormat = '';
|
|
|
265 |
|
|
|
266 |
foreach ($format as $item) {
|
|
|
267 |
[$class, $length] = $item;
|
|
|
268 |
$expandedFormat .= str_repeat($class, $length);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
$result = $prefix;
|
|
|
272 |
$expandedFormat = substr($expandedFormat, strlen($result));
|
|
|
273 |
|
|
|
274 |
foreach (str_split($expandedFormat) as $class) {
|
|
|
275 |
switch ($class) {
|
|
|
276 |
default:
|
|
|
277 |
case 'c':
|
|
|
278 |
$result .= Miscellaneous::boolean() ? static::randomDigit() : strtoupper(static::randomLetter());
|
|
|
279 |
|
|
|
280 |
break;
|
|
|
281 |
|
|
|
282 |
case 'a':
|
|
|
283 |
$result .= strtoupper(static::randomLetter());
|
|
|
284 |
|
|
|
285 |
break;
|
|
|
286 |
|
|
|
287 |
case 'n':
|
|
|
288 |
$result .= static::randomDigit();
|
|
|
289 |
|
|
|
290 |
break;
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
$checksum = Iban::checksum($countryCode . '00' . $result);
|
|
|
295 |
|
|
|
296 |
return $countryCode . $checksum . $result;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* Return the String of a SWIFT/BIC number
|
|
|
301 |
*
|
|
|
302 |
* @example 'RZTIAT22263'
|
|
|
303 |
*
|
|
|
304 |
* @see http://en.wikipedia.org/wiki/ISO_9362
|
|
|
305 |
*
|
|
|
306 |
* @return string Swift/Bic number
|
|
|
307 |
*/
|
|
|
308 |
public static function swiftBicNumber()
|
|
|
309 |
{
|
|
|
310 |
return self::regexify('^([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?$');
|
|
|
311 |
}
|
|
|
312 |
}
|