Skip to content

Instantly share code, notes, and snippets.

@tgeorge91
Last active November 17, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgeorge91/02cd1ddc1ff027011d97fc204a8fdcdf to your computer and use it in GitHub Desktop.
Save tgeorge91/02cd1ddc1ff027011d97fc204a8fdcdf to your computer and use it in GitHub Desktop.
using DYMO.Label.Framework;
using System;
using System.Text;
using System.Configuration;
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;
using eBay.Service.Call;
namespace EbayAutomatedShipping
{
class Program
{
static void Main(string[] args)
{
try {
var userToken = ConfigurationManager.AppSettings["userToken"];
var apiContext = new ApiContext { ApiCredential = new ApiCredential(userToken) };
var orders = GetOrders(apiContext);
foreach (OrderType order in orders)
{
if (!order.ShippedTimeSpecified && order.AmountPaid.Value > 0)
{
var address = FormatAddress(order.ShippingAddress);
var transaction = order.TransactionArray[0];
var item = transaction.Item;
var itemFromConfig = ConfigurationManager.AppSettings[item.ItemID];
if (itemFromConfig == null) continue;
var initialQuantity = int.Parse(itemFromConfig);
var quantity = initialQuantity * transaction.QuantityPurchased;
PrintShippingLabel(address, quantity);
ShipOrder(apiContext, transaction.TransactionID, item.ItemID);
}
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Completed");
Console.ReadLine();
}
catch (ExpectedException) { } // do nothing, it's already been done
catch (Exception ex)
{
Console.WriteLine("ERROR IN Main!!!!!!!!!!!!!!!!");
Console.WriteLine(ex.ToString());
}
}
static OrderTypeCollection GetOrders(ApiContext apiContext)
{
try {
var getOrdersCall = new GetOrdersCall(apiContext)
{
NumberOfDays = 14
};
getOrdersCall.Execute();
return getOrdersCall.ApiResponse.OrderArray;
} catch(Exception ex)
{
Console.WriteLine("ERROR IN GetOrders!!!!!!!!!!!!!!!!");
Console.WriteLine(ex.ToString());
throw new ExpectedException();
}
}
static string FormatAddress(AddressType addressType)
{
try {
var address = new StringBuilder();
address.Append(addressType.Name);
address.Append("\r\n");
address.Append(addressType.Street1);
address.Append("\r\n");
if (!string.IsNullOrEmpty(addressType.Street2))
{
address.Append(addressType.Street2);
address.Append("\r\n");
}
address.Append($"{addressType.CityName}, {addressType.StateOrProvince} {addressType.PostalCode}");
return address.ToString();
}
catch (Exception ex)
{
Console.WriteLine("ERROR IN FormatAddress!!!!!!!!!!!!!!!!");
Console.WriteLine(ex.ToString());
throw new ExpectedException();
}
}
static void PrintShippingLabel (string address, int quantity)
{
try {
Console.WriteLine($"Shipping {quantity} to {address}");
Console.WriteLine(""); // empty line for easier reading later.
var label = Label.Open(@"C:\Users\Timothy\Documents\DYMO Label\Labels\address-bottom-template.label");
label.SetObjectText("Address", address);
label.SetObjectText("Quantity", quantity.ToString());
var printParams = new LabelWriterPrintParams();
printParams.RollSelection = RollSelection.Left;
label.Print("DYMO LabelWriter 450 Twin Turbo", printParams);
}
catch (Exception ex)
{
Console.WriteLine("ERROR IN PrintShippingLabel!!!!!!!!!!!!!!!!");
Console.WriteLine(ex.ToString());
throw new ExpectedException();
}
}
static void ShipOrder(ApiContext apiContext, string transactionId, string itemId)
{
try {
var fulfillOrderCall = new CompleteSaleCall(apiContext)
{
TransactionID = transactionId,
FeedbackInfo = new FeedbackInfoType { CommentType = CommentTypeCodeType.Positive, CommentText = "Fast Payment! A+ Buyer!!" },
Shipped = true,
ItemID = itemId
};
fulfillOrderCall.Execute();
}
catch (Exception ex)
{
Console.WriteLine("ERROR IN ShipOrder!!!!!!!!!!!!!!!!");
Console.WriteLine(ex.ToString());
throw new ExpectedException();
}
}
}
public class ExpectedException : Exception { } // This doesn't do anything, just lets Main know that the exception was already caught!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment