TrTaxNumberVerification::getToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 17
rs 10
1
<?php
2
3
namespace XAdam;
4
5
use GuzzleHttp\Client;
6
7
class TrTaxNumberVerification
8
{
9
    public const API_URL = 'https://ivd.gib.gov.tr/tvd_server';
10
    protected static ?Client $client = null;
11
    protected static ?string $token = null;
12
    protected static array $errorCodeList = [
13
        1 => 'İşletme tipi tüzel kişi için 1, şahıs şirketi için 2 olmalıdır!',
14
        2 => 'Vergi numarası geçerli değil!',
15
        3 => 'Servise ulaşılamıyor!',
16
        4 => 'Firma bilgileri bulunamadı!',
17
    ];
18
19
20
    public static function getClient(): Client
21
    {
22
        if (static::$client !== null) {
23
            return static::$client;
24
        }
25
26
        return static::$client = new Client();
27
    }
28
29
    public static function getToken(): string
30
    {
31
        if (static::$token !== null) {
32
            return static::$token;
33
        }
34
35
        $response = static::getClient()->post(static::API_URL . '/assos-login', [
36
            'query' => [
37
                'assoscmd' => 'cfsession',
38
                //'rtype' => 'json',
39
                'fskey' => 'intvrg.fix.session',
40
                'fuserid' => 'INTVRG_FIX',
41
                //'gn' => 'vkndogrulamalar',
42
            ]
43
        ]);
44
45
        return static::$token = json_decode($response->getBody())->token;
46
    }
47
48
    public static function makeQuery(int $company_type, string $tax_number, string $tax_office_no): array
49
    {
50
        $vkn1 = '';
51
        $tckn1 = '';
52
        if ($company_type === 1) {
53
            $vkn1 = $tax_number;
54
        } else if ($company_type === 2) {
55
            $tckn1 = $tax_number;
56
        }
57
58
        return [
59
            'dogrulama' => [
60
                'vkn1' => $vkn1,
61
                'tckn1' => $tckn1,
62
                //'iller' => substr($tax_office_no, 0, 3),
63
                'vergidaireleri' => $tax_office_no,
64
            ]
65
        ];
66
    }
67
68
    public static function verify(int $company_type, string $tax_number, string $tax_office_no)
69
    {
70
        if (!in_array($company_type, [1, 2])) {
71
            return static::errorResponse(1);
72
        }
73
74
        if (($company_type === 1 && !TrTaxNumberValidation::validate($tax_number)) || ($company_type === 2 && !TrCitizenNumberValidation::validate($tax_number))) {
0 ignored issues
show
Bug introduced by
$tax_number of type string is incompatible with the type integer expected by parameter $citizen_number of XAdam\TrCitizenNumberValidation::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        if (($company_type === 1 && !TrTaxNumberValidation::validate($tax_number)) || ($company_type === 2 && !TrCitizenNumberValidation::validate(/** @scrutinizer ignore-type */ $tax_number))) {
Loading history...
75
            return static::errorResponse(2);
76
        }
77
78
        $jp = static::makeQuery($company_type, $tax_number, $tax_office_no);
79
        $response = static::getClient()->post(static::API_URL . '/dispatch', [
80
            'query' => [
81
                'cmd' => 'vergiNoIslemleri_vergiNumarasiSorgulama',
82
                //'callid' => 'c4d3734c05df2-7',
83
                //'pageName' => 'R_INTVRG_INTVD_VERGINO_DOGRULAMA',
84
                'token' => static::getToken(),
85
                'jp' => json_encode($jp),
86
            ]
87
        ]);
88
        $response = json_decode($response->getBody());
89
90
        if (isset($response->error)) {
91
            return static::errorResponse(3);
92
        } else if (isset($response->data) && !isset($response->data->durum)) {
93
            return static::errorResponse(4);
94
        }
95
96
        return (object)[
97
            'status' => true,
98
            'data' => (object)[
99
                'title' => $response->data->unvan,
100
                'tax_no' => $response->data->vkn,
101
            ]
102
        ];
103
    }
104
105
    public static function errorResponse(int $errorCode): object
106
    {
107
        return (object)[
108
            'status' => false,
109
            'error' => (object)[
110
                'code' => $errorCode,
111
                'description' => static::$errorCodeList[$errorCode]
112
            ]
113
        ];
114
    }
115
}
116