public virtual async Task <InvoiceModel> PrepareInvoiceModelAsync(TblInvoices invoice) { var result = invoice.Adapt <InvoiceModel>(); result.UserName = invoice.User?.UserName; result.InvoiceDetails = PrepareInvoiceDetailsModel(invoice.InvoiceDetails); result.InvoiceSubTotal = invoice.ComputeInvoiceTotalAmount(false, false); result.InvoiceTotal = invoice.ComputeInvoiceTotalAmount(false); if (invoice.BillingAddress != null && invoice.BillingAddress.Any()) { result.UserBillingAddress = PrepareInvoiceBillingAddressModel(invoice.BillingAddress.FirstOrDefault()); result.UserBillingAddress.InvoiceStatus = invoice.Status; } else { if (invoice.Status == InvoiceStatus.Pending && invoice.User != null) { //Try load user latest billing address var address = await _invoiceService.FindUserLatestBillingAddressAsync(invoice.UserId) ?? new TblInvoiceBillingAddress() { Email = invoice.User.Email, FirstName = invoice.User.FirstName, LastName = invoice.User.LastName, CountryId = invoice.User.UserCountryId ?? 0 }; result.UserBillingAddress = PrepareInvoiceBillingAddressModel(address); result.UserBillingAddress.InvoiceStatus = invoice.Status; } } if (result.UserBillingAddress == null) { result.UserBillingAddress = new InvoiceBillingAddressModel { InvoiceStatus = invoice.Status }; } if (invoice.Status == InvoiceStatus.Paid) { result.PaymentGateways = new List <IPaymentMethod>() { _paymentGatewayManager.FindPaymentMethodBySystemName(invoice.PaymentGatewaySystemName) }; } else { result.PaymentGateways = _paymentGatewayManager.GetAvailablePaymentGatewaysForCurrency(_workContext.CurrentCurrency.IsoCode); } return(result); }
public virtual async Task <ActionResult> Index(Guid Id, string paymentMethod, InvoiceBillingAddressModel address) { var invoice = await _invoiceService.FindByIdAsync(Id); if (invoice == null) { return(View("PageNotFound")); } if (invoice.InvoiceDetails.Count == 0) { ViewBag.Message = _localizationService.GetResource("ShoppingCartEmpty"); return(View(await _invoiceModelFactory.PrepareInvoiceModelAsync(invoice))); } if (CurrentSettings.GetBillingAddressForInvoice) { if (!ModelState.IsValid) { return(View(await _invoiceModelFactory.PrepareInvoiceModelAsync(invoice))); } } else { if (!ModelState.IsValidField("Id") || !ModelState.IsValidField("paymentMethod")) { return(View(await _invoiceModelFactory.PrepareInvoiceModelAsync(invoice))); } } if (CurrentSettings.GetBillingAddressForInvoice) { await _invoiceService.AddUpdateBillingAddressAsync(Id, _invoiceModelFactory.PrepareTblInvoiceBillingAddress(address)); } var paymentGateway = _paymentGatewayManager.FindPaymentMethodBySystemName(paymentMethod); if (invoice.ComputeInvoiceTotalAmount() <= 0) { await _invoiceService.SetGatewayNameAndTokenAsync(Id, paymentGateway.PaymentGatewayName, paymentGateway.PaymentGatewaySystemName, "-", WorkContext.CurrentCurrency.Id); return(RedirectToAction("VerifyPayment", new { id = Id })); } var result = await paymentGateway.RequestForPaymentUrl( Url.Action("VerifyPayment", "Invoice", new { id = Id }, Request.Url.Scheme), invoice); if (result.IsSuccess) { await _invoiceService.SetGatewayNameAndTokenAsync(Id, paymentGateway.PaymentGatewayName, paymentGateway.PaymentGatewaySystemName, result.Token, WorkContext.CurrentCurrency.Id); if (result.PostDate != null) { return(new RedirectAndPostActionResult(result.RedirectUrl, result.PostDate)); } return(Redirect(result.RedirectUrl)); } ViewBag.Message = result.ErrorMessage; return(View(await _invoiceModelFactory.PrepareInvoiceModelAsync(invoice))); }