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 ||
            ($this->getOpaqueDataDescriptor() && $this->getOpaqueDataValue())
        ) {
            // force card update flag turned off. No need to further process.
            // also if opaque data is being used we are not able to update existing payment profiles
            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;
    }
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 ||
            ($this->getOpaqueDataDescriptor() && $this->getOpaqueDataValue())
        ) {
            // force card update flag turned off. No need to further process.
            // also if opaque data is being used we are not able to update existing payment profiles
            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;
    }
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 || ( $ this -> getOpaqueDataDescriptor ( ) && $ this -> getOpaqueDataValue ( ) ) ) { // force card update flag turned off. No need to further process. // also if opaque data is being used we are not able to update existing payment profiles 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 ; }
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
Attempts to add a payment profile to the existing customer profile and return the updated customer profile