private static string BuildEbayHeaderSql(EbayOrderHeaderType eohType) { string sql = @"INSERT INTO [dbo].[EbayOrderHeader] ([OrderID],[AdjustmentAmount],[AmountPaid],[AmountSaved],[BuyerCheckoutMessage] ,[BuyerUserID],[eBayPaymentStatus],[PaymentMethod],[CreatedTime],[ExtendedOrderID] ,[OrderStatus],[PaidTime],[PaymentExpectedReleaseDate],[PaymentHoldReason] ,[PaymentHoldStatus],[SellerEmail],[SellerUserID],[AddressID] ,[CityName],[Country],[CountryName],[ExternalAddressID] ,[Name],[Phone],[PostalCode],[StateOrProvince] ,[Street1] ,[Street2] ,[SalesTaxAmount],[SalesTaxPercent],[ShippingIncludedInTax] ,[SalesTaxState],[SellingManagerSalesRecordNumber],[ExpeditedService] ,[ShippingService],[ShippingServiceCost],[ShippingServiceAdditionalCost] ,[ShippingServicePriority],[Subtotal],[Total],[TransferStatus] ,[EnterDate],[UpdateDate]) VALUES ('" + eohType.OrderID + "','" + eohType.AdjustmentAmount + "','" + eohType.AmountPaid + "','" + eohType.AmountSaved + "','" + eohType.BuyerCheckoutMessage + "',N'" + eohType.BuyerUserID + "','" + eohType.eBayPaymentStatus + "','" + eohType.PaymentMethod + "','" + eohType.CreatedTime + "','" + eohType.ExtendedOrderID + "','" + eohType.OrderStatus + "','" + eohType.PaidTime + "','" + eohType.PaymentExpectedReleaseDate + "','" + eohType.PaymentHoldReason + "','" + eohType.PaymentHoldStatus + "','" + eohType.SellerEmail + "','" + eohType.SellerUserID + "','" + eohType.AddressID + "',N'" + eohType.CityName + "',N'" + eohType.Country + "',N'" + eohType.CountryName + "','" + eohType.ExternalAddressID + "',N'" + eohType.Name + "','" + eohType.Phone + "',N'" + eohType.PostalCode + "',N'" + eohType.StateOrProvince + "',N'" + eohType.Street1 + "',N'" + eohType.Street2 + "','" + eohType.SalesTaxAmount + "','" + eohType.SalesTaxPerecent + "','" + eohType.ShippingIncludedInTax + "',N'" + eohType.SalesTaxState + "','" + eohType.SellingManagerSalesRecordNumber + "','" + eohType.ExpeditedService + "','" + eohType.ShippingService + "','" + eohType.ShippingServiceCost + "','" + eohType.ShippingServiceAdditionalCost + "','" + eohType.ShippingServicePriority + "','" + eohType.Subtotal + "','" + eohType.Total + "','" + eohType.TransferStatus + "','" + eohType.EnterDate + "','" + eohType.UpdateDate + "')"; return(sql); }
public static List<EbayOrderType> GetOrderFromEbay(string token,string accountNum) { string senderEmail = ConfigurationManager.AppSettings["senderEmail"]; string messageFromPassword= ConfigurationManager.AppSettings["messageFromPassword"]; string messageToEmail = ConfigurationManager.AppSettings["messageToEmail"]; string smtpClient = ConfigurationManager.AppSettings["smtpClient"]; int smtpPortNum = ConvertUtility.ToInt(ConfigurationManager.AppSettings["smtpPortNum"]); List<EbayOrderType> OrderList = new List<EbayOrderType>(); ApiContext context = new ApiContext(); context.ApiCredential.eBayToken = token; context.SoapApiServerUrl = "https://api.ebay.com/wsapi"; context.Site = SiteCodeType.US; DateTime createTimeFrom, createTimeTo; GetOrdersCall GetOrderCall = new GetOrdersCall(context); GetOrderCall.DetailLevelList = new DetailLevelCodeTypeCollection(); GetOrderCall.DetailLevelList.Add(DetailLevelCodeType.ReturnAll); createTimeFrom = DateTime.Now.AddDays(-15).ToUniversalTime(); createTimeTo = DateTime.Now.ToUniversalTime(); int pageNumber = 1; int errorCount = 0; GetOrderCall.CreateTimeFrom = createTimeFrom; GetOrderCall.CreateTimeTo = createTimeTo; GetOrderCall.OrderStatus = OrderStatusCodeType.Completed; while (pageNumber<100) { try { PaginationType pagination = new PaginationType(); pagination.EntriesPerPage = 100; GetOrderCall.Pagination = pagination; pagination.PageNumber = pageNumber; GetOrderCall.Execute(); int totalPageNumber = GetOrderCall.PaginationResult.TotalNumberOfPages; if (pageNumber > totalPageNumber) { break; } else { if (GetOrderCall.ApiResponse.Ack != AckCodeType.Failure) { if (GetOrderCall.ApiResponse.OrderArray.Count != 0) { foreach (OrderType orderType in GetOrderCall.ApiResponse.OrderArray) { try { EbayOrderType ebayOrderType = new EbayOrderType(); string orderId = orderType.OrderID; DataRow checkDuplidatedDr = Db.Db.CheckDuplicatedOrderID(orderId); if (checkDuplidatedDr!=null) { if(checkDuplidatedDr["OrderStatus"].ToString() == "Completed") { continue; // continue if order already exist } else { Db.Db.DeleteUncompletedEbayOrder(orderId); } } else { EbayOrderHeaderType ebayOrderHeaderType = AddOrderHeader(orderType);//add header info foreach (ExternalTransactionType externalTransactionType in orderType.ExternalTransaction)//add external trasnaction info { EbayOrderPaymentTransactionType ebayOrderPaymentTransactionType = AddOrderPaymentTransaction(orderId, externalTransactionType); ebayOrderType.paymentTransaction.Add(ebayOrderPaymentTransactionType); } ebayOrderType.Header = ebayOrderHeaderType; int lineId = 0; foreach (TransactionType transactionType in orderType.TransactionArray)//add line info { lineId = lineId + 1; EbayOrderLineType ebayOrderLineType = AddOrderLine(orderId, transactionType, lineId); ebayOrderType.Line.Add(ebayOrderLineType); } OrderList.Add(ebayOrderType); } } catch(Exception ex) { ExceptionUtility exceptionUtility = new ExceptionUtility(); exceptionUtility.CatchMethod(ex, accountNum + ": GetOrderCall--foreach ", ex.Message.ToString(), senderEmail, messageFromPassword, messageToEmail, smtpClient, smtpPortNum); continue; //continue next single order if current order error } } } else { continue; // continue next account if no order } } else { ExceptionUtility exceptionUtility = new ExceptionUtility(); exceptionUtility.ErrorWarningMethod("AckCodeType Failure: " + accountNum, "AckCodeType Failure: " + accountNum, senderEmail, messageFromPassword, messageToEmail, smtpClient, smtpPortNum); continue; // continue next account if ack error } } pageNumber = pageNumber + 1; } catch (Exception ex) { if (errorCount > 4) { ExceptionUtility exceptionUtility = new ExceptionUtility(); exceptionUtility.CatchMethod(ex,accountNum+ ": While ",ex.Message.ToString(),senderEmail,messageFromPassword,messageToEmail,smtpClient,smtpPortNum); break; } else { errorCount = errorCount + 1; continue; // continue try if error less 4 times } } } return OrderList; }
private static EbayOrderHeaderType AddOrderHeader(OrderType orderType) { EbayOrderHeaderType ebayOrderHeaderType = new EbayOrderHeaderType(); ebayOrderHeaderType.OrderID = orderType.OrderID; ebayOrderHeaderType.AdjustmentAmount = orderType.AdjustmentAmount.Value; ebayOrderHeaderType.AmountPaid = orderType.AmountPaid.Value; ebayOrderHeaderType.AmountSaved = orderType.AmountSaved.Value; ebayOrderHeaderType.BuyerCheckoutMessage = orderType.BuyerCheckoutMessage == null ? "" : orderType.BuyerCheckoutMessage.ToString().Replace("'", "''"); ebayOrderHeaderType.BuyerUserID = orderType.BuyerUserID.ToString().Replace("'", "''"); ebayOrderHeaderType.eBayPaymentStatus = orderType.CheckoutStatus.eBayPaymentStatus.ToString().Replace("'", "''"); ebayOrderHeaderType.PaymentMethod = orderType.CheckoutStatus.PaymentMethod.ToString().Replace("'", "''"); if (orderType.CreatedTime.ToLocalTime() <= ConvertUtility.ToDateTime("1901-01-01 00:00:00")) { ebayOrderHeaderType.CreatedTime = (DateTime)SqlDateTime.MinValue; } else { ebayOrderHeaderType.CreatedTime = orderType.CreatedTime.ToLocalTime(); } ebayOrderHeaderType.ExtendedOrderID = orderType.ExtendedOrderID == null ? "" : orderType.ExtendedOrderID.ToString(); ebayOrderHeaderType.OrderStatus = orderType.OrderStatus.ToString(); if (orderType.PaidTime.ToLocalTime() <= ConvertUtility.ToDateTime("1901-01-01 00:00:00")) { ebayOrderHeaderType.PaidTime = (DateTime)SqlDateTime.MinValue; } else { ebayOrderHeaderType.PaidTime = orderType.PaidTime.ToLocalTime(); } if (orderType.PaymentHoldDetails == null) { ebayOrderHeaderType.PaymentExpectedReleaseDate = (DateTime)SqlDateTime.MinValue; ebayOrderHeaderType.PaymentHoldReason = ""; } else { if (orderType.PaymentHoldDetails.ExpectedReleaseDate.ToLocalTime() <= ConvertUtility.ToDateTime("1901-01-01 00:00:00")) { ebayOrderHeaderType.PaymentExpectedReleaseDate = (DateTime)SqlDateTime.MinValue; } else { ebayOrderHeaderType.PaymentExpectedReleaseDate = orderType.PaymentHoldDetails.ExpectedReleaseDate.ToLocalTime(); } ebayOrderHeaderType.PaymentHoldReason = orderType.PaymentHoldDetails.PaymentHoldReason.ToString(); } ebayOrderHeaderType.PaymentHoldStatus = orderType.PaymentHoldStatus.ToString(); ebayOrderHeaderType.SellerEmail = orderType.SellerEmail.Replace("'", "''"); ebayOrderHeaderType.SellerUserID = orderType.SellerUserID.Replace("'", "''"); ebayOrderHeaderType.AddressID = orderType.ShippingAddress.AddressID; ebayOrderHeaderType.CityName = orderType.ShippingAddress.CityName.Replace("'", "''"); ebayOrderHeaderType.Country = orderType.ShippingAddress.Country.ToString(); ebayOrderHeaderType.CountryName = orderType.ShippingAddress.CountryName.ToString().Replace("'", "''"); ebayOrderHeaderType.ExternalAddressID = orderType.ShippingAddress.ExternalAddressID == null ? "" : orderType.ShippingAddress.ExternalAddressID.ToString(); ebayOrderHeaderType.Name = orderType.ShippingAddress.Name == null ? "" : orderType.ShippingAddress.Name.ToString().Replace("'", "''"); ebayOrderHeaderType.Phone = orderType.ShippingAddress.Phone == null ? "" : orderType.ShippingAddress.Phone.ToString().Replace("'", "''"); ; ebayOrderHeaderType.PostalCode = orderType.ShippingAddress.PostalCode == null ? "" : orderType.ShippingAddress.PostalCode.ToString().Replace("'", "''"); ebayOrderHeaderType.StateOrProvince = orderType.ShippingAddress.StateOrProvince == null ? "" : orderType.ShippingAddress.StateOrProvince.ToString().Replace("'", "''"); ebayOrderHeaderType.Street1 = orderType.ShippingAddress.Street1 == null ? "" : orderType.ShippingAddress.Street1.ToString().Replace("'", "''"); ebayOrderHeaderType.Street2 = orderType.ShippingAddress.Street2 == null ? "" : orderType.ShippingAddress.Street2.ToString().Replace("'", "''"); ebayOrderHeaderType.SalesTaxAmount = orderType.ShippingDetails.SalesTax.SalesTaxAmount == null ? 0 : orderType.ShippingDetails.SalesTax.SalesTaxAmount.Value; ebayOrderHeaderType.SalesTaxPerecent = ConvertUtility.ToDouble(orderType.ShippingDetails.SalesTax.SalesTaxPercent.ToString()); ebayOrderHeaderType.SalesTaxState = orderType.ShippingDetails.SalesTax.SalesTaxState == null ? "" : orderType.ShippingDetails.SalesTax.SalesTaxState.ToString(); ebayOrderHeaderType.ShippingIncludedInTax = orderType.ShippingDetails.SalesTax.ShippingIncludedInTax == true ? 1 : 0; ebayOrderHeaderType.SellingManagerSalesRecordNumber = orderType.ShippingDetails.SellingManagerSalesRecordNumber.ToString(); ebayOrderHeaderType.ExpeditedService = orderType.ShippingServiceSelected.ExpeditedService == true ? 1 : 0; ebayOrderHeaderType.ShippingService = orderType.ShippingServiceSelected.ShippingService == null ? "" : orderType.ShippingServiceSelected.ShippingService.ToString(); ebayOrderHeaderType.ShippingServiceCost = orderType.ShippingServiceSelected.ShippingServiceCost == null ? 0 : orderType.ShippingServiceSelected.ShippingServiceCost.Value; ebayOrderHeaderType.ShippingServiceAdditionalCost = orderType.ShippingServiceSelected.ShippingServiceAdditionalCost == null ? 0 : orderType.ShippingServiceSelected.ShippingServiceAdditionalCost.Value; ebayOrderHeaderType.ShippingServicePriority = orderType.ShippingServiceSelected.ShippingServicePriority; ebayOrderHeaderType.Subtotal = orderType.Subtotal == null ? 0 : orderType.Subtotal.Value; ebayOrderHeaderType.Total = orderType.Total == null ? 0 : orderType.Total.Value; ebayOrderHeaderType.TransferStatus = 0; ebayOrderHeaderType.EnterDate = System.DateTime.Now; ebayOrderHeaderType.UpdateDate = System.DateTime.Now; return ebayOrderHeaderType; }