Skip to content

Commit

Permalink
CS-3726 handle XML to JSON array conversion issue when a single payme…
Browse files Browse the repository at this point in the history
…nt profile is present (#20)
  • Loading branch information
anush authored and rushi committed Apr 26, 2019
1 parent 431d274 commit 6b39872
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/Message/CIMGetProfileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ 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.
// TODO: In some situations payment attribute is not there. We need to investigate why. See #php7
if (isset($paymentProfile['payment']['creditCard']['cardNumber'])) {
$cardLast4 = substr($paymentProfile['payment']['creditCard']['cardNumber'], -4);
if ($last4 == $cardLast4) {
return (string)$paymentProfile['customerPaymentProfileId'];
}
$cardLast4 = substr($paymentProfile['payment']['creditCard']['cardNumber'], -4);
if ($last4 == $cardLast4) {
return (string)$paymentProfile['customerPaymentProfileId'];
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/CIMGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testCreateCardSuccess()
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 @@ -95,7 +95,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 @@ -110,7 +110,7 @@ public function testShouldUpdateExistingPaymentProfileIfDuplicateExistsAndForceC
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
13 changes: 11 additions & 2 deletions tests/Message/CIMGetProfileResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,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.
14 changes: 14 additions & 0 deletions tests/Mock/CIMGetSingleProfileSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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 6b39872

Please sign in to comment.