Skip to content

Commit

Permalink
Merge pull request #130 from anush/xml_array
Browse files Browse the repository at this point in the history
Handle XML to JSON array conversion issue when a single payment profile is present
  • Loading branch information
judgej authored Apr 27, 2019
2 parents 1fe6f4e + 9145e44 commit 1c2c341
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/Message/CIMGetProfileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function getMatchingPaymentProfileId($last4)
return null;
}

// Handle quirkiness with XML -> JSON conversion
if (!array_key_exists(0, $this->data['profile']['paymentProfiles'])) {
$this->data['profile']['paymentProfiles'] = [$this->data['profile']['paymentProfiles']];
}

foreach ($this->data['profile']['paymentProfiles'] as $paymentProfile) {
// For every payment profile check if the last4 matches the last4 of the card in request.
$cardLast4 = substr($paymentProfile['payment']['creditCard']['cardNumber'], -4);
Expand Down
10 changes: 5 additions & 5 deletions tests/CIMGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testCreateCardFromOpaqueDataSuccess()
public function testShouldCreateCardIfDuplicateCustomerProfileExists()
{
$this->setMockHttpResponse(array('CIMCreateCardFailureWithDuplicate.txt', 'CIMCreatePaymentProfileSuccess.txt',
'CIMGetProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));
'CIMGetMultipleProfilesSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));

$response = $this->gateway->createCard($this->createCardOptions)->send();

Expand All @@ -133,7 +133,7 @@ public function testShouldCreateCardIfDuplicateCustomerProfileExists()
public function testShouldCreateCardFromOpaqueDataIfDuplicateCustomerProfileExists()
{
$this->setMockHttpResponse(array('CIMCreateCardFailureWithDuplicate.txt', 'CIMCreatePaymentProfileSuccess.txt',
'CIMGetProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));
'CIMGetMultipleProfilesSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));

$response = $this->gateway->createCard($this->createCardFromOpaqueDataOptions)->send();

Expand All @@ -149,7 +149,7 @@ public function testShouldUpdateExistingPaymentProfileIfDuplicateExistsAndForceC
{
// Duplicate **payment** profile
$this->setMockHttpResponse(array('CIMCreateCardFailureWithDuplicate.txt', 'CIMCreatePaymentProfileFailure.txt',
'CIMGetProfileSuccess.txt', 'CIMUpdatePaymentProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));
'CIMGetMultipleProfilesSuccess.txt', 'CIMUpdatePaymentProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));

$response = $this->gateway->createCard($this->createCardOptions)->send();

Expand All @@ -165,7 +165,7 @@ public function testDoesntUpdateExistingPaymentProfileFromOpaqueData()
{
// Duplicate **payment** profile
$this->setMockHttpResponse(array('CIMCreateCardFailureWithDuplicate.txt', 'CIMCreatePaymentProfileFailure.txt',
'CIMGetProfileSuccess.txt', 'CIMUpdatePaymentProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));
'CIMGetMultipleProfilesSuccess.txt', 'CIMUpdatePaymentProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));

$response = $this->gateway->createCard($this->createCardFromOpaqueDataOptions)->send();

Expand All @@ -175,7 +175,7 @@ public function testDoesntUpdateExistingPaymentProfileFromOpaqueData()
public function testShouldUpdateExistingPaymentProfileIfDuplicateExistsAndMaxPaymentProfileLimitIsMet()
{
$this->setMockHttpResponse(array('CIMCreateCardFailureWithDuplicate.txt',
'CIMCreatePaymentProfileFailureMaxProfileLimit.txt', 'CIMGetProfileSuccess.txt',
'CIMCreatePaymentProfileFailureMaxProfileLimit.txt', 'CIMGetMultipleProfilesSuccess.txt',
'CIMUpdatePaymentProfileSuccess.txt', 'CIMGetPaymentProfileSuccess.txt'));

$response = $this->gateway->createCard($this->createCardOptions)->send();
Expand Down
14 changes: 11 additions & 3 deletions tests/Message/CIMGetProfileResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Omnipay\AuthorizeNet\Message;

use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Tests\TestCase;

class CIMGetProfileResponseTest extends TestCase
Expand All @@ -15,13 +14,22 @@ public function testConstructEmpty()
new CIMGetProfileResponse($this->getMockRequest(), '');
}

public function testGetMatchingPaymentProfileId()
public function testGetMultipleMatchingPaymentProfileId()
{
$httpResponse = $this->getMockHttpResponse('CIMGetProfileSuccess.txt');
$httpResponse = $this->getMockHttpResponse('CIMGetMultipleProfilesSuccess.txt');
$response = new CIMGetProfileResponse($this->getMockRequest(), $httpResponse->getBody());

$this->assertEquals('26455656', $response->getMatchingPaymentProfileId('1111'));
$this->assertEquals('26455709', $response->getMatchingPaymentProfileId('8888'));
$this->assertNull($response->getMatchingPaymentProfileId('8889'));
}

public function testGetSingleMatchingPaymentProfileId()
{
$httpResponse = $this->getMockHttpResponse('CIMGetSingleProfileSuccess.txt');
$response = new CIMGetProfileResponse($this->getMockRequest(), $httpResponse->getBody());

$this->assertEquals('26455656', $response->getMatchingPaymentProfileId('1111'));
$this->assertNull($response->getMatchingPaymentProfileId('8889'));
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions tests/Mock/CIMGetSingleProfileSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 746
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Access-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
Date: Thu, 18 Sep 2014 03:59:27 GMT

<?xml version="1.0" encoding="utf-8"?><getCustomerProfileResponse><messages><resultCode>Ok</resultCode><message><code>I00001</code><text>Successful.</text></message></messages><profile><email>[email protected]</email><customerProfileId>28775801</customerProfileId><paymentProfiles><billTo><firstName/><lastName/><company/><address/><city/><state/><zip>12345</zip><country/></billTo><customerPaymentProfileId>26455656</customerPaymentProfileId><payment><creditCard><cardNumber>XXXX1111</cardNumber><expirationDate>XXXX</expirationDate></creditCard></payment></paymentProfiles></profile></getCustomerProfileResponse>

0 comments on commit 1c2c341

Please sign in to comment.