public async Task <ActionResult> Donate(decimal Units)
        {
            if (unitsService.Get(Units) != null)
            {
                //get the subscriber
                var account = await _userManager.FindByNameAsync(User.Identity.Name);

                if (account != null)
                {
                    //log the transaction
                    CashDonationAttempt attempt = new CashDonationAttempt()
                    {
                        Email       = account.Email,
                        PhoneNumber = account.PhoneNumber,
                        Amount      = Units,
                        CreatedBy   = User.Identity.Name,
                        DateCreated = DateTime.Now,
                        Reference   = Guid.NewGuid().ToString()
                    };

                    Session.Add("trxref", attempt.Reference);

                    //Save the transaction
                    attempt = cashDonationAttemptService.Insert(attempt);

                    //create transaction
                    var transaction = new TransactionInitialize()
                    {
                        Amount      = Units,
                        Email       = account.Email,
                        Reference   = attempt.Reference,
                        CallBackUrl = String.Format("{0}/{1}/{2}", Request.Url.GetLeftPart(UriPartial.Authority), "Donate", "Verify")
                    };

                    //decrypt secret
                    var paymentConfiguration = payStackConfiguration.GetDefault();
                    var secret = paymentConfiguration.Secret;//StringEncrypterDecrypter.StringCipher.Decrypt(paymentConfiguration.Secret, String.Format("{0}{1}{2}{3}{4}{5}{6}", User.Identity.Name, paymentConfiguration.DateCreated.Year, paymentConfiguration.DateCreated.Month, paymentConfiguration.DateCreated.Day, paymentConfiguration.DateCreated.Hour, paymentConfiguration.DateCreated.Minute, paymentConfiguration.DateCreated.Second, paymentConfiguration.DateCreated.Millisecond));

                    //request charge authorization
                    var canCharge = await paystackService.Initialize(secret, transaction);

                    if (canCharge == null)
                    {
                        return(RedirectToAction("Donate"));
                    }
                    if (canCharge.Successful)
                    {
                        //redirect the user to the charge page
                        return(Redirect(canCharge.Data.AuthorizationUrl));
                    }
                }
                else
                {
                    ViewBag.Message = "Only Registered users are allowed to top up";
                    return(View("AccessDenied"));
                }
            }
            return(View());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Purchase(decimal Units)
        {
            if (unitsService.Get(Units) != null)
            {
                //get the subscriber
                var account = subscriberService.FindByUserName(User.Identity.Name);


                if (account != null)
                {
                    //log the transaction
                    TopUpAttempt attempt = new TopUpAttempt()
                    {
                        Email        = emailAddressService.List(account.Id).FirstOrDefault(x => x.Ok).Text,
                        PhoneNumber  = phoneNumberService.List(account.Id).FirstOrDefault(x => x.Ok).Digits,
                        Amount       = Units,
                        CreatedBy    = User.Identity.Name,
                        DateCreated  = DateTime.Now,
                        Reference    = Guid.NewGuid().ToString(),
                        SubscriberId = account.Id,
                    };

                    Session.Add("trxref", attempt.Reference);

                    //Save the transaction
                    attempt = topUpAttemptService.Insert(attempt);



                    //create transaction
                    var transaction = new TransactionInitialize()
                    {
                        Amount      = Units,
                        Email       = emailAddressService.List(account.Id).FirstOrDefault(x => x.Ok).Text,
                        Reference   = attempt.Reference,
                        CallBackUrl = String.Format("{0}/{1}/{2}", Request.Url.GetLeftPart(UriPartial.Authority), "TopUp", "Verify")
                    };

                    //request charge authorization
                    var canCharge = await paystackService.Initialize(payStackConfiguration.GetDefault().Secret, transaction);

                    if (canCharge == null)
                    {
                        return(RedirectToAction("Purchase"));
                    }
                    if (canCharge.Successful)
                    {
                        //redirect the user to the charge page
                        return(Redirect(canCharge.Data.AuthorizationUrl));
                    }
                    //var page = paystfackService.GetPaymentPages().FirstOrDefault(x => x.Amount == Units);
                    //if (page != null)
                    //{
                    //    return Redirect(page.Url);
                    //}
                }
                else
                {
                    ViewBag.Message = "Only subscribers are allowed to top up";
                    return(View("AccessDenied"));
                }
            }
            return(View());
        }