Result Objects
Most API calls will return a result object. The only exceptions to that are API calls that don’t have validations, like searches. Result objects will indicate whether the API call was a success or not.
Success Results
If the API call was successful, the success method on the result object will return true.
bool success = result.IsSuccess();
Transaction transaction = result.Target;
Generics are used to preserve static typing of the resource objects. The target will be an instance of the requested resource class.
Result<Customer> customerResult = gateway.Customer.Create(new CustomerRequest());
Customer customer = customerResult.Target;
Result<Transaction> transactionResult = gateway.Transaction.Sale(
new TransactionRequest()
);
Transaction transaction = transactionResult.Target;
Error Results
If the API call was not successful, you will get an error result. In most cases, there will be validation errors for the validations that failed. However, error results are also returned when valid data is submitted, but errors occurred in processing it. For example, if all the parameters to create a transaction are valid, but the processor declines the transaction, you will get an error result.
Message
If you want one single message that tells you what went wrong, regardless of whether it’s validation errors, a declined transaction, failed card verification, etc., you can check the message on the error result. The message can contain multiple error messages.
`result.Message` /* e.g. "Amount is required.\nCredit card number is invalid." */
Note: This was added in version 2.5.0
Params
Error results include the parameters that were submitted. This is primarily useful when using the Transparent Redirect API to repopulate your form if validations fail. For PCI compliance reasons, credit card number and cvv parameters are not included in the params.
foreach (var param in result.Parameters) {
Console.WriteLine(param.Key + "=" + param.Value);
}
// transaction[type]=sale
// transaction[amount]=1000
// transaction[credit_card][expiration_date]=05/2012