AuthorizeNet / sdk-dotnet

.Net SDK for Authorize.Net API
Other
149 stars 206 forks source link

GetApiResponse returns null when lineItemType name is invalid #224

Closed twooton61 closed 6 years ago

twooton61 commented 6 years ago

A lot of the example code for this API shows checking for response.messages.resultCode to check for errors immediately after calling controller.GetApiResponse(). But with validation errors, the response is null. It was only after digging around in the API I found GetResultCode() and the actual error validation error in GetResults(). I wouldn't expect GetApiResponse() to return null on a validation error, but rather a response with response.messages.resultCode set to messageTypeEnum.Error and the relevant error messages in response.messages

chsriniv9 commented 6 years ago

@twooton61

Below steps show how to get the errorresponse when response is null.

Step 1: Instantiate the controller that will call the service

       var controller = new createCustomerProfileController(request);
       controller.Execute();

Step 2: Get the response from the service (errors contained if any)

        createCustomerProfileResponse response = controller.GetApiResponse();

Step 3: Validate response

        if (response != null)
        {
            // Your code goes here
        }

Step 4: If Response is null, then in else part, call the GetErrorResponse() method as shown below to get the error messages/response.

       else
       {
           // Check for errors
           ANetApiResponse errorResponse = controller.GetErrorResponse();
           if (errorResponse.messages.message.Length > 0)
           {
               Console.WriteLine("Customer Profile Creation Failed.");
               Console.WriteLine("Error Code: " + errorResponse.messages.message[0].code);
               Console.WriteLine("Error message: " + errorResponse.messages.message[0].text);
           }
           else
           {
               Console.WriteLine("Null Response.");
           }
       }
chsriniv9 commented 6 years ago

Closing this based on the steps provided previously. Please feel free to re-open if further support needed on this issue.