Completed
Pull Request — master (#114)
by Naman
27:16
created

AuthorizeNetCIM::getCustomerPaymentProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Easily interact with the Authorize.Net CIM XML API.
4
 *
5
 * @package    AuthorizeNet
6
 * @subpackage AuthorizeNetCIM
7
 * @link       http://www.authorize.net/support/CIM_XML_guide.pdf CIM XML Guide
8
 */
9
10
11
12
/**
13
 * A class to send a request to the CIM XML API.
14
 *
15
 * @package    AuthorizeNet
16
 * @subpackage AuthorizeNetCIM
17
 */ 
18
class AuthorizeNetCIM extends AuthorizeNetRequest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
21
    const LIVE_URL = "https://api2.authorize.net/xml/v1/request.api";
22
    const SANDBOX_URL = "https://apitest.authorize.net/xml/v1/request.api";
23
24
    
25
    private $_xml;
26
    private $_refId = false;
27
    private $_validationMode = "none"; // "none","testMode","liveMode"
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
    private $_extraOptions;
29
    private $_transactionTypes = array(
0 ignored issues
show
Unused Code introduced by
The property $_transactionTypes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        'AuthOnly',
31
        'AuthCapture',
32
        'CaptureOnly',
33
        'PriorAuthCapture',
34
        'Refund',
35
        'Void',
36
    );
37
    
38
    /**
39
     * Optional. Used if the merchant wants to set a reference ID.
40
     *
41
     * @param string $refId
42
     */
43
    public function setRefId($refId)
44
    {
45
        $this->_refId = $refId;
0 ignored issues
show
Documentation Bug introduced by
The property $_refId was declared of type boolean, but $refId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
    }
47
    
48
    /**
49
     * Create a customer profile.
50
     *
51
     * @param AuthorizeNetCustomer $customerProfile
52
     * @param string               $validationMode
53
     *
54
     * @return AuthorizeNetCIM_Response
55
     */
56 5
    public function createCustomerProfile($customerProfile, $validationMode = "none")
57
    {
58 5
        $this->_validationMode = $validationMode;
59 5
        $this->_constructXml("createCustomerProfileRequest");
60 5
        $profile = $this->_xml->addChild("profile");
61 5
        $this->_addObject($profile, $customerProfile);
62 5
        return $this->_sendRequest();
63
    }
64
    
65
    /**
66
     * Create a customer payment profile.
67
     *
68
     * @param int                        $customerProfileId
69
     * @param AuthorizeNetPaymentProfile $paymentProfile
70
     * @param string                     $validationMode
71
     *
72
     * @return AuthorizeNetCIM_Response
73
     */
74 2
    public function createCustomerPaymentProfile($customerProfileId, $paymentProfile, $validationMode = "none")
75
    {
76 2
        $this->_validationMode = $validationMode;
77 2
        $this->_constructXml("createCustomerPaymentProfileRequest");
78 2
        $this->_xml->addChild("customerProfileId", $customerProfileId);
79 2
        $profile = $this->_xml->addChild("paymentProfile");
80 2
        $this->_addObject($profile, $paymentProfile);
81 2
        return $this->_sendRequest();
82
    }
83
    
84
    /**
85
     * Create a shipping address.
86
     *
87
     * @param int                        $customerProfileId
88
     * @param AuthorizeNetAddress        $shippingAddress
89
     *
90
     * @return AuthorizeNetCIM_Response
91
     */
92 1
    public function createCustomerShippingAddress($customerProfileId, $shippingAddress)
93
    {
94 1
        $this->_constructXml("createCustomerShippingAddressRequest");
95 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
96 1
        $address = $this->_xml->addChild("address");
97 1
        $this->_addObject($address, $shippingAddress);
98 1
        return $this->_sendRequest();
99
    }
100
    
101
    /**
102
     * Create a transaction.
103
     *
104
     * @param string                     $transactionType
105
     * @param AuthorizeNetTransaction    $transaction
106
     * @param string                     $extraOptionsString
107
     *
108
     * @return AuthorizeNetCIM_Response
109
     */
110 1
    public function createCustomerProfileTransaction($transactionType, $transaction, $extraOptionsString = "")
111
    {
112 1
        $this->_constructXml("createCustomerProfileTransactionRequest");
113 1
        $transactionParent = $this->_xml->addChild("transaction");
114 1
        $transactionChild = $transactionParent->addChild("profileTrans" . $transactionType);
115 1
        $this->_addObject($transactionChild, $transaction);
116 1
        $this->_extraOptions = $extraOptionsString . "x_encap_char=|";
117 1
        return $this->_sendRequest();
118
    }
119
    
120
    /**
121
     * Delete a customer profile.
122
     *
123
     * @param int $customerProfileId
124
     *
125
     * @return AuthorizeNetCIM_Response
126
     */
127 2
    public function deleteCustomerProfile($customerProfileId)
128
    {
129 2
        $this->_constructXml("deleteCustomerProfileRequest");
130 2
        $this->_xml->addChild("customerProfileId", $customerProfileId);
131 2
        return $this->_sendRequest();
132
    }
133
    
134
    /**
135
     * Delete a payment profile.
136
     *
137
     * @param int $customerProfileId
138
     * @param int $customerPaymentProfileId
139
     *
140
     * @return AuthorizeNetCIM_Response
141
     */
142 1
    public function deleteCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId)
143
    {
144 1
        $this->_constructXml("deleteCustomerPaymentProfileRequest");
145 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
146 1
        $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId);
147 1
        return $this->_sendRequest();
148
    }
149
    
150
    /**
151
     * Delete a shipping address.
152
     *
153
     * @param int $customerProfileId
154
     * @param int $customerAddressId
155
     *
156
     * @return AuthorizeNetCIM_Response
157
     */
158 1
    public function deleteCustomerShippingAddress($customerProfileId, $customerAddressId)
159
    {
160 1
        $this->_constructXml("deleteCustomerShippingAddressRequest");
161 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
162 1
        $this->_xml->addChild("customerAddressId", $customerAddressId);
163 1
        return $this->_sendRequest();
164
    }
165
    
166
    /**
167
     * Get all customer profile ids.
168
     *
169
     * @return AuthorizeNetCIM_Response
170
     */
171 2
    public function getCustomerProfileIds()
172
    {
173 2
        $this->_constructXml("getCustomerProfileIdsRequest");
174 2
        return $this->_sendRequest();
175
    }
176
    
177
    /**
178
     * Get a customer profile.
179
     *
180
     * @param int $customerProfileId
181
     *
182
     * @return AuthorizeNetCIM_Response
183
     */
184 2
    public function getCustomerProfile($customerProfileId)
185
    {
186 2
        $this->_constructXml("getCustomerProfileRequest");
187 2
        $this->_xml->addChild("customerProfileId", $customerProfileId);
188 2
        return $this->_sendRequest();
189
    }
190
    
191
    /**
192
     * Get a payment profile.
193
     *
194
     * @param int $customerProfileId
195
     * @param int $customerPaymentProfileId
196
     *
197
     * @return AuthorizeNetCIM_Response
198
     */
199 1
    public function getCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId)
200
    {
201 1
        $this->_constructXml("getCustomerPaymentProfileRequest");
202 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
203 1
        $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId);
204 1
        return $this->_sendRequest();
205
    }
206
    
207
    /**
208
     * Get a shipping address.
209
     *
210
     * @param int $customerProfileId
211
     * @param int $customerAddressId
212
     *
213
     * @return AuthorizeNetCIM_Response
214
     */
215
    public function getCustomerShippingAddress($customerProfileId, $customerAddressId)
216
    {
217
        $this->_constructXml("getCustomerShippingAddressRequest");
218
        $this->_xml->addChild("customerProfileId", $customerProfileId);
219
        $this->_xml->addChild("customerAddressId", $customerAddressId);
220
        return $this->_sendRequest();
221
    }
222
    
223
    /**
224
     * Update a profile.
225
     *
226
     * @param int                        $customerProfileId
227
     * @param AuthorizeNetCustomer       $customerProfile
228
     *
229
     * @return AuthorizeNetCIM_Response
230
     */
231 1
    public function updateCustomerProfile($customerProfileId, $customerProfile)
232
    {
233 1
        $this->_constructXml("updateCustomerProfileRequest");
234 1
        $customerProfile->customerProfileId = $customerProfileId;
235 1
        $profile = $this->_xml->addChild("profile");
236 1
        $this->_addObject($profile, $customerProfile);
237 1
        return $this->_sendRequest();
238
    }
239
    
240
    /**
241
     * Update a payment profile.
242
     *
243
     * @param int                        $customerProfileId
244
     * @param int                        $customerPaymentProfileId
245
     * @param AuthorizeNetPaymentProfile $paymentProfile
246
     * @param string                     $validationMode
247
     *
248
     * @return AuthorizeNetCIM_Response
249
     */
250 2
    public function updateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $paymentProfile, $validationMode = "none")
251
    {
252 2
        $this->_validationMode = $validationMode;
253 2
        $this->_constructXml("updateCustomerPaymentProfileRequest");
254 2
        $this->_xml->addChild("customerProfileId", $customerProfileId);
255 2
        $paymentProfile->customerPaymentProfileId = $customerPaymentProfileId;
256 2
        $profile = $this->_xml->addChild("paymentProfile");
257 2
        $this->_addObject($profile, $paymentProfile);
258 2
        return $this->_sendRequest();
259
    }
260
    
261
    /**
262
     * Update a shipping address.
263
     *
264
     * @param int                        $customerProfileId
265
     * @param int                        $customerShippingAddressId
266
     * @param AuthorizeNetAddress        $shippingAddress
267
     *
268
     * @return AuthorizeNetCIM_Response
269
     */
270 1
    public function updateCustomerShippingAddress($customerProfileId, $customerShippingAddressId, $shippingAddress)
271
    {
272
        
273 1
        $this->_constructXml("updateCustomerShippingAddressRequest");
274 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
275 1
        $shippingAddress->customerAddressId = $customerShippingAddressId;
276 1
        $sa = $this->_xml->addChild("address");
277 1
        $this->_addObject($sa, $shippingAddress);
278 1
        return $this->_sendRequest();
279
    }
280
    
281
    /**
282
     * Update the status of an existing order that contains multiple transactions with the same splitTenderId.
283
     *
284
     * @param int                        $splitTenderId
285
     * @param string                     $splitTenderStatus
286
     *
287
     * @return AuthorizeNetCIM_Response
288
     */
289 1
    public function updateSplitTenderGroup($splitTenderId, $splitTenderStatus)
290
    {
291 1
        $this->_constructXml("updateSplitTenderGroupRequest");
292 1
        $this->_xml->addChild("splitTenderId", $splitTenderId);
293 1
        $this->_xml->addChild("splitTenderStatus", $splitTenderStatus);
294 1
        return $this->_sendRequest();
295
    }
296
    
297
    /**
298
     * Validate a customer payment profile.
299
     *
300
     * @param int                        $customerProfileId
301
     * @param int                        $customerPaymentProfileId
302
     * @param int                        $customerShippingAddressId
303
     * @param int                        $cardCode
304
     * @param string                     $validationMode
305
     *
306
     * @return AuthorizeNetCIM_Response
307
     */
308
    public function validateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $customerShippingAddressId, $cardCode, $validationMode = "testMode")
309
    {
310
        $this->_validationMode = $validationMode;
311
        $this->_constructXml("validateCustomerPaymentProfileRequest");
312
        $this->_xml->addChild("customerProfileId",$customerProfileId);
313
        $this->_xml->addChild("customerPaymentProfileId",$customerPaymentProfileId);
314
        $this->_xml->addChild("customerShippingAddressId",$customerShippingAddressId);
315
        $this->_xml->addChild("cardCode",$cardCode);
316
        return $this->_sendRequest();
317
    }
318
    
319
    /**
320
     * Get hosted profile page request token
321
     *
322
     * @param string $customerProfileId
323
     * @param mixed  $settings
324
     *
325
     * @return AuthorizeNetCIM_Response
326
     */
327
    public function getHostedProfilePageRequest($customerProfileId, $settings=0)
328
    {
329
        $this->_constructXml("getHostedProfilePageRequest");
330
        $this->_xml->addChild("customerProfileId", $customerProfileId);
331
332
        if (!empty($settings)) {
333
            $hostedSettings = $this->_xml->addChild("hostedProfileSettings");
334
            foreach ($settings as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $settings of type integer is not traversable.
Loading history...
335
                $setting = $hostedSettings->addChild("setting");
336
                $setting->addChild("settingName", $key);
337
                $setting->addChild("settingValue", $val);
338
            }
339
        }
340
341
        return $this->_sendRequest();
342
    }
343
    
344
     /**
345
     * @return string
346
     */
347 7
    protected function _getPostUrl()
348
    {
349 7
        return ($this->_sandbox ? self::SANDBOX_URL : self::LIVE_URL);
350
    }
351
    
352
    /**
353
     *
354
     *
355
     * @param string $response
356
     * 
357
     * @return AuthorizeNetCIM_Response
358
     */
359 7
    protected function _handleResponse($response)
360
    {
361 7
        return new AuthorizeNetCIM_Response($response);
362
    }
363
    
364
    /**
365
     * Prepare the XML post string.
366
     */
367 7
    protected function _setPostString()
368
    {
369 7
        ($this->_validationMode != "none" ? $this->_xml->addChild('validationMode',$this->_validationMode) : "");
370 7
        $this->_post_string = $this->_xml->asXML();
371
        
372
        // Add extraOptions CDATA
373 7
        if ($this->_extraOptions) {
374 1
            $this->_xml->addChild("extraOptions");
375 1
            $this->_post_string = str_replace(array("<extraOptions></extraOptions>","<extraOptions/>"),'<extraOptions><![CDATA[' . $this->_extraOptions . ']]></extraOptions>', $this->_xml->asXML());
376 1
            $this->_extraOptions = false;
377
        }
378
        // Blank out our validation mode, so that we don't include it in calls that
379
        // don't use it.
380 7
        $this->_validationMode = "none";
381 7
    }
382
    
383
    /**
384
     * Start the SimpleXMLElement that will be posted.
385
     *
386
     * @param string $request_type The action to be performed.
387
     */
388 7
    private function _constructXml($request_type)
389
    {
390 7
        $string = '<?xml version="1.0" encoding="utf-8"?><'.$request_type.' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"></'.$request_type.'>';
391 7
        $this->_xml = @new SimpleXMLElement($string);
392 7
        $merchant = $this->_xml->addChild('merchantAuthentication');
393 7
        $merchant->addChild('name',$this->_api_login);
394 7
        $merchant->addChild('transactionKey',$this->_transaction_key);
395 7
        ($this->_refId ? $this->_xml->addChild('refId',$this->_refId) : "");
396 7
    }
397
    
398
    /**
399
     * Add an object to an SimpleXMLElement parent element.
400
     *
401
     * @param SimpleXMLElement $destination The parent element.
402
     * @param Object           $object      An object, array or value.  
403
     */
404 5
    private function _addObject($destination, $object)
405
    {
406 5
        $array = (array)$object;
407 5
        foreach ($array as $key => $value) {
408 5
            if ($value && !is_object($value)) {
409 5
                if (is_array($value) && count($value)) {
410 2
                    foreach ($value as $index => $item) {
411 2
                        $items = $destination->addChild($key);
412 2
                        $this->_addObject($items, $item);
413
                    }
414
                } else {
415 5
                    $destination->addChild($key,$value);
416
                }
417 5
            } elseif (is_object($value) && self::_notEmpty($value)) {
418 3
                $dest = $destination->addChild($key);
419 5
                $this->_addObject($dest, $value);
420
            }
421
        }
422 5
    }
423
    
424
    /**
425
     * Checks whether an array or object contains any values.
426
     *
427
     * @param Object $object
428
     *
429
     * @return bool
430
     */
431 3
    private static function _notEmpty($object)
432
    {
433 3
        $array = (array)$object;
434 3
        foreach ($array as $key => $value) {
435 3
            if ($value && !is_object($value)) {
436 3
                return true;
437
            } elseif (is_object($value)) {
438 3
                if (self::_notEmpty($value)) {
439 3
                    return true;
440
                }
441
            }
442
        }
443 3
        return false;
444
    }
445
    
446
}
447
448
/**
449
 * A class to parse a response from the CIM XML API.
450
 *
451
 * @package    AuthorizeNet
452
 * @subpackage AuthorizeNetCIM
453
 */
454
class AuthorizeNetCIM_Response extends AuthorizeNetXMLResponse
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
455
{
456
    /**
457
     * @return AuthorizeNetAIM_Response
458
     */
459 1
    public function getTransactionResponse()
460
    {
461 1
        return new AuthorizeNetAIM_Response($this->_getElementContents("directResponse"), ",", "|", array());
0 ignored issues
show
Security Bug introduced by
It seems like $this->_getElementContents('directResponse') targeting AuthorizeNetXMLResponse::_getElementContents() can also be of type false; however, AuthorizeNetAIM_Response::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
462
    }
463
    
464
    /**
465
     * @return array Array of AuthorizeNetAIM_Response objects for each payment profile.
466
     */
467
    public function getValidationResponses()
468
    {
469
        $responses = (array)$this->xml->validationDirectResponseList;
470
        $return = array();
471
        foreach ((array)$responses["string"] as $response) {
472
            $return[] = new AuthorizeNetAIM_Response($response, ",", "", array());
473
        }
474
        return $return;
475
    }
476
    
477
    /**
478
     * @return AuthorizeNetAIM_Response
479
     */
480
    public function getValidationResponse()
481
    {
482
        return new AuthorizeNetAIM_Response($this->_getElementContents("validationDirectResponse"), ",", "|", array());
0 ignored issues
show
Security Bug introduced by
It seems like $this->_getElementConten...idationDirectResponse') targeting AuthorizeNetXMLResponse::_getElementContents() can also be of type false; however, AuthorizeNetAIM_Response::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
483
    }
484
    
485
    /**
486
     * @return array
487
     */
488 2
    public function getCustomerProfileIds()
489
    {
490 2
        $ids = (array)$this->xml->ids;
491 2
        if(!empty($ids))
492 2
            return $ids["numericString"];
493
        else
494
            return $ids;
495
    }
496
    
497
    /**
498
     * @return array
499
     */
500 1
    public function getCustomerPaymentProfileIds()
501
    {
502 1
        $ids = (array)$this->xml->customerPaymentProfileIdList;
503 1
        if(!empty($ids))
504 1
            return $ids["numericString"];
505
        else
506
            return $ids;
507
    }
508
    
509
    /**
510
     * @return array
511
     */
512 1
    public function getCustomerShippingAddressIds()
513
    {
514 1
        $ids = (array)$this->xml->customerShippingAddressIdList;
515 1
        if(!empty($ids))
516 1
            return $ids["numericString"];
517
        else
518
            return $ids;
519
    }
520
    
521
    /**
522
     * @return string
523
     */
524 1
    public function getCustomerAddressId()
525
    {
526 1
        return $this->_getElementContents("customerAddressId");
527
    }
528
    
529
    /**
530
     * @return string
531
     */
532 5
    public function getCustomerProfileId()
533
    {
534 5
        return $this->_getElementContents("customerProfileId");
535
    }
536
    
537
    /**
538
     * @return string
539
     */
540 2
    public function getPaymentProfileId()
541
    {
542 2
        return $this->_getElementContents("customerPaymentProfileId");
543
    }
544
545
}
546