Result Objectsanchor

Result objects are wrapper objects that indicate whether or not the API call was a success and, if successful, include the requested data.

note

API calls that don't have validations, such as searches, will return a collection of requested objects instead of a result object.

Success resultsanchor

If the API call was successful, the isSuccess() on the result will return true.

  1. Java
result.isSuccess();
// true

Transaction transaction = result.getTarget();

Generics are used to preserve static typing of the resource objects. The target will be an instance of the requested resource class.

  1. Java
Result<Customer> customerResult = gateway.customer().create(new CustomerRequest());
Customer customer = customerResult.getTarget();

Result<Transaction> transactionResult = gateway.transaction().sale(
  new TransactionRequest()
);
Transaction transaction = transactionResult.getTarget();

Error resultsanchor

If the API call was not successful, the success on the result will return false. An error may be due to:

  1. Java
result.isSuccess();
// false

ValidationErrors errors = result.getErrors();

The getErrors() result object will only be populated if the error is due to a failed validation. In this case, the object will contain one or more validation errors indicating which parameters were invalid:

  1. Java
CustomerRequest request = new CustomerRequest()
  .email("invalid_email")
  .creditCard()
    .number("not_numeric")
    .done();

for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
  System.out.println(error.getAttribute());
  System.out.println(error.getCode());
  System.out.println(error.getMessage());
}

List<ValidationError> customerErrors = result.getErrors().forObject("customer").getAllValidationErrors();
for (ValidationError error : customerErrors) {
  System.out.println(error.getAttribute());
  System.out.println(error.getCode());
  System.out.println(error.getMessage());
}

List<ValidationError> creditCardErrors = result.getErrors().forObject("customer").forObject("creditCard").getAllValidationErrors();
for (ValidationError error : creditCardErrors) {
  System.out.println(error.getAttribute());
  System.out.println(error.getCode());
  System.out.println(error.getMessage());
}

For details on transaction error results, see the transaction response object .

Messageanchor

The message on the error result gives a human-readable description of what went wrong, regardless of the cause and nature of the error.

  1. Java
System.out.println(result.getMessage());
// "Amount is required.
Credit card number is invalid."

The message can contain multiple error messages.

note

This was added in version {{sdkVersionForDate "Jul09_2010"}}

Paramsanchor

Error results include the parameters that were submitted. This can be useful during Transparent Redirects to repopulate your form if validations fail.

  1. Java
System.out.println(result.getParameters());
// {transaction[amount]=1000.00, transaction[type]=sale, transaction[credit_card][expiration_date]=05/2012}

For PCI compliance reasons, credit card number and cvv parameters are not included.

See also