public bool InvoiceOrder(OrderResponse invoiceResponse, ConnectorPaymentProvider connectorPaymentProvider, AuditLog4Net.Adapter.IAuditLogAdapter log)
        {
            Concentrator.Payment.Providers.AfterPayMerchant.MerchantClient client = new Concentrator.Payment.Providers.AfterPayMerchant.MerchantClient();
            try
            {
                //user credentials
                client.ClientCredentials.UserName.UserName = connectorPaymentProvider.UserName;
                client.ClientCredentials.UserName.Password = connectorPaymentProvider.Password;

                var request = new Concentrator.Payment.Providers.AfterPayMerchant.CreateInvoice();

                request.OrderNumber = invoiceResponse.OrderResponseLines.FirstOrDefault(x => x.OrderLineID.HasValue).OrderLine.Order.WebSiteOrderNumber;
                request.Portfolio   = connectorPaymentProvider.Portfolio;

                request.InvoiceDate   = invoiceResponse.InvoiceDate.Value;
                request.InvoiceNumber = invoiceResponse.InvoiceDocumentNumber;

                request.InvoiceLines = (from iLine in invoiceResponse.OrderResponseLines
                                        let quantity = iLine.Invoiced < 0 ? 0 - iLine.Invoiced : iLine.Invoiced
                                                       where iLine.OrderLineID.HasValue
                                                       select new InvoiceLine
                {
                    LineNumber = int.Parse(iLine.OrderLine.CustomerOrderLineNr), //web line number;
                    Quantity = quantity,                                         //quantity to invoice;
                    //UnitPriceInVat = iLine.OrderLine.Price.HasValue ? (decimal)iLine.OrderLine.Price.Value : 0 //unit price from invoice

                    UnitPriceInVat = (iLine.VatAmount.HasValue ? iLine.Price + iLine.VatAmount.Value : iLine.Price * (decimal)1.19) / quantity                 //unit price from invoice
                }).ToArray();

                var response = client.CreateInvoice(request);


                if (response.Success)
                {
                    return(true);
                }
                else
                {
                    log.AuditError(string.Format("Error processing AfterPay invoice for order {0} error {1}", invoiceResponse.OrderResponseID, response.ErrorMessage), "AfterPay invoice Process");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                log.Error("Failed Afterpay create invoice", ex);
                return(false);
            }
            finally
            {
                client.Close();
            }
        }
        public int DispatchOrders(Dictionary <Models.Orders.Order, List <Models.Orders.OrderLine> > orderLines, Models.Vendors.Vendor vendor, AuditLog4Net.Adapter.IAuditLogAdapter log, DataAccess.UnitOfWork.IUnitOfWork uni)
        {
            try
            {
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConcentratorMailerUrl"]))
                {
                    throw new Exception("ConcentratorMailerUrl setting empty or not in Config");
                }

                var mailerUrl = new Uri(new Uri(ConfigurationManager.AppSettings["ConcentratorMailerUrl"]), "SendIngramOrder");

                List <IngramMailProduct> productList = new List <IngramMailProduct>();

                foreach (var order in orderLines.Keys)
                {
                    foreach (var orderline in orderLines[order])
                    {
                        IngramMailProduct product = new IngramMailProduct();

                        product.CustomItemNumber   = orderline.Product.VendorAssortments.FirstOrDefault().CustomItemNumber;
                        product.ProductDescription = orderline.Product.VendorAssortments.FirstOrDefault().ShortDescription;
                        product.Price            = orderline.Price.ToString();
                        product.NumberOfProducts = orderline.Quantity.ToString();

                        productList.Add(product);
                    }


                    var data = new IngramMailData
                    {
                        CustomerName = order.ShippedToCustomer.CustomerName,
                        Address      = order.ShippedToCustomer.CustomerAddressLine1,
                        Email        = order.ShippedToCustomer.CustomerEmail,
                        PhoneNumber  = order.ShippedToCustomer.CustomerTelephone,
                        ProductList  = productList
                    };

                    StringBuilder     requestString = new StringBuilder();
                    XmlWriterSettings settings      = new XmlWriterSettings();
                    settings.Encoding = Encoding.UTF8;
                    using (XmlWriter xw = XmlWriter.Create(requestString, settings))
                    {
                        XmlSerializer           serializer = new XmlSerializer(typeof(IngramMailData));
                        XmlSerializerNamespaces nm         = new XmlSerializerNamespaces();
                        nm.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                        serializer.Serialize(xw, data);

                        XmlDocument document = new XmlDocument();
                        document.LoadXml(requestString.ToString());
                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(mailerUrl);
                        request.Method = "POST";

                        byte[] byteData = UTF8Encoding.UTF8.GetBytes(document.OuterXml);

                        using (Stream s = request.GetRequestStream())
                        {
                            s.Write(byteData, 0, byteData.Length);
                        }

                        var result = request.GetResponse();

                        log.AuditSuccess("The order has been successfully mailed to the customer");

                        LogOrder(document, vendor.VendorID, string.Format("{0}.xml", order.OrderID), log);
                    }
                }
                return(1);
            }
            catch (Exception e)
            {
                log.Error(e.Message, e.InnerException);

                return(0);
            }
        }