Sam
Sam

Reputation: 15770

ASP.NET MVC Validation

So I have my View Model validation down using Fluent Validation and my Service Layer validation down as far as field validation goes, but my question is how do you deal with errors like, "Your credit card was declined", errors that have to do with logic other then input validation?

Upvotes: 0

Views: 195

Answers (1)

eulerfx
eulerfx

Reputation: 37739

A credit card being declined is not a validation error, instead it is a result of a payment processing attempt. For example, suppose you have a payment gateway service which returns a PaymentProcessingResult class, which can either have a status of approved or declined, with a possible reason for the decline:

class PaymentProcessingResult {
    bool IsApproved { get; set; }
    string DeclineReason { get; set; }
}

interface IPaymentGateway {
  PaymentProcessingResult ProcessPayment(PaymentInfo p);
}

Then in the controller which invokes this payment gateway service you can provide a proper response:

class CheckoutController : Controller {

  IPaymentGayeway paymentGateway;

  [HttpPost]
  public ActionResult ProcessPayment() {
     var paymentInfo = /* get payment info, from a shopping cart or the like */;

     var paymentResponse = this.paymentGateway.ProcessPayment(paymentInfo);

     if (paymentResponse.IsApproved) return View("PaymentReceipt");
     else return View("PaymentDeclined", paymentRespone);

  }
}

For security reasons, be sure not to expose too much information about the reason for a decline. A payment being declined can be conveyed in many ways. Instead of displaying a payment decline view, you can redirect back to a page where payment information is entered and highlight and display a message there, perhaps using the TempData dictionary to store the message for the next request.

Upvotes: 2

Related Questions