protected void OnSaveToQuote(object sender, EventArgs e) { UpdateCartItems(); var dependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext); var purchasable = GetPurchaseableQuote(Cart, dependencies, Request.RequestContext.HttpContext.Profile.UserName); if (purchasable == null) { throw new ApplicationException("Purchase could not be determined."); } var quote = purchasable.Quote; if (quote == null) { throw new ApplicationException("Quote could not be determined."); } var quoteId = quote.Id; var page = ServiceContext.GetPageBySiteMarkerName(Website, "Quote History"); if (page == null) { throw new ApplicationException("Page could not be found for Site Marker named 'Quote History'"); } var url = ServiceContext.GetUrl(page); if (string.IsNullOrWhiteSpace(url)) { throw new ApplicationException("Url could not be determined for Site Marker named 'Quote History'"); } var urlBuilder = new UrlBuilder(url); urlBuilder.QueryString.Add("QuoteID", quoteId.ToString()); Cart.DeactivateCart(); Response.Redirect(urlBuilder.PathWithQueryString); }
protected virtual void LogPaymentRequest(HttpContext context, PortalConfigurationDataAdapterDependencies dataAdapterDependencies, Tuple <Guid, string> quoteAndReturnUrl, IPaymentValidation validation) { var log = new StringBuilder(); log.AppendLine(GetType().FullName).AppendLine(); log.AppendLine(validation.Log); if (!string.IsNullOrEmpty(validation.ErrorMessage)) { log.AppendLine().AppendFormat("Error message: {0}", validation.ErrorMessage).AppendLine(); } LogPaymentRequest( context, dataAdapterDependencies, quoteAndReturnUrl, "Payment Log ({0})".FormatWith(validation.Success ? "Successful" : "Unsuccessful"), log.ToString()); }
protected virtual void ConvertQuoteToOrder(HttpContext context, PortalConfigurationDataAdapterDependencies dataAdapterDependencies, Guid quoteId, string receiptNumber) { ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Start"); var quoteEntityReference = new EntityReference("quote", quoteId); var serviceContext = dataAdapterDependencies.GetServiceContextForWrite(); var quote = serviceContext.CreateQuery("quote").FirstOrDefault(q => q.GetAttributeValue <Guid>("quoteid") == quoteId); if (quote == null) { ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Quote could not be found with id '{0}'. Sales Order could not be created.", quoteId)); return; } var status = quote.GetAttributeValue <OptionSetValue>("statecode"); if (status == null || status.Value != 0) { ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Quote with id '{0}' statecode '{1}' is not in Draft state. Sales Order could not be created.", quoteId, status == null ? "null" : status.Value.ToString(CultureInfo.InvariantCulture))); return; } // Activate the quote (should be in Draft status [statecode=0]). serviceContext.Execute(new SetStateRequest { EntityMoniker = quoteEntityReference, State = new OptionSetValue(1), Status = new OptionSetValue(2) }); var quoteClose = new Entity("quoteclose"); quoteClose["subject"] = "Payment"; quoteClose["quoteid"] = quoteEntityReference; // Win the quote (required before converting to order). serviceContext.Execute(new WinQuoteRequest { QuoteClose = quoteClose, Status = new OptionSetValue(-1), }); // Convert the quote to an order. var convertQuoteToSalesOrderResponse = (ConvertQuoteToSalesOrderResponse)serviceContext.Execute(new ConvertQuoteToSalesOrderRequest { ColumnSet = new ColumnSet("salesorderid"), QuoteId = quoteEntityReference.Id, }); if (convertQuoteToSalesOrderResponse != null && !string.IsNullOrWhiteSpace(receiptNumber)) { var order = convertQuoteToSalesOrderResponse.Entity; if (order != null) { var orderUpdate = new Entity("salesorder") { Id = order.Id }; orderUpdate["adx_receiptnumber"] = receiptNumber; serviceContext.Attach(orderUpdate); serviceContext.UpdateObject(orderUpdate); serviceContext.SaveChanges(); } } ADXTrace.Instance.TraceInfo(TraceCategory.Application, "End"); }
protected void Page_Load(object sender, EventArgs e) { Target = GetTargetEntityReference(); if (Target == null) { Payment.Visible = false; GeneralErrorMessage.Visible = true; return; } Guid quoteId; if (IsPostBack && Guid.TryParse(QuoteId.Value, out quoteId)) { WebForm.CurrentSessionHistory.QuoteId = quoteId; } var dataAdapterDependencies = new Adxstudio.Xrm.Commerce.PortalConfigurationDataAdapterDependencies(PortalName, Request.RequestContext); var dataAdapter = CreatePurchaseDataAdapter(Target, CurrentStepEntityPrimaryKeyLogicalName); Purchasable = dataAdapter.Select(); if (Purchasable == null) { Payment.Visible = false; GeneralErrorMessage.Visible = true; return; } // If the session quote is not the purchase quote, update and persist the session, as // there won't necessarily be a postback to save the session later. if (WebForm.CurrentSessionHistory.QuoteId != Purchasable.Quote.Id) { WebForm.CurrentSessionHistory.QuoteId = Purchasable.Quote.Id; WebForm.SaveSessionHistory(dataAdapterDependencies.GetServiceContext()); } QuoteId.Value = Purchasable.Quote.Id.ToString(); if (Purchasable.TotalAmount == 0) { var portal = PortalCrmConfigurationManager.CreatePortalContext(PortalName); var context = dataAdapterDependencies.GetServiceContext(); var quote = context.CreateQuery("quote").First(q => q.GetAttributeValue <Guid>("quoteid") == Purchasable.Quote.Id); var adapter = new CoreDataAdapter(portal, context); var status = quote.GetAttributeValue <OptionSetValue>("statecode").Value; if (status != (int)QuoteState.Active) { adapter.SetState(quote.ToEntityReference(), (int)QuoteState.Active, (int)QuoteStatusCode.InProgressActive); } if (status != (int)QuoteState.Won) { adapter.WinQuote(quote.ToEntityReference()); } adapter.CovertQuoteToSalesOrder(quote.ToEntityReference()); dataAdapter.CompletePurchase(); SetAttributeValuesAndSave(); MoveToNextStep(); return; } if (Paid) { dataAdapter.CompletePurchase(); SetAttributeValuesAndSave(); MoveToNextStep(); return; } Payment.Visible = true; GeneralErrorMessage.Visible = false; if (IsPaymentError) { SetErrorFields(); } SetMerchantShippingFields(ServiceContext.CreateQuery("quote").FirstOrDefault(q => q.GetAttributeValue <Guid>("quoteid") == Purchasable.Quote.Id)); if (IsPaymentAuthorizeNet || IsPaymentDemo) { CreditCardPaymentPanel.Visible = true; PayPalPaymentPanel.Visible = false; SetMerchantFields(); PopulateContactInfo(Contact); EnableTestMode(TestModeEnabled); PurchaseDiscounts.DataSource = Purchasable.Discounts; PurchaseDiscounts.DataBind(); PurchaseItems.DataSource = Purchasable.Items.Where(item => item.IsSelected && item.Quantity > 0); PurchaseItems.DataBind(); } else if (IsPaymentPaypal) { PayPalPaymentPanel.Visible = true; CreditCardPaymentPanel.Visible = false; PayPalPurchaseDiscounts.DataSource = Purchasable.Discounts; PayPalPurchaseDiscounts.DataBind(); PayPalPurchaseItems.DataSource = Purchasable.Items.Where(item => item.IsSelected && item.Quantity > 0); PayPalPurchaseItems.DataBind(); } }