From 6b39872a42be3d0d191f07091e2fd732b2115cd7 Mon Sep 17 00:00:00 2001 From: Anush Ramani Date: Thu, 25 Apr 2019 21:31:51 -0700 Subject: [PATCH] CS-3726 handle XML to JSON array conversion issue when a single payment profile is present (#20) --- src/Message/CIMGetProfileResponse.php | 14 ++++++++------ tests/CIMGatewayTest.php | 6 +++--- tests/Message/CIMGetProfileResponseTest.php | 13 +++++++++++-- ...ccess.txt => CIMGetMultipleProfilesSuccess.txt} | 0 tests/Mock/CIMGetSingleProfileSuccess.txt | 14 ++++++++++++++ 5 files changed, 36 insertions(+), 11 deletions(-) rename tests/Mock/{CIMGetProfileSuccess.txt => CIMGetMultipleProfilesSuccess.txt} (100%) create mode 100644 tests/Mock/CIMGetSingleProfileSuccess.txt diff --git a/src/Message/CIMGetProfileResponse.php b/src/Message/CIMGetProfileResponse.php index 3753a24d..ff644fc0 100644 --- a/src/Message/CIMGetProfileResponse.php +++ b/src/Message/CIMGetProfileResponse.php @@ -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']; } } diff --git a/tests/CIMGatewayTest.php b/tests/CIMGatewayTest.php index 8c785f25..9e03603d 100644 --- a/tests/CIMGatewayTest.php +++ b/tests/CIMGatewayTest.php @@ -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(); @@ -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(); @@ -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(); diff --git a/tests/Message/CIMGetProfileResponseTest.php b/tests/Message/CIMGetProfileResponseTest.php index c1abda05..912c7d0b 100644 --- a/tests/Message/CIMGetProfileResponseTest.php +++ b/tests/Message/CIMGetProfileResponseTest.php @@ -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')); + } } diff --git a/tests/Mock/CIMGetProfileSuccess.txt b/tests/Mock/CIMGetMultipleProfilesSuccess.txt similarity index 100% rename from tests/Mock/CIMGetProfileSuccess.txt rename to tests/Mock/CIMGetMultipleProfilesSuccess.txt diff --git a/tests/Mock/CIMGetSingleProfileSuccess.txt b/tests/Mock/CIMGetSingleProfileSuccess.txt new file mode 100644 index 00000000..27d4120d --- /dev/null +++ b/tests/Mock/CIMGetSingleProfileSuccess.txt @@ -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 + +OkI00001Successful.kaylee@serenity.com28775801
1234526455656XXXX1111XXXX