当我发布输入错误(例如错误的cvv)的事务时,将引发实际的C#异常,并且响应为null。因此,我显然无法访问“错误”列表。为什么有更好的方法来捕获错误然后解析Message值?
try
{
sqResponse = transactionsApi.Charge(tokenInfo.LocationID, chargeRequest);
}
catch (SquareUp.Client.ApiException e)
{
if (e.Message.Contains("VERIFY_CVV_FAILURE")) { error = "CVV is incorrect."; }
else if (e.Message.Contains("VERIFY_AVS_FAILURE")) { error = "Postal Code is incorrect."; }
else if (e.Message.Contains("INVALID_EXPIRATION")) { error = "Expiration date is incorrect."; }
else if (e.Message.Contains("CARD_DECLINED")) { error = "Credit card has been declined."; }
else if (e.Message.Contains("INVALID_CARD")) { error = "Credit card number is not valid"; }
else
{
error = e.ErrorCode.ToString();
}
return false;
}