PaypalAutoPaymentModel PreParePaypalAutoPaymentModelForList(PaypalAutoPayment paypalAutoPayment) { PaypalAutoPaymentModel model = new PaypalAutoPaymentModel(); model.Id = paypalAutoPayment.Id; model.GrossAmount = paypalAutoPayment.GrossAmount; model.BillingAggrementID = paypalAutoPayment.BillingAggrementID; model.ReferenceID = paypalAutoPayment.ReferenceID; model.TransactionDate = paypalAutoPayment.TransactionDate; model.TrasactionID = paypalAutoPayment.TrasactionID; model.UserID = paypalAutoPayment.UserID; model.CustomerName = paypalAutoPayment.AspNetUser.Email; model.UserPackageID = paypalAutoPayment.UserPackageID; model.UserPackageName = paypalAutoPayment.UserPackage.Package.Package_Name; model.ServiceDate = paypalAutoPayment.ServiceDate; model.PaymentStatus = paypalAutoPayment.PaymentStatus; model.PendingReason = paypalAutoPayment.PendingReason; model.CreatedOn = paypalAutoPayment.CreatedOn; return(model); }
public ActionResult ProcessPayment(UserPackageAddOnPaymentInformation model) { if (model.UserPackageID > 0) { return(RedirectToAction("ProcessPaypalPayment", "Paypal", new { userpackageid = model.UserPackageID })); } var userid = User.Identity.GetUserId(); if (ModelState.IsValid) { var userPackage = db.UserPackages.Where(a => a.Id == model.UserPackageID).FirstOrDefault(); var finalPrice = userPackage.TotalPrice + userPackage.TaxAmount + userPackage.TipAmount; IPaymentMethod _paymentMethod = new PayPalDirectPaymentProcessor(); ProcessPaymentRequest processPaymentRequest = new ProcessPaymentRequest(); processPaymentRequest.OrderTotal = finalPrice; processPaymentRequest.CreditCardName = model.NameOnCard; processPaymentRequest.CreditCardNumber = model.CardNumber; processPaymentRequest.CreditCardCvv2 = model.CardSecurityCode; processPaymentRequest.CreditCardExpireMonth = model.CardExpiryMonth; processPaymentRequest.CreditCardExpireYear = model.CardExpiryYear; processPaymentRequest.CustomerId = User.Identity.GetUserId();; if (processPaymentRequest.OrderGuid == Guid.Empty) { processPaymentRequest.OrderGuid = Guid.NewGuid(); } try { ProcessPaymentResult processPaymentResult = null; processPaymentResult = _paymentMethod.ProcessPayment(processPaymentRequest); if (processPaymentResult.Success) { PayPalLog log = new PayPalLog() { ACK = "SUCCESS", ApiSatus = string.Empty, BillingAggrementID = processPaymentResult.CaptureTransactionId, CorrelationID = string.Empty, ECToken = string.Empty, ResponseError = string.Empty, ResponseRedirectURL = string.Empty, ServerDate = DateTime.Now, TimeStamp = DateTime.Now.ToString(), UserId = userid, SubscriptionID = model.UserPackageID }; db.PayPalLogs.Add(log); db.SaveChanges(); //Save USer Logs UserTransaction _transaction = new UserTransaction() { Userid = userPackage.UserId, PaypalId = processPaymentResult.CaptureTransactionId, TransactionDate = DateTime.Now, Amount = finalPrice, PackageId = userPackage.Id, Details = "No Details", BillingAggrementID = processPaymentResult.CaptureTransactionId }; db.UserTransactions.Add(_transaction); db.SaveChanges(); //Logic to disable autorenew if Billingagreement is null //Logic to update Transaction status if (userPackage != null) { string message = string.Empty; string responseString = string.Empty; var package = db.Packages.FirstOrDefault(a => a.PackageId == userPackage.PackageId); PaypalAutoPayment paypalAutoPayment = new PaypalAutoPayment(); paypalAutoPayment.UserPackageID = userPackage.Id; paypalAutoPayment.UserID = userPackage.UserId; paypalAutoPayment.IsPaid = true; paypalAutoPayment.GrossAmount = String.Format("{0:0.00}", finalPrice); //Convert.ToString(finalPrice); paypalAutoPayment.PaymentStatus = "COMPLETED"; paypalAutoPayment.PaymentDate = DateTime.Now.ToString(); paypalAutoPayment.TrasactionID = processPaymentResult.CaptureTransactionId; paypalAutoPayment.BillingAggrementID = processPaymentResult.CaptureTransactionId; paypalAutoPayment.TransactionDate = DateTime.Now; DateTime currentDate = DateTime.Now; DateTime serviceDate = currentDate; if (userPackage.ServiceDay == currentDate.DayOfWeek.ToString()) { serviceDate = currentDate.AddDays(7); } else { DateTime nextServiceDate; for (int i = 1; i <= 6; i++) { if (i == 1) { nextServiceDate = currentDate.AddDays(i); if (userPackage.ServiceDay == nextServiceDate.DayOfWeek.ToString()) { if (userPackage.SubscriptionTypeId == 1) { serviceDate = nextServiceDate.AddDays(7); } else if (userPackage.SubscriptionTypeId == 2) { serviceDate = nextServiceDate.AddDays(14); } else if (userPackage.SubscriptionTypeId == 3) { serviceDate = nextServiceDate.AddDays(28); } else { serviceDate = nextServiceDate.AddDays(7); } break; } } else { nextServiceDate = currentDate.AddDays(i); if (userPackage.ServiceDay == nextServiceDate.DayOfWeek.ToString()) { serviceDate = currentDate.AddDays(i); break; } } } } paypalAutoPayment.ServiceDate = serviceDate; paypalAutoPayment.CreatedOn = DateTime.Now; db.PaypalAutoPayments.Add(paypalAutoPayment); db.SaveChanges(); if (paypalAutoPayment.IsPaid) { userPackage.PaymentRecieved = true; userPackage.IsActive = true; userPackage.NextServiceDate = serviceDate; userPackage.PaymentMethodName = "credit card"; db.Entry(userPackage).State = EntityState.Modified; db.SaveChanges(); var addOnsServices = userPackage.UserPackagesAddons.ToList(); foreach (var addOns in addOnsServices) { if (!addOns.NextServiceDate.HasValue) { addOns.NextServiceDate = serviceDate; db.SaveChanges(); } } } } Session["NewServiceCarTypeId"] = null; Session["SelectedCar"] = null; Session["NewServiceGarageId"] = null; var userInfo = userPackage.AspNetUser; // Send Notification Mail Admin for buy new subscrition. _workflowMessageService.SendNewSubscriptionNotificationToAdmin(userInfo.FirstName + " " + userInfo.LastName, userInfo.UserName, userPackage.Package.Package_Name, userPackage.SubscribedDate.ToString(), userPackage.CarUser.Garage.Garage_Name); return(RedirectToRoute("Completed", new { id = model.UserPackageID })); } foreach (var error in processPaymentResult.Errors) { model.Warnings.Add(error); } } catch (Exception exc) { model.Warnings.Add(exc.Message); } } model.UserPackge = db.UserPackages.Where(a => a.Id == model.UserPackageID).FirstOrDefault(); model.Addons = db.UserPackagesAddons.Where(a => a.UserPackageID == model.UserPackageID); //adding tax if (db.Taxes != null && db.Taxes.Count() > 0) { var taxPercentage = db.Taxes.Select(a => a.TaxPercentage).Sum(); model.UserPackge.TaxAmount = model.TaxAmount = model.UserPackge.TotalPrice * (taxPercentage / 100); model.TaxPercentage = taxPercentage; db.SaveChanges(); } //CC types model.CreditCardTypes.Add(new SelectListItem { Text = "Visa", Value = "Visa", }); model.CreditCardTypes.Add(new SelectListItem { Text = "Master card", Value = "MasterCard", }); model.CreditCardTypes.Add(new SelectListItem { Text = "Discover", Value = "Discover", }); model.CreditCardTypes.Add(new SelectListItem { Text = "Amex", Value = "Amex", }); //years model.ExpireYears.Add(new SelectListItem { Text = "Year", Value = "", }); for (int i = 0; i < 25; i++) { string year = Convert.ToString(DateTime.Now.Year + i); model.ExpireYears.Add(new SelectListItem { Text = year, Value = year, }); } //months //months model.ExpireMonths.Add(new SelectListItem { Text = "Month", Value = "", }); for (int i = 1; i <= 12; i++) { string text = (i < 10) ? "0" + i : i.ToString(); model.ExpireMonths.Add(new SelectListItem { Text = text, Value = i.ToString(), }); } return(View(model)); }
public string TakePaymentFromPaypal(int UserPackageId, string BillingAggrementID, DateTime CarServiceDate, int JobId) { string responseString = string.Empty; var db = new GreenProDbEntities(); if (UserPackageId > 0) { var userPackage = db.UserPackages.FirstOrDefault(a => a.Id == UserPackageId); var car = db.CarUsers.FirstOrDefault(a => a.CarId == userPackage.CarId); var package = db.Packages.FirstOrDefault(a => a.PackageId == userPackage.PackageId); var JobDetail = db.Garage_CarDaySetting.Where(i => i.Id == JobId).SingleOrDefault(); //decimal finalPrice = userPackage.TotalPrice + userPackage.TaxAmount + userPackage.TipAmount; // Comment By Nitendra 28 SEP 2016 decimal AddOnsPrice = 0M; AddOnsPrice = userPackage.UserPackagesAddons.Sum(s => s.ActualPrice); decimal finalPrice = userPackage.ActualPrice + AddOnsPrice + userPackage.TaxAmount; if (userPackage != null && (car != null && (car.AutoRenewal))) { string message = string.Empty; string OriResponsePaypal = string.Empty; DoReferenceTrasaction paypal = new DoReferenceTrasaction(); var response = paypal.DoTransaction(BillingAggrementID, package.Package_Description, package.Package_Name, (double)finalPrice, "Weekly Renewal", out OriResponsePaypal); responseString = JsonConvert.SerializeObject(response); responseString = responseString + " : Paypal Response: " + OriResponsePaypal; PaypalAutoPayment paypalAutoPayment = new PaypalAutoPayment(); paypalAutoPayment.UserPackageID = userPackage.Id; paypalAutoPayment.UserID = userPackage.UserId; paypalAutoPayment.IsPaid = response.PaymentStatus == "COMPLETED"? true:false; paypalAutoPayment.GrossAmount = String.Format("{0:0.00}", finalPrice); //Convert.ToString(finalPrice); paypalAutoPayment.PaymentStatus = response.PaymentStatus; paypalAutoPayment.PaymentDate = response.PaymentDate; paypalAutoPayment.TrasactionID = response.TransactionID; paypalAutoPayment.BillingAggrementID = BillingAggrementID; paypalAutoPayment.TransactionDate = DateTime.Now; paypalAutoPayment.ServiceDate = CarServiceDate; paypalAutoPayment.CreatedOn = DateTime.Now; db.PaypalAutoPayments.Add(paypalAutoPayment); db.SaveChanges(); if (paypalAutoPayment.IsPaid) { JobDetail.IsPaid = true; db.SaveChanges(); UserTransaction _transaction = new UserTransaction() { Userid = userPackage.UserId, PaypalId = "", TransactionDate = DateTime.Now, Amount = finalPrice, PackageId = userPackage.Id, Details = "No Details", BillingAggrementID = BillingAggrementID, TrasactionID = paypalAutoPayment.TrasactionID, }; db.UserTransactions.Add(_transaction); db.SaveChanges(); } } } return(responseString); }
public ActionResult Success() { SetExpressCheckOut express = new SetExpressCheckOut(); PayPalSystem.Models.PaypalResponse response = new PayPalSystem.Models.PaypalResponse(); response.ECToken = Request.QueryString["token"]; var token = response.ECToken; var user = db.PayPalLogs.Where(a => a.ECToken == token).FirstOrDefault(); response = express.CreateBillingAgreement(response); PayPalLog log = new PayPalLog() { ACK = "BillingAgreement", ApiSatus = response.ApiStatus, BillingAggrementID = (response.BillingAgreementID == null) ? string.Empty : response.BillingAgreementID, CorrelationID = response.CorrelationID, ECToken = token, ResponseError = (response.ResponseError == null) ? string.Empty : response.ResponseError.ToString(), ResponseRedirectURL = (response.ResponseRedirectURL == null) ? string.Empty : response.ResponseRedirectURL, ServerDate = DateTime.Now, TimeStamp = response.Timestamp, UserId = user.UserId, SubscriptionID = user.SubscriptionID }; db.PayPalLogs.Add(log); db.SaveChanges(); //Save USer Logs var finalPrice = user.UserPackage.TotalPrice + user.UserPackage.TaxAmount + user.UserPackage.TipAmount; UserTransaction _transaction = new UserTransaction() { Userid = user.UserId, PaypalId = user.ECToken, TransactionDate = DateTime.Now, Amount = finalPrice, PackageId = user.SubscriptionID, Details = "No Details", BillingAggrementID = log.BillingAggrementID }; db.UserTransactions.Add(_transaction); db.SaveChanges(); //Logic to disable autorenew if Billingagreement is null //Logic to update Transaction status var userPackages = db.UserPackages.Find(user.SubscriptionID); if (userPackages != null) { string message = string.Empty; string responseString = string.Empty; var package = db.Packages.FirstOrDefault(a => a.PackageId == userPackages.PackageId); string OriResponsePaypal = string.Empty; DoReferenceTrasaction paypal = new DoReferenceTrasaction(); var doResponse = paypal.DoTransaction(log.BillingAggrementID, package.Package_Description, package.Package_Name, (double)finalPrice, "Weekly Renewal", out OriResponsePaypal); ///Write paypal response in txt file. responseString = JsonConvert.SerializeObject(response); responseString = responseString + " : Paypal Response: " + OriResponsePaypal; string fileName = userPackages.Id + "-" + log.BillingAggrementID + "__" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture) + ".txt"; System.IO.File.WriteAllText(Server.MapPath("~/App_Data/" + fileName), responseString); PaypalAutoPayment paypalAutoPayment = new PaypalAutoPayment(); paypalAutoPayment.UserPackageID = userPackages.Id; paypalAutoPayment.UserID = userPackages.UserId; paypalAutoPayment.IsPaid = doResponse.PaymentStatus == "COMPLETED" ? true : false; paypalAutoPayment.GrossAmount = String.Format("{0:0.00}", finalPrice); //Convert.ToString(finalPrice); paypalAutoPayment.PaymentStatus = doResponse.PaymentStatus; paypalAutoPayment.PaymentDate = doResponse.PaymentDate; paypalAutoPayment.TrasactionID = doResponse.TransactionID; paypalAutoPayment.BillingAggrementID = log.BillingAggrementID; paypalAutoPayment.TransactionDate = DateTime.Now; DateTime currentDate = DateTime.Now; DateTime serviceDate = currentDate; if (userPackages.ServiceDay == currentDate.DayOfWeek.ToString()) { serviceDate = currentDate.AddDays(7); } else { DateTime nextServiceDate; for (int i = 1; i <= 6; i++) { if (i == 1) { nextServiceDate = currentDate.AddDays(i); if (userPackages.ServiceDay == nextServiceDate.DayOfWeek.ToString()) { if (userPackages.SubscriptionTypeId == 1) { serviceDate = nextServiceDate.AddDays(7); } else if (userPackages.SubscriptionTypeId == 2) { serviceDate = nextServiceDate.AddDays(14); } else if (userPackages.SubscriptionTypeId == 3) { serviceDate = nextServiceDate.AddDays(28); } else { serviceDate = nextServiceDate.AddDays(7); } break; } } else { nextServiceDate = currentDate.AddDays(i); if (userPackages.ServiceDay == nextServiceDate.DayOfWeek.ToString()) { serviceDate = currentDate.AddDays(i); break; } } } } paypalAutoPayment.ServiceDate = serviceDate; paypalAutoPayment.CreatedOn = DateTime.Now; db.PaypalAutoPayments.Add(paypalAutoPayment); db.SaveChanges(); if (paypalAutoPayment.IsPaid) { userPackages.PaymentRecieved = true; userPackages.IsActive = true; userPackages.NextServiceDate = serviceDate; userPackages.PaymentMethodName = "paypal"; db.Entry(userPackages).State = EntityState.Modified; db.SaveChanges(); ///Added By Sachin 29 SEP 2016 var addOnsServices = userPackages.UserPackagesAddons.ToList(); foreach (var addOns in addOnsServices) { if (!addOns.NextServiceDate.HasValue) { addOns.NextServiceDate = serviceDate; db.SaveChanges(); } } } } Session["NewServiceCarTypeId"] = null; Session["SelectedCar"] = null; Session["NewServiceGarageId"] = null; var userInfo = userPackages.AspNetUser; // Send Notification Mail Admin for buy new subscrition. _workflowMessageService.SendNewSubscriptionNotificationToAdmin(userInfo.FirstName + " " + userInfo.LastName, userInfo.UserName, userPackages.Package.Package_Name, userPackages.SubscribedDate.ToString(), userPackages.CarUser.Garage.Garage_Name); return(View()); }