public function testCreateCardFailure()
 {
     $httpResponse = $this->getMockHttpResponse('CIMCreateCardFailure.txt');
     $response = new CIMCreateCardResponse($this->getMockRequest(), $httpResponse->getBody());
     $this->assertFalse($response->isSuccessful());
     $this->assertEquals('E00041', $response->getReasonCode());
     $this->assertEquals("3", $response->getResultCode());
     $this->assertEquals("One or more fields in the profile must contain a value.", $response->getMessage());
     $this->assertNull($response->getCustomerProfileId());
     $this->assertNull($response->getCustomerPaymentProfileId());
     $this->assertNull($response->getCardReference());
 }
 /**
  * Attempts to add a payment profile to the existing customer profile and return the updated customer profile
  *
  * @param CIMCreateCardResponse $createCardResponse Duplicate customer profile response
  *
  * @return CIMCreateCardResponse
  */
 public function createPaymentProfile(CIMCreateCardResponse $createCardResponse)
 {
     // Parse the customer profile Id from the message
     $msg = $createCardResponse->getMessage();
     preg_match("/ID (.+) already/i", $msg, $matches);
     if (empty($matches[1])) {
         // Duplicate profile id not found. Return current response
         return $createCardResponse;
     }
     // Use the customerProfileId and create a payment profile for the customer
     $parameters = array_replace($this->getParameters(), array('customerProfileId' => $matches[1]));
     $createPaymentProfileResponse = $this->makeCreatePaymentProfileRequest($parameters);
     if ($createPaymentProfileResponse->isSuccessful()) {
         $parameters['customerPaymentProfileId'] = $createPaymentProfileResponse->getCustomerPaymentProfileId();
     } elseif ($this->getForceCardUpdate() !== true) {
         // force card update flag turned off. No need to further process.
         return $createCardResponse;
     }
     $getProfileResponse = $this->makeGetProfileRequest($parameters);
     if (!$createPaymentProfileResponse->isSuccessful() && $createPaymentProfileResponse->getReasonCode() == 'E00039') {
         // Found a duplicate payment profile existing for the same card data. Force update is turned on,
         // So find matching payment profile id from the customer profile and update it.
         $card = $this->getCard();
         $last4 = substr($card->getNumber(), -4);
         $customerPaymentProfileId = $getProfileResponse->getMatchingPaymentProfileId($last4);
         if (!$customerPaymentProfileId) {
             // Failed. Matching customer payment profile id not found. Return the original response
             return $createCardResponse;
         }
         $parameters['customerPaymentProfileId'] = $customerPaymentProfileId;
         $updatePaymentProfileResponse = $this->makeUpdatePaymentProfileRequest($parameters);
         if (!$updatePaymentProfileResponse->isSuccessful()) {
             // Could not update payment profile. Return the original response
             return $createCardResponse;
         }
     }
     // return the updated customer profile
     $getPaymentProfileResponse = $this->makeGetPaymentProfileRequest($parameters);
     if (!$getPaymentProfileResponse->isSuccessful()) {
         // Could not get the updated customer profile. Return the original response
         return $createCardResponse;
     }
     return $getPaymentProfileResponse;
 }
 /**
  * Attempts to add a payment profile to the existing customer profile and return the updated customer profile
  *
  * @param CIMCreateCardResponse $createCardResponse Duplicate customer profile response
  *
  * @return CIMCreateCardResponse
  */
 public function createPaymentProfile(CIMCreateCardResponse $createCardResponse)
 {
     // Parse the customer profile Id from the message
     $msg = $createCardResponse->getMessage();
     preg_match("/ID (.+) already/i", $msg, $matches);
     if (empty($matches[1])) {
         // Duplicate profile id not found. Return current response
         return $createCardResponse;
     }
     // Use the customerProfileId and create a payment profile for the customer
     $parameters = array_replace($this->getParameters(), array('customerProfileId' => $matches[1]));
     $createPaymentProfileResponse = $this->makeCreatePaymentProfileRequest($parameters);
     if ($createPaymentProfileResponse->isSuccessful()) {
         $parameters['customerPaymentProfileId'] = $createPaymentProfileResponse->getCustomerPaymentProfileId();
     } elseif ($this->getForceCardUpdate() !== true) {
         // force card update flag turned off. No need to further process.
         return $createCardResponse;
     }
     $getProfileResponse = $this->makeGetProfileRequest($parameters);
     // Check if there is a pre-existing profile for the given card numbers.
     // For these codes we should check for duplicate payment profiles
     $otherErrorCodes = array(CIMGetProfileResponse::ERROR_DUPLICATE_PROFILE, CIMGetProfileResponse::ERROR_MAX_PAYMENT_PROFILE_LIMIT_REACHED);
     if (!$createPaymentProfileResponse->isSuccessful() && in_array($createPaymentProfileResponse->getReasonCode(), $otherErrorCodes)) {
         // There is a possibility of a duplicate payment profile, so find matching payment profile id
         // from the customer profile and update it.
         $card = $this->getCard();
         $last4 = substr($card->getNumber(), -4);
         $customerPaymentProfileId = $getProfileResponse->getMatchingPaymentProfileId($last4);
         if (!$customerPaymentProfileId) {
             // Failed. Matching customer payment profile id not found. Return the original response
             return $createCardResponse;
         }
         $parameters['customerPaymentProfileId'] = $customerPaymentProfileId;
         $updatePaymentProfileResponse = $this->makeUpdatePaymentProfileRequest($parameters);
         if (!$updatePaymentProfileResponse->isSuccessful()) {
             // Could not update payment profile. Return the original response
             return $createCardResponse;
         }
     }
     // return the updated customer profile
     $getPaymentProfileResponse = $this->makeGetPaymentProfileRequest($parameters);
     if (!$getPaymentProfileResponse->isSuccessful()) {
         // Could not get the updated customer profile. Return the original response
         return $createCardResponse;
     }
     return $getPaymentProfileResponse;
 }