Completed
Branch master (e11220)
by John
02:37
created

AuthnetApiFactory::getWebhooksURL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the AuthnetJSON package.
5
 *
6
 * (c) John Conde <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace JohnConde\Authnet;
13
14
use \Curl\Curl;
15
16
/**
17
 * Factory to instantiate an instance of an AuthnetJson object with the proper endpoint
18
 * URL and Processor Class
19
 *
20
 * @package    AuthnetJSON
21
 * @author     John Conde <[email protected]>
22
 * @copyright  John Conde <[email protected]>
23
 * @license    http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
24
 * @link       https://github.com/stymiee/authnetjson
25
 */
26
27
class AuthnetApiFactory
28
{
29
    /**
30
     * @const Indicates use of Authorize.Net's production server
31
     */
32
    const USE_PRODUCTION_SERVER = 0;
33
34
    /**
35
     * @const Indicates use of the development server
36
     */
37
    const USE_DEVELOPMENT_SERVER = 1;
38
39
    /**
40
     * @const Indicates use of the Akamai endpoint
41
     */
42
    const USE_AKAMAI_SERVER = 2;
43
44
    /**
45
     * Validates the Authorize.Net credentials and returns a Request object to be used to make an API call
46
     *
47
     * @param   string      $login                          Authorize.Net API Login ID
48
     * @param   string      $transaction_key                Authorize.Net API Transaction Key
49
     * @param   integer     $server                         ID of which server to use (optional)
50
     * @return  object      \JohnConde\Authnet\AuthnetJson
51
     * @throws  \JohnConde\Authnet\AuthnetInvalidCredentialsException
52
     */
53
    public static function getJsonApiHandler($login, $transaction_key, $server = self::USE_AKAMAI_SERVER)
54
    {
55
        $login           = trim($login);
56
        $transaction_key = trim($transaction_key);
57
        $api_url         = static::getWebServiceURL($server);
58
59
        if (empty($login) || empty($transaction_key)) {
60
            throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
61
        }
62
63
        $curl = new Curl();
64
        $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
65
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
66
        $curl->setOpt(CURLOPT_HEADER, false);
67
        $curl->setHeader('Content-Type', 'text/json');
68
69
        $object = new AuthnetJsonRequest($login, $transaction_key, $api_url);
70
        $object->setProcessHandler($curl);
71
72
        return $object;
73
    }
74
75
    /**
76
     * Gets the API endpoint to be used for a JSON API call
77
     *
78
     * @param   integer     $server     ID of which server to use
79
     * @return  string                  The URL endpoint the request is to be sent to
80
     * @throws  \JohnConde\Authnet\AuthnetInvalidServerException
81
     */
82
    protected static function getWebServiceURL($server)
83
    {
84
        if ($server === static::USE_PRODUCTION_SERVER) {
85
            $url = 'https://api.authorize.net/xml/v1/request.api';
86
        }
87
        else if ($server === static::USE_DEVELOPMENT_SERVER) {
88
            $url = 'https://apitest.authorize.net/xml/v1/request.api';
89
        }
90
        else if ($server === static::USE_AKAMAI_SERVER) {
91
            $url = 'https://api2.authorize.net/xml/v1/request.api';
92
        }
93
        else {
94
            throw new AuthnetInvalidServerException('You did not provide a valid server.');
95
        }
96
        return $url;
97
    }
98
99
    /**
100
     * Validates the Authorize.Net credentials and returns a SIM object to be used to make a SIM API call
101
     *
102
     * @param   string      $login                          Authorize.Net API Login ID
103
     * @param   string      $transaction_key                Authorize.Net API Transaction Key
104
     * @param   integer     $server                         ID of which server to use (optional)
105
     * @return  object      \JohnConde\Authnet\AuthnetSim
106
     * @throws  \JohnConde\Authnet\AuthnetInvalidCredentialsException
107
     */
108
    public static function getSimHandler($login, $transaction_key, $server = self::USE_PRODUCTION_SERVER)
109
    {
110
        $login           = trim($login);
111
        $transaction_key = trim($transaction_key);
112
        $api_url         = static::getSimURL($server);
113
114
        if (empty($login) || empty($transaction_key)) {
115
            throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
116
        }
117
118
        return new AuthnetSim($login, $transaction_key, $api_url);
119
    }
120
121
    /**
122
     * Gets the API endpoint to be used for a SIM API call
123
     *
124
     * @param   integer     $server     ID of which server to use
125
     * @return  string                  The URL endpoint the request is to be sent to
126
     * @throws  \JohnConde\Authnet\AuthnetInvalidServerException
127
     */
128 View Code Duplication
    protected static function getSimURL($server)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        if ($server === static::USE_PRODUCTION_SERVER) {
131
            $url = 'https://secure2.authorize.net/gateway/transact.dll';
132
        }
133
        else if ($server === static::USE_DEVELOPMENT_SERVER) {
134
            $url = 'https://test.authorize.net/gateway/transact.dll';
135
        }
136
        else {
137
            throw new AuthnetInvalidServerException('You did not provide a valid server.');
138
        }
139
        return $url;
140
    }
141
142
    /**
143
     * Validates the Authorize.Net credentials and returns a Webhooks Request object to be used to make an Webhook call
144
     *
145
     * @param   string      $login                          Authorize.Net API Login ID
146
     * @param   string      $transaction_key                Authorize.Net API Transaction Key
147
     * @param   integer     $server                         ID of which server to use (optional)
148
     * @return  object      \JohnConde\Authnet\AuthnetJson
149
     * @throws  \JohnConde\Authnet\AuthnetInvalidCredentialsException
150
     */
151
    public static function getWebhooksHandler($login, $transaction_key, $server = self::USE_PRODUCTION_SERVER)
152
    {
153
        $login           = trim($login);
154
        $transaction_key = trim($transaction_key);
155
        $api_url         = static::getWebhooksURL($server);
156
157
        if (empty($login) || empty($transaction_key)) {
158
            throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
159
        }
160
161
        $base64credentials = base64_encode(sprintf('%s:%s', $login, $transaction_key));
162
163
        $curl = new Curl();
164
        $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
165
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
166
        $curl->setOpt(CURLOPT_HEADER, false);
167
        $curl->setHeader('Content-Type', 'application/json');
168
        $curl->setHeader('Authorization', sprintf('Basic %s', $base64credentials));
169
170
        $object = new AuthnetWebhooksRequest($api_url);
171
        $object->setProcessHandler($curl);
172
173
        return $object;
174
    }
175
176
    /**
177
     * Gets the API endpoint to be used for a SIM API call
178
     *
179
     * @param   integer     $server     ID of which server to use
180
     * @return  string                  The URL endpoint the request is to be sent to
181
     * @throws  \JohnConde\Authnet\AuthnetInvalidServerException
182
     */
183 View Code Duplication
    protected static function getWebhooksURL($server)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        if ($server === static::USE_PRODUCTION_SERVER) {
186
            $url = 'https://api.authorize.net/rest/v1/';
187
        }
188
        else if ($server === static::USE_DEVELOPMENT_SERVER) {
189
            $url = 'https://apitest.authorize.net/rest/v1/';
190
        }
191
        else {
192
            throw new AuthnetInvalidServerException('You did not provide a valid server.');
193
        }
194
        return $url;
195
    }
196
}