Passed
Push — master ( 73f8f7...36e07c )
by Sébastien
15:06
created

Card::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use Carbon\Carbon;
6
7
class Card
8
{
9
    const ENDPOINT = '/api/cards/';
10
11
    /**
12
     * @var \Sebdesign\VivaPayments\Client
13
     */
14
    protected $client;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param \Sebdesign\VivaPayments\Client $client
20
     */
21
    public function __construct(Client $client)
22
    {
23
        $this->client = $client;
24
    }
25
26
    /**
27
     * Get a token for the credit card.
28
     *
29
     * @param  string $name          The cardholder's name
30
     * @param  string $number        The credit card number
31
     * @param  int    $cvc           The CVC number
32
     * @param  int    $month         The expiration month
33
     * @param  int    $year          The expiration year
34
     * @param  array  $guzzleOptions Additional options for the Guzzle client
35
     * @return string
36
     */
37
    public function token(
38
        string $name,
39
        string $number,
40
        int $cvc,
41
        int $month,
42
        int $year,
43
        array $guzzleOptions = []
44
    ): string {
45
        $parameters = [
46
            'CardHolderName' => $name,
47
            'Number' => $this->normalizeNumber($number),
48
            'CVC' => $cvc,
49
            'ExpirationDate' => $this->getExpirationDate($month, $year),
50
        ];
51
52
        $response = $this->client->post(self::ENDPOINT, array_merge([
53
            \GuzzleHttp\RequestOptions::FORM_PARAMS => $parameters,
54
            \GuzzleHttp\RequestOptions::QUERY => [
55
                'key'=> $this->client->getKey(),
56
            ],
57
        ], $guzzleOptions));
58
59
        return $response->Token;
60
    }
61
62
    /**
63
     * Strip non-numeric characters.
64
     */
65
    protected function normalizeNumber(string $cardNumber): string
66
    {
67
        return preg_replace('/\D/', '', $cardNumber);
68
    }
69
70
    /**
71
     * Get the expiration date.
72
     *
73
     * @param  int $month
74
     * @param  int $year
75
     * @return string
76
     */
77
    protected function getExpirationDate(int $month, int $year): string
78
    {
79
        return Carbon::createFromDate($year, $month, 15)->toDateString();
80
    }
81
82
    /**
83
     * Check for installments support.
84
     */
85
    public function installments(string $cardNumber, array $guzzleOptions = []): int
86
    {
87
        $response = $this->client->get(
88
            self::ENDPOINT.'/installments',
89
            array_merge([
90
                \GuzzleHttp\RequestOptions::HEADERS => [
91
                    'CardNumber' => $this->normalizeNumber($cardNumber),
92
                ],
93
                \GuzzleHttp\RequestOptions::QUERY => [
94
                    'key' => $this->client->getKey(),
95
                ],
96
            ], $guzzleOptions)
97
        );
98
99
        return $response->MaxInstallments;
100
    }
101
}
102