// GET: Checkout
        public ActionResult Index(string message)
        {
            ActionResult actionResult;
            if (EnsureLoggedOnUser(out actionResult)) return actionResult;

            using (var repository = RepositoryFactory.GetInstance(Session))
            {
                var userPurchases = FindPurchasesForUser(repository).ToArray();
                var checkoutViewModel = GetCheckoutViewModel(userPurchases, repository);
                checkoutViewModel.ErrorMessage = message;

                string payerId;
                string paymentId;

                var ppsuccess = HttpContext.Request["ppsuccess"] != null && HttpContext.Request["ppsuccess"].ToLower() == "true";
                var ppcancel = HttpContext.Request["ppcancel"] != null && HttpContext.Request["ppcancel"].ToLower() == "true"; ;

                var currencyRetriver = new CurrencyRetriver(HttpContext, Session, repository);
                var sessionState = new SessionState();
                var expressCheckoutManager = new ExpressCheckoutManager();

                var checkoutLogic = new CheckoutLogic(repository, currencyRetriver, sessionState, expressCheckoutManager);

                try
                {
                    if (ppsuccess && PaymentParamsExist(out payerId, out paymentId))
                    {
                        checkoutLogic.ExecutePayment(payerId, paymentId, checkoutViewModel, Session.GetLoggedInUser());
                    }
                    else if (ppcancel)
                    {
                        checkoutLogic.CancelPayment(checkoutViewModel);
                    }
                }
                catch (Exception ex)
                {
                    BL.DomainServices.Log.LoggerFactory.GetLogger().LogError(ex);
                    checkoutViewModel.ErrorMessage = Strings.GetLocalizedString(Strings.ShoppingCartPaymentFailure);
                }

                return View(checkoutViewModel);
            }
        }
예제 #2
0
        private async Task StartPaymentProcess(double amountForPayment, string currentUrl, CreateCouponViewModel createCuponVm, ParseRepository repository)
        {
            var currencyRetriever = new CurrencyRetriver(HttpContext, Session, repository);
            var paymentOk = true;
            try
            {
                var itemNameLabel = string.IsNullOrEmpty(createCuponVm.ContentItemDetails.BundleId)
                    ? MyMentorResources.paypalCuponFor
                    : MyMentorResources.paypalCuponForBundle;
                var itemName = string.Concat(itemNameLabel, createCuponVm.ContentItemDetails.NamePart1, " ", createCuponVm.ContentItemDetails.NamePart2);
                var ppManager = new ExpressCheckoutManager(currentUrl);
               
                var paypalCurrencyCode = createCuponVm.TeacherData.Currency.PaypalCode;
                amountForPayment = CurrencyConverter.Convert(amountForPayment,currencyRetriever.GetCurrent(),createCuponVm.TeacherData.Currency);
                var validationResponse = ppManager.CouponSpecialDiscountPaymentValidation(itemName, amountForPayment, paypalCurrencyCode);
                
                createCuponVm.PaymentApprovalUrl = validationResponse.ValidationUrl;
                createCuponVm.PaymentId = validationResponse.PaymentId;

                //update paymentid in the event record
                createCuponVm.EventId =
                    await
                        CreateUpdateEvent(createCuponVm, EventStatus.EventStarted, repository);
            }
            catch (Exception ex)
            {
                paymentOk = false;
                mLogger.Log(LogLevel.Error, ex);
            }

            if (!paymentOk)
            {
                await DeleteCoupon(createCuponVm, repository);
                await CreateUpdateEvent(createCuponVm, EventStatus.EventErrorResolved, repository);
            }
        }
        public ActionResult CompleteCheckout(CheckoutViewModel model)
        {
            ActionResult actionResult;
            if (EnsureLoggedOnUser(out actionResult)) return actionResult;
            var checkoutViewModel = new CheckoutViewModel();
            var loggedInUser = Session.GetLoggedInUser();

            try
            {
                using (var repository = RepositoryFactory.GetInstance(Session))
                {
                    var userPurchases = FindPurchasesForUser(repository).ToArray();
                    var userHistoricalPurchases = repository.FindHistoricalUserPurchases(Session.GetLoggedInUser().ObjectId).ToArray();
                    var worldId = new WorldContentTypeRetriver(HttpContext, repository).GetWorldContentTypeId();
                    string paymentUrl;

                    checkoutViewModel = GetCheckoutViewModel(userPurchases, repository);
                    checkoutViewModel.PurchaseFor = model.PurchaseFor;
                    SetBundleClips(userPurchases, checkoutViewModel, repository);

                    var currencyRetriver = new CurrencyRetriver(HttpContext, Session, repository);
                    var sessionState = new SessionState();
                    var indexUrl = HttpContext.Request.Url.ToString().ToLower().Replace("completecheckout", "index") + string.Format("/?purchaseFor={0}", model.PurchaseFor);
                    var expressCheckoutManager = new ExpressCheckoutManager(indexUrl);
                    var logic = new CheckoutLogic(repository, currencyRetriver, sessionState, expressCheckoutManager);

                    logic.ExecuteCompleteCheckout(loggedInUser, checkoutViewModel, worldId, userHistoricalPurchases, out paymentUrl);
                    if (!string.IsNullOrEmpty(paymentUrl))
                    {
                        return Redirect(paymentUrl);
                    }
                }
            }
            catch
            {
                checkoutViewModel.ErrorMessage = Strings.GetLocalizedString(Strings.ShoppingCartPaymentFailure);
            }
            return View("Index", checkoutViewModel);
        }
예제 #4
0
        public async Task<ActionResult> PaymentValidationSuccess(string paymentId, string token, string payerId)
        {

            using (var repository = RepositoryFactory.GetInstance(Session))
            {
                var paymentSuccess = true;

                // check if session is still active
                var createCouponVm = Session.GetCouponData();
                if (createCouponVm == null)
                {
                    createCouponVm = new CreateCouponViewModel
                    {
                        CouponErrors = {GeneralError = MyMentorResources.createCouponGeneralError}
                    };
                    return Json(createCouponVm, JsonRequestBehavior.AllowGet);
                }

                if (Session.PaymentSubmitted().HasValue)
                {
                    return Json(createCouponVm, JsonRequestBehavior.AllowGet);
                }

                // pay with paypal
                try
                {
                    // only after payment Id is updated, proceed to payment
                    var ppManager = new ExpressCheckoutManager();
                    var paymnet = ppManager.ExecutePayment(payerId, paymentId);
                    Session.SetPaymentSubmitted();
                    createCouponVm.PaymentData = paymnet.ConvertToJson();
                    await CreateUpdateEvent(createCouponVm, EventStatus.EventStarted, repository);
                }
                catch (Exception ex)
                {
                    paymentSuccess = false;

                    if (ex is PaymentsException)
                    {
                        var ppException = ex as PaymentsException;                        
                        SendErrorEmail(createCouponVm, MyMentorResources.couponErrPaymentFailedDbError);                        
                        mLogger.Log(LogLevel.Error, ppException.Details.message);
                    }
                    else
                    {
                        //SendErrorEmail(createCouponVm, ex.Message);
                        mLogger.Log(LogLevel.Error, ex);
                    }
                    createCouponVm.CouponErrors.GeneralError = MyMentorResources.createCouponGeneralError;
                }

                // write payment transaction to parse  db
                try
                {
                    if (paymentSuccess)
                    {
                        await ExecutePaymentTransaction(repository, createCouponVm);
                    }
                    else
                    {
                        createCouponVm.PaymentId = string.Empty;
                        await CreateUpdateEvent(createCouponVm, EventStatus.PaymentNotReceived, repository);
                        await DeleteCoupon(createCouponVm, repository);
                    }
                }
                catch (Exception e)
                {
                    SendErrorEmail(createCouponVm, MyMentorResources.couponErrPaymentSuccessDbError);                    
                    mLogger.Log(LogLevel.Error, e);
                }

                return Json(createCouponVm, JsonRequestBehavior.AllowGet);
            }
        }