Exemple #1
0
        public ActionResult Authorize(PaymentCard card)
        {
            try
            {
                //Validate whether the entered captcha is valid.
                //If Captcha is invalid it adds the error to model state, so nothgn needed  the code to show the error.
                //Validation control takes care if showing the message.
                var validcaptcha = this.IsCaptchaValid("Please enter the text as shown in the image.");

                //If the card expiry year is the current year make sure the month is later than or equal to current month
                if ((card.CCExpYear == DateTime.Now.Year && card.CCExpMonth < DateTime.Now.Month) || card.CCExpYear < DateTime.Now.Year)
                {
                    this.ModelState.AddModelError(nameof(card.CCExpMonth), "Expiry month is invalid.");
                    this.ModelState.AddModelError(nameof(card.CCExpYear), "Expiry year is invalid.");
                }
                if (this.ModelState.IsValid)
                {
                    //Make call to payment gateway to authorize the card, it returns id of the profile create on payent gateway.
                    var authResult = PaymentGateway?.Authorize(card);

                    //if payment card is valid render the form/view the collect the organizatiion inforation.
                    if (authResult.IsAuthorized)
                    {
                        return(View("CreateOrg", new OrgInfo()
                        {
                            CustomerPaymentProfileId = authResult.PaymentProfileId,
                            CustomerProfileId = authResult.ProfileId,
                            EmailAddress = card.EmailAddress,
                            Address = new Address()
                        }));
                    }
                    else if (authResult.IsError)
                    {
                        ModelState.AddModelError(nameof(card.CCnumber), authResult.Error);
                    }
                }

                //Will reach here if there are any errors or card is not authorized
                return(View("CreatePaymentProfile", card));
            }
            catch (Exception ex)
            {
                WriteError(ex);
                return(View("Error"));
            }
        }