Send methods within a httpost Instance error

0

I am doing a payment service and I want that when a card is validated, it calls another service that registers data in a table.

This is the controller

public class PagoController : ApiController
    {
        ValidarPagoNegocios negocios = new ValidarPagoNegocios();
        [HttpPost]
        public ValidarPagoResponse ValidarPago(ValidarPagoRequest request) {
            ValidarPagoResponse response = new ValidarPagoResponse();
            string mensaje = "";
            response.TransaccionCompleta 
                =  negocios.ValidarPago(out mensaje,
                                        request.TipoTarjeta,request.NumeroTarjeta,
                                        request.TitularTarjeta, request.MontoConsumir,
                                        request.MesExpiracionTarjeta,request.AñoExpiracionTarjeta,
                                        request.CodigoSeguridadTarjeta);
            response.TransaccionMensaje = mensaje;
             return response;
        }

This is the response of the postman when the card is validated

"TransaccionCompletada" : true 
"TransaccionMensaje" : " "

I tried to do it this way but I get an error message: Object reference not established as an instance of an object pointing to the whole method

TutoriaNegocios negocios2 = new TutoriaNegocios();

        ValidarPagoNegocios negocios = new ValidarPagoNegocios();
        [HttpPost]
        public ValidarPagoResponse ValidarPago(ValidarPagoRequest request)
        {
            ValidarPagoResponse response = new ValidarPagoResponse();
            string mensaje = negocios2.matriculaTutoria(request.matricula);
            response.TransaccionCompleta
                = negocios.ValidarPago(out mensaje,
                                        request.TipoTarjeta, request.NumeroTarjeta,
                                        request.TitularTarjeta, request.MontoConsumir,
                                        request.MesExpiracionTarjeta, request.AñoExpiracionTarjeta,
                                        request.CodigoSeguridadTarjeta,request.matricula.idAlumno,request.matricula.idTutoria);
            response.TransaccionMensaje = mensaje;
            return response;
        }

the classes that it takes of parameters are these:

public class ValidarPagoResponse
    {
        public bool TransaccionCompleta { get; set; }
        public string TransaccionMensaje { get; set; }
    }



 public class ValidarPagoRequest
    {

        public string NumeroTarjeta { get; set; }
        public int TipoTarjeta { get; set; }
        public string CodigoSeguridadTarjeta { get; set; }
        public string TitularTarjeta { get; set; }
        public string MesExpiracionTarjeta { get; set; }
        public string AñoExpiracionTarjeta { get; set; }
        public double MontoConsumir { get; set; }
        public MatriculaViewModel matricula { get; set; }
    }
    
asked by Marcelo Fabian 06.12.2018 в 23:46
source

1 answer

0

At first sight what I suspect is the property matricula of ValidarPagoRequest is unassigned, so when you access your properties you have the fault

To solve the problem you could instantiate the property in the constructor

public class ValidarPagoRequest
{
    public ValidarPagoRequest()
    {
        this.matricula = new MatriculaViewModel();
    }

    public string NumeroTarjeta { get; set; }
    public int TipoTarjeta { get; set; }
    public string CodigoSeguridadTarjeta { get; set; }
    public string TitularTarjeta { get; set; }
    public string MesExpiracionTarjeta { get; set; }
    public string AñoExpiracionTarjeta { get; set; }
    public double MontoConsumir { get; set; }
    public MatriculaViewModel matricula { get; set; }
}

But in reality what you should validate is because the client that invokes the service is not sending the data correctly from the registration

It could also be validated with something like being

[HttpPost]
public ValidarPagoResponse ValidarPago(ValidarPagoRequest request)
{       
    if(request.matricula == null)
        throw new Exception("debe asignar los datos de la matricula");

    //resto codigo
}
    
answered by 07.12.2018 в 00:09