-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
CreditCard.php
1550 lines (1394 loc) · 33.3 KB
/
CreditCard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Credit Card class
*/
namespace Omnipay\Common;
use DateTime;
use DateTimeZone;
use Omnipay\Common\Exception\InvalidCreditCardException;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Credit Card class
*
* This class defines and abstracts all of the credit card types used
* throughout the Omnipay system.
*
* Example:
*
* <code>
* // Define credit card parameters, which should look like this
* $parameters = [
* 'firstName' => 'Bobby',
* 'lastName' => 'Tables',
* 'number' => '4444333322221111',
* 'cvv' => '123',
* 'expiryMonth' => '12',
* 'expiryYear' => '2017',
* 'email' => '[email protected]',
* ];
*
* // Create a credit card object
* $card = new CreditCard($parameters);
* </code>
*
* The full list of card attributes that may be set via the parameter to
* *new* is as follows:
*
* * title
* * firstName
* * lastName
* * name
* * company
* * address1
* * address2
* * city
* * postcode
* * state
* * country
* * phone
* * phoneExtension
* * fax
* * number
* * expiryMonth
* * expiryYear
* * startMonth
* * startYear
* * cvv
* * tracks
* * issueNumber
* * billingTitle
* * billingName
* * billingFirstName
* * billingLastName
* * billingCompany
* * billingAddress1
* * billingAddress2
* * billingCity
* * billingPostcode
* * billingState
* * billingCountry
* * billingPhone
* * billingFax
* * shippingTitle
* * shippingName
* * shippingFirstName
* * shippingLastName
* * shippingCompany
* * shippingAddress1
* * shippingAddress2
* * shippingCity
* * shippingPostcode
* * shippingState
* * shippingCountry
* * shippingPhone
* * shippingFax
* * email
* * birthday
* * gender
*
* If any unknown parameters are passed in, they will be ignored. No error is thrown.
*/
class CreditCard
{
use ParametersTrait;
const BRAND_VISA = 'visa';
const BRAND_MASTERCARD = 'mastercard';
const BRAND_DISCOVER = 'discover';
const BRAND_AMEX = 'amex';
const BRAND_DINERS_CLUB = 'diners_club';
const BRAND_JCB = 'jcb';
const BRAND_SWITCH = 'switch';
const BRAND_SOLO = 'solo';
const BRAND_DANKORT = 'dankort';
const BRAND_MAESTRO = 'maestro';
const BRAND_FORBRUGSFORENINGEN = 'forbrugsforeningen';
const BRAND_LASER = 'laser';
/**
* All known/supported card brands, and a regular expression to match them.
*
* The order of the card brands is important, as some of the regular expressions overlap.
*
* Note: The fact that a particular card brand has been added to this array does not imply
* that a selected gateway will support the card.
*
* @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb
* @var array
*/
const REGEX_MASTERCARD = '/^(5[1-5]\d{4}|677189)\d{10}$|^2(?:2(?:2[1-9]|[3-9]\d)|[3-6]\d\d|7(?:[01]\d|20))\d{12}$/';
protected $supported_cards = array(
self::BRAND_VISA => '/^4\d{12}(\d{3})?$/',
self::BRAND_MASTERCARD => self::REGEX_MASTERCARD,
self::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/',
self::BRAND_AMEX => '/^3[47]\d{13}$/',
self::BRAND_DINERS_CLUB => '/^3(0[0-5]|[68]\d)\d{11}$/',
self::BRAND_JCB => '/^35(28|29|[3-8]\d)\d{12}$/',
self::BRAND_SWITCH => '/^6759\d{12}(\d{2,3})?$/',
self::BRAND_SOLO => '/^6767\d{12}(\d{2,3})?$/',
self::BRAND_DANKORT => '/^5019\d{12}$/',
self::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/',
self::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/',
self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
);
/**
* Create a new CreditCard object using the specified parameters
*
* @param array $parameters An array of parameters to set on the new object
*/
public function __construct($parameters = null)
{
$this->initialize($parameters);
}
/**
* All known/supported card brands, and a regular expression to match them.
*
* Note: The fact that this class knows about a particular card brand does not imply
* that your gateway supports it.
*
* @return array
*/
public function getSupportedBrands()
{
return $this->supported_cards;
}
/**
* Set a custom supported card brand with a regular expression to match it.
*
* Note: The fact that a particular card is known does not imply that your
* gateway supports it.
*
* Set $add_to_front to true if the key should be added to the front of the array
*
* @param string $name The name of the new supported brand.
* @param string $expression The regular expression to check if a card is supported.
* @return boolean success
*/
public function addSupportedBrand($name, $expression)
{
$known_brands = array_keys($this->supported_cards);
if (in_array($name, $known_brands)) {
return false;
}
$this->supported_cards[$name] = $expression;
return true;
}
/**
* Initialize the object with parameters.
*
* If any unknown parameters passed, they will be ignored.
*
* @param array $parameters An associative array of parameters
* @return $this
*/
public function initialize(array $parameters = null)
{
$this->parameters = new ParameterBag;
Helper::initialize($this, $parameters);
return $this;
}
/**
* Set the credit card year.
*
* The input value is normalised to a 4 digit number.
*
* @param string $key Parameter key, e.g. 'expiryYear'
* @param mixed $value Parameter value
* @return $this
*/
protected function setYearParameter($key, $value)
{
// normalize year to four digits
if (null === $value || '' === $value) {
$value = null;
} else {
$value = (int) gmdate('Y', gmmktime(0, 0, 0, 1, 1, (int) $value));
}
return $this->setParameter($key, $value);
}
/**
* Validate this credit card. If the card is invalid, InvalidCreditCardException is thrown.
*
* This method is called internally by gateways to avoid wasting time with an API call
* when the credit card is clearly invalid.
*
* Generally if you want to validate the credit card yourself with custom error
* messages, you should use your framework's validation library, not this method.
*
* @return void
* @throws Exception\InvalidRequestException
* @throws InvalidCreditCardException
*/
public function validate()
{
$requiredParameters = array(
'number' => 'credit card number',
'expiryMonth' => 'expiration month',
'expiryYear' => 'expiration year'
);
foreach ($requiredParameters as $key => $val) {
if (!$this->getParameter($key)) {
throw new InvalidCreditCardException("The $val is required");
}
}
if ($this->getExpiryDate('Ym') < gmdate('Ym')) {
throw new InvalidCreditCardException('Card has expired');
}
if (!Helper::validateLuhn($this->getNumber())) {
throw new InvalidCreditCardException('Card number is invalid');
}
if (!is_null($this->getNumber()) && !preg_match('/^\d{12,19}$/i', $this->getNumber())) {
throw new InvalidCreditCardException('Card number should have 12 to 19 digits');
}
}
/**
* Get Card Title.
*
* @return string
*/
public function getTitle()
{
return $this->getBillingTitle();
}
/**
* Set Card Title.
*
* @param string $value Parameter value
* @return $this
*/
public function setTitle($value)
{
$this->setBillingTitle($value);
$this->setShippingTitle($value);
return $this;
}
/**
* Get Card First Name.
*
* @return string
*/
public function getFirstName()
{
return $this->getBillingFirstName();
}
/**
* Set Card First Name (Billing and Shipping).
*
* @param string $value Parameter value
* @return $this
*/
public function setFirstName($value)
{
$this->setBillingFirstName($value);
$this->setShippingFirstName($value);
return $this;
}
/**
* Get Card Last Name.
*
* @return string
*/
public function getLastName()
{
return $this->getBillingLastName();
}
/**
* Set Card Last Name (Billing and Shipping).
*
* @param string $value Parameter value
* @return $this
*/
public function setLastName($value)
{
$this->setBillingLastName($value);
$this->setShippingLastName($value);
return $this;
}
/**
* Get Card Name.
*
* @return string
*/
public function getName()
{
return $this->getBillingName();
}
/**
* Set Card Name (Billing and Shipping).
*
* @param string $value Parameter value
* @return $this
*/
public function setName($value)
{
$this->setBillingName($value);
$this->setShippingName($value);
return $this;
}
/**
* Get Card Number.
*
* @return string
*/
public function getNumber()
{
return $this->getParameter('number');
}
/**
* Set Card Number
*
* Non-numeric characters are stripped out of the card number, so
* it's safe to pass in strings such as "4444-3333 2222 1111" etc.
*
* @param string $value Parameter value
* @return $this
*/
public function setNumber($value)
{
// strip non-numeric characters
return $this->setParameter('number', preg_replace('/\D/', '', $value));
}
/**
* Get the last 4 digits of the card number.
*
* @return string
*/
public function getNumberLastFour()
{
return substr($this->getNumber(), -4, 4) ?: null;
}
/**
* Returns a masked credit card number with only the last 4 chars visible
*
* @param string $mask Character to use in place of numbers
* @return string
*/
public function getNumberMasked($mask = 'X')
{
$maskLength = strlen($this->getNumber()) - 4;
return str_repeat($mask, $maskLength) . $this->getNumberLastFour();
}
/**
* Credit Card Brand
*
* Iterates through known/supported card brands to determine the brand of this card
*
* @return string
*/
public function getBrand()
{
foreach ($this->getSupportedBrands() as $brand => $val) {
if (preg_match($val, $this->getNumber())) {
return $brand;
}
}
}
/**
* Get the card expiry month.
*
* @return int
*/
public function getExpiryMonth()
{
return $this->getParameter('expiryMonth');
}
/**
* Sets the card expiry month.
*
* @param string $value
* @return $this
*/
public function setExpiryMonth($value)
{
return $this->setParameter('expiryMonth', (int) $value);
}
/**
* Get the card expiry year.
*
* @return int
*/
public function getExpiryYear()
{
return $this->getParameter('expiryYear');
}
/**
* Sets the card expiry year.
*
* @param string $value
* @return $this
*/
public function setExpiryYear($value)
{
return $this->setYearParameter('expiryYear', $value);
}
/**
* Get the card expiry date, using the specified date format string.
*
* @param string $format
*
* @return string
*/
public function getExpiryDate($format)
{
return gmdate($format, gmmktime(0, 0, 0, $this->getExpiryMonth(), 1, $this->getExpiryYear()));
}
/**
* Get the card start month.
*
* @return string
*/
public function getStartMonth()
{
return $this->getParameter('startMonth');
}
/**
* Sets the card start month.
*
* @param string $value
* @return $this
*/
public function setStartMonth($value)
{
return $this->setParameter('startMonth', (int) $value);
}
/**
* Get the card start year.
*
* @return int
*/
public function getStartYear()
{
return $this->getParameter('startYear');
}
/**
* Sets the card start year.
*
* @param string $value
* @return $this
*/
public function setStartYear($value)
{
return $this->setYearParameter('startYear', $value);
}
/**
* Get the card start date, using the specified date format string
*
* @param string $format
*
* @return string
*/
public function getStartDate($format)
{
return gmdate($format, gmmktime(0, 0, 0, $this->getStartMonth(), 1, $this->getStartYear()));
}
/**
* Get the card CVV.
*
* @return string
*/
public function getCvv()
{
return $this->getParameter('cvv');
}
/**
* Sets the card CVV.
*
* @param string $value
* @return $this
*/
public function setCvv($value)
{
return $this->setParameter('cvv', $value);
}
/**
* Get raw data for all tracks on the credit card magnetic strip.
*
* @return string
*/
public function getTracks()
{
return $this->getParameter('tracks');
}
/**
* Get raw data for track 1 on the credit card magnetic strip.
*
* @return string|null
*/
public function getTrack1()
{
return $this->getTrackByPattern('/\%B\d{1,19}\^.{2,26}\^\d{4}\d*\?/');
}
/**
* Get raw data for track 2 on the credit card magnetic strip.
*
* @return string|null
*/
public function getTrack2()
{
return $this->getTrackByPattern('/;\d{1,19}=\d{4}\d*\?/');
}
/**
* Get raw data for a track on the credit card magnetic strip based on the pattern for track 1 or 2.
*
* @param $pattern
* @return string|null
*/
protected function getTrackByPattern($pattern)
{
if ($tracks = $this->getTracks()) {
if (preg_match($pattern, $tracks, $matches) === 1) {
return $matches[0];
}
}
}
/**
* Sets raw data from all tracks on the credit card magnetic strip. Used by gateways that support card-present
* transactions.
*
* @param $value
* @return $this
*/
public function setTracks($value)
{
return $this->setParameter('tracks', $value);
}
/**
* Get the card issue number.
*
* @return string
*/
public function getIssueNumber()
{
return $this->getParameter('issueNumber');
}
/**
* Sets the card issue number.
*
* @param string $value
* @return $this
*/
public function setIssueNumber($value)
{
return $this->setParameter('issueNumber', $value);
}
/**
* Get the card billing title.
*
* @return string
*/
public function getBillingTitle()
{
return $this->getParameter('billingTitle');
}
/**
* Sets the card billing title.
*
* @param string $value
* @return $this
*/
public function setBillingTitle($value)
{
return $this->setParameter('billingTitle', $value);
}
/**
* Get the card billing name.
*
* @return string
*/
public function getBillingName()
{
return trim($this->getBillingFirstName() . ' ' . $this->getBillingLastName());
}
/**
* Split the full name in the first and last name.
*
* @param $fullName
* @return array with first and lastname
*/
protected function listFirstLastName($fullName)
{
$names = explode(' ', $fullName, 2);
return [$names[0], isset($names[1]) ? $names[1] : null];
}
/**
* Sets the card billing name.
*
* @param string $value
* @return $this
*/
public function setBillingName($value)
{
$names = $this->listFirstLastName($value);
$this->setBillingFirstName($names[0]);
$this->setBillingLastName($names[1]);
return $this;
}
/**
* Get the first part of the card billing name.
*
* @return string
*/
public function getBillingFirstName()
{
return $this->getParameter('billingFirstName');
}
/**
* Sets the first part of the card billing name.
*
* @param string $value
* @return $this
*/
public function setBillingFirstName($value)
{
return $this->setParameter('billingFirstName', $value);
}
/**
* Get the last part of the card billing name.
*
* @return string
*/
public function getBillingLastName()
{
return $this->getParameter('billingLastName');
}
/**
* Sets the last part of the card billing name.
*
* @param string $value
* @return $this
*/
public function setBillingLastName($value)
{
return $this->setParameter('billingLastName', $value);
}
/**
* Get the billing company name.
*
* @return string
*/
public function getBillingCompany()
{
return $this->getParameter('billingCompany');
}
/**
* Sets the billing company name.
*
* @param string $value
* @return $this
*/
public function setBillingCompany($value)
{
return $this->setParameter('billingCompany', $value);
}
/**
* Get the billing address, line 1.
*
* @return string
*/
public function getBillingAddress1()
{
return $this->getParameter('billingAddress1');
}
/**
* Sets the billing address, line 1.
*
* @param string $value
* @return $this
*/
public function setBillingAddress1($value)
{
return $this->setParameter('billingAddress1', $value);
}
/**
* Get the billing address, line 2.
*
* @return string
*/
public function getBillingAddress2()
{
return $this->getParameter('billingAddress2');
}
/**
* Sets the billing address, line 2.
*
* @param string $value
* @return $this
*/
public function setBillingAddress2($value)
{
return $this->setParameter('billingAddress2', $value);
}
/**
* Get the billing city.
*
* @return string
*/
public function getBillingCity()
{
return $this->getParameter('billingCity');
}
/**
* Sets billing city.
*
* @param string $value
* @return $this
*/
public function setBillingCity($value)
{
return $this->setParameter('billingCity', $value);
}
/**
* Get the billing postcode.
*
* @return string
*/
public function getBillingPostcode()
{
return $this->getParameter('billingPostcode');
}
/**
* Sets the billing postcode.
*
* @param string $value
* @return $this
*/
public function setBillingPostcode($value)
{
return $this->setParameter('billingPostcode', $value);
}
/**
* Get the billing state.
*
* @return string
*/
public function getBillingState()
{
return $this->getParameter('billingState');
}
/**
* Sets the billing state.
*
* @param string $value
* @return $this
*/
public function setBillingState($value)
{
return $this->setParameter('billingState', $value);
}
/**
* Get the billing country name.
*
* @return string
*/
public function getBillingCountry()
{
return $this->getParameter('billingCountry');
}
/**
* Sets the billing country name.
*
* @param string $value
* @return $this
*/
public function setBillingCountry($value)
{
return $this->setParameter('billingCountry', $value);
}
/**
* Get the billing phone number.
*
* @return string
*/
public function getBillingPhone()
{
return $this->getParameter('billingPhone');
}
/**
* Sets the billing phone number.
*
* @param string $value
* @return $this
*/
public function setBillingPhone($value)
{
return $this->setParameter('billingPhone', $value);
}
/**
* Get the billing phone number extension.
*
* @return string
*/
public function getBillingPhoneExtension()
{
return $this->getParameter('billingPhoneExtension');
}
/**
* Sets the billing phone number extension.
*
* @param string $value
* @return $this
*/
public function setBillingPhoneExtension($value)
{
return $this->setParameter('billingPhoneExtension', $value);
}
/**
* Get the billing fax number.
*
* @return string
*/
public function getBillingFax()
{
return $this->getParameter('billingFax');
}
/**
* Sets the billing fax number.
*
* @param string $value
* @return $this
*/
public function setBillingFax($value)
{
return $this->setParameter('billingFax', $value);
}
/**
* Get the title of the card shipping name.
*
* @return string
*/
public function getShippingTitle()
{
return $this->getParameter('shippingTitle');
}
/**
* Sets the title of the card shipping name.
*
* @param string $value
* @return $this
*/
public function setShippingTitle($value)
{
return $this->setParameter('shippingTitle', $value);
}
/**
* Get the card shipping name.
*
* @return string
*/
public function getShippingName()
{
return trim($this->getShippingFirstName() . ' ' . $this->getShippingLastName());
}
/**
* Sets the card shipping name.
*
* @param string $value
* @return $this
*/
public function setShippingName($value)
{
$names = $this->listFirstLastName($value);
$this->setShippingFirstName($names[0]);
$this->setShippingLastName($names[1]);
return $this;
}
/**
* Get the first part of the card shipping name.
*
* @return string
*/
public function getShippingFirstName()
{
return $this->getParameter('shippingFirstName');
}
/**
* Sets the first part of the card shipping name.
*