Client::cardProcess()   C
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0018

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 36
ccs 29
cts 30
cp 0.9667
rs 6.7272
cc 7
eloc 26
nc 64
nop 14
crap 7.0018

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * Copyright 2015 Alexey Maslov <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace alxmsl\PaymentNinja;
19
20
use alxmsl\PaymentNinja\Error\ErrorException;
21
use alxmsl\PaymentNinja\Response\AuthenticateResponse;
22
use alxmsl\PaymentNinja\Response\ProcessRecurringResponse;
23
use alxmsl\PaymentNinja\Response\ProcessResponse;
24
use alxmsl\PaymentNinja\Response\SuccessResponse;
25
use alxmsl\PaymentNinja\Response\TokenResponse;
26
use alxmsl\PaymentNinja\Response\UserResponse;
27
use Closure;
28
29
/**
30
 * Payment.Ninja REST API client
31
 * @author alxmsl
32
 */
33
final class Client {
34
    /**
35
     * @var string public application key
36
     */
37
    private $publicKey = '';
38
39
    /**
40
     * @var string private application key
41
     */
42
    private $privateKey = '';
43
44
    /**
45
     * @var int request timeout, seconds
46
     */
47
    private $timeout = 10;
48
49
    /**
50
     * @param string $publicKey public application key
51
     * @param string $privateKey private application key
52
     */
53 9
    public function __construct($publicKey, $privateKey = '') {
54 9
        $this->publicKey  = (string) $publicKey;
55 9
        $this->privateKey = (string) $privateKey;
56 9
    }
57
58
    /**
59
     * REST API method user/resolve implementation
60
     * @param string $id user's unique, unchangeable identifier
61
     * @param string $email user's email address
62
     * @param string $ip user's IP address
63
     * @param null|string $displayName user's display name, if present
64
     * @param null|string $locale user's locale as ISO 639-1
65
     * @param null|string $phone user's phne number if present
66
     * @return Request user/resolved request instance
67
     * @throws ErrorException if there is an API error
68
     */
69 1
    public function userResolve($id, $email, $ip, $displayName = null, $locale = null, $phone = null) {
70
        $parameters = [
71 1
            'identifier' => (string) $id,
72 1
            'email'      => (string) $email,
73 1
            'ip'         => (string) $ip,
74 1
        ];
75 1
        if (!is_null($displayName)) {
76 1
            $parameters['display_name'] = (string) $displayName;
77 1
        }
78 1
        if (!is_null($locale)) {
79 1
            $parameters['locale'] = (string) $locale;
80 1
        }
81 1
        if (!is_null($phone)) {
82 1
            $parameters['phone'] = (string) $phone;
83 1
        }
84
        return $this->getRequest('user/resolve', $parameters, function($string) {
85
            return UserResponse::initializeByString($string);
86 1
        });
87
    }
88
89
    /**
90
     * REST API method user/changeRecurring implementation
91
     * @param string $userId user's identifier
92
     * @param null|int $interval automatic recurring interval in days (if set 0, then only manual recurring will remain active)
93
     * @param null|float $price recurring price
94
     * @param null|int $currency recurring currency as ISO 4217
95
     * @return Request user/changeRecurring request instance
96
     * @throws ErrorException if there is an API error
97
     */
98 1
    public function userChangeRecurring($userId, $interval = null, $price = null, $currency = null) {
99
        $parameters = [
100 1
            'user' => (string) $userId,
101 1
        ];
102 1
        if (!is_null($interval)) {
103 1
            $parameters['interval'] = (int) $interval;
104 1
        }
105 1
        if (!is_null($price)) {
106 1
            $parameters['price'] = (float) $price;
107 1
        }
108 1
        if (!is_null($currency)) {
109 1
            $parameters['currency'] = (string) $currency;
110 1
        }
111
        return $this->getRequest('user/changeRecurring', $parameters, function($string) {
112
            return SuccessResponse::initializeByString($string);
113 1
        });
114
    }
115
116
    /**
117
     * REST API method user/cancelRecurring implementation
118
     * @param string $userId user's identifier
119
     * @return Request user/cancelRecurring request instance
120
     * @throws ErrorException if there is an API error
121
     */
122 1
    public function userCancelRecurring($userId) {
123 1
        return $this->getRequest('user/cancelRecurring', [
124 1
            'user' => (string) $userId,
125
        ], function($string) {
126
            return SuccessResponse::initializeByString($string);
127 1
        });
128
    }
129
130
    /**
131
     * REST API method card/getToken implementation
132
     * @param string $number credit card's number
133
     * @param int $expirationMonth credit card's expiration month, without leading zero
134
     * @param int $expirationYear credit card's expiration year (4 digits)
135
     * @param string $securityCode credit card's security code: CVC, CVV2
136
     * @param null|string $callback callback function name for JSONP
137
     * @return Request card/getToken request instance
138
     * @throws ErrorException if there is an API error
139
     */
140 1
    public function cardGetToken($number, $expirationMonth, $expirationYear, $securityCode, $callback = null) {
141
        $parameters = [
142 1
            'number'           => (string) $number,
143 1
            'expiration_month' => (int) $expirationMonth,
144 1
            'expiration_year'  => (int) $expirationYear,
145 1
            'security_code'    => (string) $securityCode,
146 1
        ];
147 1
        if (!is_null($callback)) {
148 1
            $parameters['callback'] = (string) $callback;
149 1
        }
150
        return $this->getRequest('card/getToken', $parameters, function($string) {
151
            return TokenResponse::initializeByString($string);
152 1
        });
153
    }
154
155
    /**
156
     * REST API method card/process implementation
157
     * @param string $userId user's identifier
158
     * @param string $cardToken credit card token (temporary or permanent)
159
     * @param string $orderId merchant's order ID that will be returned back in a callback
160
     * @param float $price price in real currency
161
     * @param string $currency currency code as ISO 4217
162
     * @param string $description product description
163
     * @param string $ip user's IP address
164
     * @param string $acsReturnUrl URL where 3DSecure service will return user after the authentication
165
     * @param null|bool $remember indicates whether a user wants to remember his credit card in Merchant's service.
166
     *  If true , then permanenToken ​in response will contain token, that will be used for transaction processing,
167
     *  instead of temporary token
168
     * @param null|bool $verifyCard if true, then transaction price will be set to 1 EUR, that will be put on hold and
169
     *  then instantly returned
170
     * @param null|bool $recurring indicates whether a user wants to subscribe to recurring payments
171
     * @param null|int $recurringInterval automatic recurring interval in days (if not set or set to 0, then only manual
172
     *  recurring will be active)
173
     * @param null|int $recurringTrial Recurring trial period in days (first recurring payment will occur after trial).
174
     *  Recurring trial will work only if recurring interval is set
175
     * @param array $attributes custom attributes data
176
     * @return Request card/process request instance
177
     * @throws ErrorException if there is an API error
178
     */
179 1
    public function cardProcess($userId, $cardToken, $orderId, $price, $currency, $description, $ip, $acsReturnUrl,
180
                                $remember = null, $verifyCard = null, $recurring = null, $recurringInterval = null,
181
                                $recurringTrial = null, array $attributes = []) {
182
183
        $parameters = [
184 1
            'user'           => (string) $userId,
185 1
            'card_token'     => (string) $cardToken,
186 1
            'order_id'       => (string) $orderId,
187 1
            'price'          => (float) $price,
188 1
            'currency'       => (string) $currency,
189 1
            'description'    => (string) $description,
190 1
            'ip'             => (string) $ip,
191 1
            'acs_return_url' => (string) $acsReturnUrl,
192 1
        ];
193 1
        if (!is_null($remember)) {
194 1
            $parameters['remember'] = (bool) $remember;
195 1
        }
196 1
        if (!is_null($verifyCard)) {
197 1
            $parameters['verify_card'] = (bool) $verifyCard;
198 1
        }
199 1
        if (!is_null($recurring)) {
200 1
            $parameters['recurring'] = (bool) $recurring;
201 1
        }
202 1
        if (!is_null($recurringInterval)) {
203 1
            $parameters['recurring_interval'] = (int) $recurringInterval;
204 1
        }
205 1
        if (!is_null($recurringTrial)) {
206 1
            $parameters['recurring_trial'] = (int) $recurringTrial;
207 1
        }
208 1
        foreach ($attributes as $attributeName => $attributeValue) {
209 1
            $parameters[sprintf('attr_%s', $attributeName)] = $attributeValue;
210 1
        }
211
        return $this->getRequest('card/process', $parameters, function($string) {
212
            return ProcessResponse::initializeByString($string);
213 1
        });
214
    }
215
216
    /**
217
     * REST API method card/authenticate implementation
218
     * @param string $payerResponse payer authentication response. Returned from ACS to a​cs_return_url
219
     * @param string $merchantData merchant data. Returned from ACS to acs_return_url
220
     * @return Request card/authenticate request instance
221
     * @throws ErrorException if there is an API error
222
     */
223 1
    public function cardAuthenticate($payerResponse, $merchantData) {
224 1
        return $this->getRequest('card/authenticate', [
225 1
            'PaRes' => (string) $payerResponse,
226 1
            'MD'    => (string) $merchantData,
227
        ], function($string) {
228
            return AuthenticateResponse::initializeByString($string);
229 1
        });
230
    }
231
232
    /**
233
     * REST API method card/processRecurring implementation
234
     * @param string $userId user's identifier
235
     * @param float $price price in real currency
236
     * @param string $currency currency code as ISO 4217
237
     * @param null|string $orderId merchant's order ID that will be returned back in a callback
238
     * @param null|string $description product description
239
     * @return Request card/processRecurring request instance
240
     * @throws ErrorException if there is an API error
241
     */
242 1
    public function cardProcessRecurring($userId, $price, $currency, $orderId = null, $description = null) {
243
        $parameters = [
244 1
            'user'     => (string) $userId,
245 1
            'price'    => (float) $price,
246 1
            'currency' => (string) $currency,
247 1
        ];
248 1
        if (!is_null($orderId)) {
249 1
            $parameters['order_id'] = (string) $orderId;
250 1
        }
251 1
        if (!is_null($description)) {
252 1
            $parameters['description'] = (string) $description;
253 1
        }
254 1
        return $this->getRequest('card/processRecurring', $parameters, function($string) {
255
            return ProcessRecurringResponse::initializeByString($string);
256 1
        });
257
    }
258
259
    /**
260
     * Create request instance
261
     * @param string $method API method name
262
     * @param array $parameters method call parameters
263
     * @param Closure $ResponseBuilder function that building response instance
264
     * @return Request request instance for API call
265
     */
266 7
    private function getRequest($method, $parameters, $ResponseBuilder) {
267 7
        $Request = new Request($method, $ResponseBuilder, $parameters, $this->timeout);
268 7
        $Request->sign($this->publicKey, $this->privateKey);
269 7
        return $Request;
270
    }
271
272
    /**
273
     * Set request timeout
274
     * @param int $timeout seconds
275
     * @return $this
276
     */
277 1
    public function setTimeout($timeout) {
278 1
        $this->timeout = (int) $timeout;
279 1
        return $this;
280
    }
281
}
282