Issues (19)

src/Authnetjson/AuthnetSim.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the AuthnetJSON package.
7
 *
8
 * (c) John Conde <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Authnetjson;
15
16
use Authnetjson\Exception\AuthnetInvalidAmountException;
17
use Exception;
18
19
/**
20
 * Wrapper to simplify the creation of SIM data
21
 *
22
 * @author    John Conde <[email protected]>
23
 * @copyright 2015 - 2023 John Conde <[email protected]>
24
 * @license   http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
25
 * @link      https://github.com/stymiee/authnetjson
26
 */
27
class AuthnetSim
28
{
29
    /**
30
     * @var string  Authorize.Net API login ID
31
     */
32
    private $login;
33
34
    /**
35
     * @var string  Authorize.Net API signature key
36
     */
37
    private $signature;
38
39
    /**
40
     * @var string  URL endpoint for processing a transaction
41
     */
42
    private $url;
43
44
    /**
45
     * @var int  Randomly generated number
0 ignored issues
show
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
46
     */
47
    private $sequence;
48
49
    /**
50
     * @var int  Unix timestamp the request was made
0 ignored issues
show
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
51
     */
52
    private $timestamp;
53
54
    /**
55
     * Creates a SIM wrapper by setting the Authorize.Net credentials and URL of the endpoint to be used
56
     * for the API call
57
     *
58
     * @param string $login Authorize.Net API login ID
59
     * @param string $signature Authorize.Net API Transaction Key
60
     * @param string $api_url URL endpoint for processing a transaction
61
     * @throws Exception
62
     */
63 1
    public function __construct(string $login, string $signature, string $api_url)
64
    {
65 1
        $this->login = $login;
66 1
        $this->signature = $signature;
67 1
        $this->url = $api_url;
68 1
        $this->resetParameters();
69 1
    }
70
71
    /**
72
     * Returns the hash for the SIM transaction
73
     *
74
     * @param float $amount The amount of the transaction
75
     * @return string           Hash of five different unique transaction parameters
76
     * @throws AuthnetInvalidAmountException
77
     */
78 2
    public function getFingerprint(float $amount): string
79
    {
80 2
        if (!filter_var($amount, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND)) {
81 1
            throw new AuthnetInvalidAmountException('You must enter a valid amount greater than zero.');
82
        }
83
84 1
        return strtoupper(
85 1
            hash_hmac(
86 1
                'sha512',
87 1
                sprintf(
88 1
                    '%s^%s^%s^%s^',
89 1
                    $this->login,
90 1
                    $this->sequence,
91 1
                    $this->timestamp,
92 1
                    $amount
93
                ),
94 1
                hex2bin($this->signature)
95
            )
96
        );
97
    }
98
99
    /**
100
     * Returns the sequence generated for a transaction
101
     *
102
     * @return int Current sequence
103
     */
104 1
    public function getSequence(): int
105
    {
106 1
        return $this->sequence;
107
    }
108
109
    /**
110
     * Returns the timestamp for a transaction
111
     *
112
     * @return int Current timestamp
113
     */
114 1
    public function getTimestamp(): int
115
    {
116 1
        return $this->timestamp;
117
    }
118
119
    /**
120
     * Returns the account login ID
121
     *
122
     * @return string           API login ID
123
     */
124 1
    public function getLogin(): string
125
    {
126 1
        return $this->login;
127
    }
128
129
    /**
130
     * Returns the url endpoint for the transaction
131
     *
132
     * @return string           url endpoint
133
     */
134 1
    public function getEndpoint(): string
135
    {
136 1
        return $this->url;
137
    }
138
139
    /**
140
     * Resets the sequence and timestamp
141
     *
142
     * @throws Exception
143
     */
144 1
    public function resetParameters(): void
145
    {
146 1
        $this->sequence = random_int(1, 1000);
147 1
        $this->timestamp = time();
148 1
    }
149
}
150