Skip to content

Commit 4eaa02b

Browse files
ivancalle-sipayXayOn
authored andcommitted
Merged in HSDK-2 (pull request #2)
HSDK-2 Approved-by: David Francos <davidfrancos@sipay.es>
2 parents bd8dc39 + 90ca8b6 commit 4eaa02b

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

src/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function autoload($class)
1919
if ($last = strrpos($class, '\\')) {
2020
$namespace = substr($class, 0, $last);
2121
$class = substr($class, $last + 1);
22-
$filename = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
22+
$filename = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $namespace)) . DIRECTORY_SEPARATOR;
2323
}
2424

2525
$filename .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

src/paymethods/Card.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
namespace Sipay\Paymethods;
3+
4+
class Card implements Paymethod
5+
{
6+
private $card_number;
7+
private $year;
8+
private $month;
9+
10+
public function __construct(string $card_number, int $year, int $month)
11+
{
12+
$this->set_card_number($card_number);
13+
$this->set_expiration_date($year, $month);
14+
}
15+
16+
public function get_card_number()
17+
{
18+
return $this->card_number;
19+
}
20+
21+
public function set_card_number(string $card_number)
22+
{
23+
if(!preg_match("/^[0-9]{14,19}$/", $card_number)) {
24+
throw new \Exception('$card_number don\'t match with pattern.');
25+
}
26+
27+
$this->card_number = $card_number;
28+
}
29+
30+
public function get_year()
31+
{
32+
return $this->year;
33+
}
34+
35+
public function get_month()
36+
{
37+
return $this->month;
38+
}
39+
40+
public function set_expiration_date(int $year, int $month)
41+
{
42+
if($year < 1000 or $year >9999) {
43+
throw new \Exception('$year doesn\'t have a correct value.');
44+
}
45+
46+
$this->year = $year;
47+
48+
if($month < 1 or $month > 12) {
49+
throw new \Exception('$month doesn\'t have a correct value.');
50+
}
51+
52+
$this->month = $month;
53+
54+
if($this->is_expirate()) {
55+
throw new \Exception('Card is expired.');
56+
}
57+
58+
}
59+
60+
private function is_expirate()
61+
{
62+
$year = intval(date('Y'));
63+
$month = intval(date('m'));
64+
return $this->year < $year or ($this->year == $year and $this->month < $month);
65+
}
66+
67+
public function to_json()
68+
{
69+
return array(
70+
'pan' => $this->card_number,
71+
'year' => $this->year,
72+
'month' => $this->month
73+
);
74+
}
75+
}

src/paymethods/FastPay.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Sipay\Paymethods;
3+
4+
class FastPay implements Paymethod
5+
{
6+
public function __construct(string $token)
7+
{
8+
$this->set_token($token);
9+
}
10+
11+
public function set_token(string $token)
12+
{
13+
if(!preg_match("/^[0-9a-fA-F]{32}$/", $token)) {
14+
throw new \Exception('$token don\'t match with pattern.');
15+
}
16+
17+
$this->token = $token;
18+
}
19+
20+
public function get_token()
21+
{
22+
return $this->token;
23+
}
24+
25+
public function to_json()
26+
{
27+
return array('fastpay' => array('request_id' => $this->token));
28+
}
29+
}

src/paymethods/Paymethod.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Sipay\Paymethods;
3+
4+
interface Paymethod
5+
{
6+
public function to_json();
7+
}

src/paymethods/StoredCard.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Sipay\Paymethods;
3+
4+
class StoredCard implements Paymethod
5+
{
6+
public function __construct(string $token)
7+
{
8+
$this->set_token($token);
9+
}
10+
11+
public function set_token(string $token)
12+
{
13+
if(!preg_match("/^[\w-]{6,128}$/", $token)) {
14+
throw new \Exception('$token don\'t match with pattern.');
15+
}
16+
17+
$this->token = $token;
18+
}
19+
20+
public function get_token()
21+
{
22+
return $this->token;
23+
}
24+
25+
public function to_json()
26+
{
27+
return array('token' => $this->token);
28+
}
29+
}

0 commit comments

Comments
 (0)