Completed
Push — master ( cb9cb0...a2a2e1 )
by
unknown
43:00 queued 13:11
created

AuthorizeNetCIM::updateCustomerPaymentProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 4
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
     * @param boolean $unmaskExpirationDate
197
     *
198
     * @return AuthorizeNetCIM_Response
199 1
     */
200
    public function getCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $unmaskExpirationDate = false)
201 1
    {
202 1
        $this->_constructXml("getCustomerPaymentProfileRequest");
203 1
        $this->_xml->addChild("customerProfileId", $customerProfileId);
204 1
        $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId);
205
        if ( $unmaskExpirationDate ) {
206
            $this->_xml->addChild("unmaskExpirationDate", true);
207
        }
208
209
        return $this->_sendRequest();
210
    }
211
    
212
    /**
213
     * Get a shipping address.
214
     *
215
     * @param int $customerProfileId
216
     * @param int $customerAddressId
217
     *
218
     * @return AuthorizeNetCIM_Response
219
     */
220
    public function getCustomerShippingAddress($customerProfileId, $customerAddressId)
221
    {
222
        $this->_constructXml("getCustomerShippingAddressRequest");
223
        $this->_xml->addChild("customerProfileId", $customerProfileId);
224
        $this->_xml->addChild("customerAddressId", $customerAddressId);
225
        return $this->_sendRequest();
226
    }
227
    
228
    /**
229
     * Update a profile.
230
     *
231 1
     * @param int                        $customerProfileId
232
     * @param AuthorizeNetCustomer       $customerProfile
233 1
     *
234 1
     * @return AuthorizeNetCIM_Response
235 1
     */
236 1
    public function updateCustomerProfile($customerProfileId, $customerProfile)
237 1
    {
238
        $this->_constructXml("updateCustomerProfileRequest");
239
        $customerProfile->customerProfileId = $customerProfileId;
240
        $profile = $this->_xml->addChild("profile");
241
        $this->_addObject($profile, $customerProfile);
242
        return $this->_sendRequest();
243
    }
244
    
245
    /**
246
     * Update a payment profile.
247
     *
248
     * @param int                        $customerProfileId
249
     * @param int                        $customerPaymentProfileId
250 2
     * @param AuthorizeNetPaymentProfile $paymentProfile
251
     * @param string                     $validationMode
252 2
     *
253 2
     * @return AuthorizeNetCIM_Response
254 2
     */
255 2
    public function updateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $paymentProfile, $validationMode = "none")
256 2
    {
257 2
        $this->_validationMode = $validationMode;
258 2
        $this->_constructXml("updateCustomerPaymentProfileRequest");
259
        $this->_xml->addChild("customerProfileId", $customerProfileId);
260
        $paymentProfile->customerPaymentProfileId = $customerPaymentProfileId;
261
        $profile = $this->_xml->addChild("paymentProfile");
262
        $this->_addObject($profile, $paymentProfile);
263
        return $this->_sendRequest();
264
    }
265
    
266
    /**
267
     * Update a shipping address.
268
     *
269
     * @param int                        $customerProfileId
270 1
     * @param int                        $customerShippingAddressId
271
     * @param AuthorizeNetAddress        $shippingAddress
272
     *
273 1
     * @return AuthorizeNetCIM_Response
274 1
     */
275 1
    public function updateCustomerShippingAddress($customerProfileId, $customerShippingAddressId, $shippingAddress)
276 1
    {
277 1
        
278 1
        $this->_constructXml("updateCustomerShippingAddressRequest");
279
        $this->_xml->addChild("customerProfileId", $customerProfileId);
280
        $shippingAddress->customerAddressId = $customerShippingAddressId;
281
        $sa = $this->_xml->addChild("address");
282
        $this->_addObject($sa, $shippingAddress);
283
        return $this->_sendRequest();
284
    }
285
    
286
    /**
287
     * Update the status of an existing order that contains multiple transactions with the same splitTenderId.
288
     *
289 1
     * @param int                        $splitTenderId
290
     * @param string                     $splitTenderStatus
291 1
     *
292 1
     * @return AuthorizeNetCIM_Response
293 1
     */
294 1
    public function updateSplitTenderGroup($splitTenderId, $splitTenderStatus)
295
    {
296
        $this->_constructXml("updateSplitTenderGroupRequest");
297
        $this->_xml->addChild("splitTenderId", $splitTenderId);
298
        $this->_xml->addChild("splitTenderStatus", $splitTenderStatus);
299
        return $this->_sendRequest();
300
    }
301
    
302
    /**
303
     * Validate a customer payment profile.
304
     *
305
     * @param int                        $customerProfileId
306
     * @param int                        $customerPaymentProfileId
307
     * @param int                        $customerShippingAddressId
308
     * @param int                        $cardCode
309
     * @param string                     $validationMode
310
     *
311
     * @return AuthorizeNetCIM_Response
312
     */
313
    public function validateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $customerShippingAddressId, $cardCode, $validationMode = "testMode")
314
    {
315
        $this->_validationMode = $validationMode;
316
        $this->_constructXml("validateCustomerPaymentProfileRequest");
317
        $this->_xml->addChild("customerProfileId",$customerProfileId);
318
        $this->_xml->addChild("customerPaymentProfileId",$customerPaymentProfileId);
319
        $this->_xml->addChild("customerShippingAddressId",$customerShippingAddressId);
320
        $this->_xml->addChild("cardCode",$cardCode);
321
        return $this->_sendRequest();
322
    }
323
    
324
    /**
325
     * Get hosted profile page request token
326
     *
327
     * @param string $customerProfileId
328
     * @param mixed  $settings
329
     *
330
     * @return AuthorizeNetCIM_Response
331
     */
332
    public function getHostedProfilePageRequest($customerProfileId, $settings=0)
333
    {
334
        $this->_constructXml("getHostedProfilePageRequest");
335
        $this->_xml->addChild("customerProfileId", $customerProfileId);
336
337
        if (!empty($settings)) {
338
            $hostedSettings = $this->_xml->addChild("hostedProfileSettings");
339
            foreach ($settings as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $settings of type integer is not traversable.
Loading history...
340
                $setting = $hostedSettings->addChild("setting");
341
                $setting->addChild("settingName", $key);
342
                $setting->addChild("settingValue", $val);
343
            }
344
        }
345
346
        return $this->_sendRequest();
347 7
    }
348
    
349 7
     /**
350
     * @return string
351
     */
352
    protected function _getPostUrl()
353
    {
354
        return ($this->_sandbox ? self::SANDBOX_URL : self::LIVE_URL);
355
    }
356
    
357
    /**
358
     *
359 7
     *
360
     * @param string $response
361 7
     * 
362
     * @return AuthorizeNetCIM_Response
363
     */
364
    protected function _handleResponse($response)
365
    {
366
        return new AuthorizeNetCIM_Response($response);
367 7
    }
368
    
369 7
    /**
370 7
     * Prepare the XML post string.
371
     */
372
    protected function _setPostString()
373 7
    {
374 1
        ($this->_validationMode != "none" ? $this->_xml->addChild('validationMode',$this->_validationMode) : "");
375 1
        $this->_post_string = $this->_xml->asXML();
376 1
        
377
        // Add extraOptions CDATA
378
        if ($this->_extraOptions) {
379
            $this->_xml->addChild("extraOptions");
380 7
            $this->_post_string = str_replace(array("<extraOptions></extraOptions>","<extraOptions/>"),'<extraOptions><![CDATA[' . $this->_extraOptions . ']]></extraOptions>', $this->_xml->asXML());
381 7
            $this->_extraOptions = false;
382
        }
383
        // Blank out our validation mode, so that we don't include it in calls that
384
        // don't use it.
385
        $this->_validationMode = "none";
386
    }
387
    
388 7
    /**
389
     * Start the SimpleXMLElement that will be posted.
390 7
     *
391 7
     * @param string $request_type The action to be performed.
392 7
     */
393 7
    private function _constructXml($request_type)
394 7
    {
395 7
        $string = '<?xml version="1.0" encoding="utf-8"?><'.$request_type.' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"></'.$request_type.'>';
396 7
        $this->_xml = @new SimpleXMLElement($string);
397
        $merchant = $this->_xml->addChild('merchantAuthentication');
398
        $merchant->addChild('name',$this->_api_login);
399
        $merchant->addChild('transactionKey',$this->_transaction_key);
400
        ($this->_refId ? $this->_xml->addChild('refId',$this->_refId) : "");
401
    }
402
    
403
    /**
404 5
     * Add an object to an SimpleXMLElement parent element.
405
     *
406 5
     * @param SimpleXMLElement $destination The parent element.
407 5
     * @param Object           $object      An object, array or value.  
408 5
     */
409 5
    private function _addObject($destination, $object)
410 2
    {
411 2
        $array = (array)$object;
412 2
        foreach ($array as $key => $value) {
413
            if ($value && !is_object($value)) {
414
                if (is_array($value) && count($value)) {
415 5
                    foreach ($value as $index => $item) {
416
                        $items = $destination->addChild($key);
417 5
                        $this->_addObject($items, $item);
418 3
                    }
419 5
                } else {
420
                    $destination->addChild($key,$value);
421
                }
422 5
            } elseif (is_object($value) && self::_notEmpty($value)) {
423
                $dest = $destination->addChild($key);
424
                $this->_addObject($dest, $value);
425
            }
426
        }
427
    }
428
    
429
    /**
430
     * Checks whether an array or object contains any values.
431 3
     *
432
     * @param Object $object
433 3
     *
434 3
     * @return bool
435 3
     */
436 3
    private static function _notEmpty($object)
437
    {
438 3
        $array = (array)$object;
439 3
        foreach ($array as $key => $value) {
440
            if ($value && !is_object($value)) {
441
                return true;
442
            } elseif (is_object($value)) {
443 3
                if (self::_notEmpty($value)) {
444
                    return true;
445
                }
446
            }
447
        }
448
        return false;
449
    }
450
    
451
}
452
453
/**
454
 * A class to parse a response from the CIM XML API.
455
 *
456
 * @package    AuthorizeNet
457
 * @subpackage AuthorizeNetCIM
458
 */
459 1
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...
460
{
461 1
    /**
462
     * @return AuthorizeNetAIM_Response
463
     */
464
    public function getTransactionResponse()
465
    {
466
        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...
467
    }
468
    
469
    /**
470
     * @return array Array of AuthorizeNetAIM_Response objects for each payment profile.
471
     */
472
    public function getValidationResponses()
473
    {
474
        $responses = (array)$this->xml->validationDirectResponseList;
475
        $return = array();
476
        foreach ((array)$responses["string"] as $response) {
477
            $return[] = new AuthorizeNetAIM_Response($response, ",", "", array());
478
        }
479
        return $return;
480
    }
481
    
482
    /**
483
     * @return AuthorizeNetAIM_Response
484
     */
485
    public function getValidationResponse()
486
    {
487
        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...
488 2
    }
489
    
490 2
    /**
491 2
     * @return array
492 2
     */
493
    public function getCustomerProfileIds()
494
    {
495
        $ids = (array)$this->xml->ids;
496
        if(!empty($ids))
497
            return $ids["numericString"];
498
        else
499
            return $ids;
500 1
    }
501
    
502 1
    /**
503 1
     * @return array
504 1
     */
505
    public function getCustomerPaymentProfileIds()
506
    {
507
        $ids = (array)$this->xml->customerPaymentProfileIdList;
508
        if(!empty($ids))
509
            return $ids["numericString"];
510
        else
511
            return $ids;
512 1
    }
513
    
514 1
    /**
515 1
     * @return array
516 1
     */
517
    public function getCustomerShippingAddressIds()
518
    {
519
        $ids = (array)$this->xml->customerShippingAddressIdList;
520
        if(!empty($ids))
521
            return $ids["numericString"];
522
        else
523
            return $ids;
524 1
    }
525
    
526 1
    /**
527
     * @return string
528
     */
529
    public function getCustomerAddressId()
530
    {
531
        return $this->_getElementContents("customerAddressId");
532 5
    }
533
    
534 5
    /**
535
     * @return string
536
     */
537
    public function getCustomerProfileId()
538
    {
539
        return $this->_getElementContents("customerProfileId");
540 2
    }
541
    
542 2
    /**
543
     * @return string
544
     */
545
    public function getPaymentProfileId()
546
    {
547
        return $this->_getElementContents("customerPaymentProfileId");
548
    }
549
550
}
551