记录点滴

记录生活

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

HI all,

During past few days i was given a project to develop an application using amazon and ebay api in C#,.net 4.

Well i had a very very bad experience with amazon the documentation was just terrible , no example code. Some samples but not all the parts , so very hard to know anything from their site.

So i searched a lot for almost three days , and developed the update amazon orders status with of multiple messages.

i am attaching/pasting complete code for anyone like me who will i am sure have trouble finding examples, you can use this as it is just change the authorization keys and it will work :) .

——————–Amazon update order status api——————————————-

First of all get the classes downloaded into your application from https://developer.amazonservices.com/gp/mws/api.html?ie=UTF8&section=feeds&group=bde&version=latest

Basically you need to use the submitfeedfunction to post info to amazon , so my first problem was i was searching in orders api .

The main thing using amazon api is to match the exact xml or flat file pattern to post info otherwise it wont work ,even slightest detail like different time format wont work.

What i do is that i have list of orders many with checkboxes i select multiple and call this function on button click , all the check boxes values are combined with , example:

123-4242453252,234-32434-234234 etc..

public void updateAmazonOrderStatus(string orderidz)

{

string curDate = string.Empty;

curDate = GetFormattedTimestamp(DateTime.Now);

/// You need to follow this datetime format that is what i have learned.Maybe i am wrong but i always worked when i used this format and never on any other.

//   ——————————————————————-

String accessKeyId = “#”;

String secretAccessKey = “#”;

String merchantId = “#”;

String marketplaceId = “#”;

string postingOrderFile = string.Empty;

string dateTest = string.Empty;

dateTest = curDate;

postingOrderFile = GenerateInventoryDocument(orderidz, merchantId, dateTest);

UTF8Encoding encoding = new UTF8Encoding();

byte[] byteData = encoding.GetBytes(postingOrderFile);

Stream s = new MemoryStream(byteData);

const string applicationName = “C#”;

const string applicationVersion = “4″;

MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

config.ServiceURL = “https://mws.amazonservices.co.uk”;

//Change the service url according to your need, .co.uk,de,com,ca etc…

MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, applicationName, applicationVersion, config); // You will get these classes from the link i pasted above

MarketplaceWebService.Model.SubmitFeedRequest request = new MarketplaceWebService.Model.SubmitFeedRequest();// You will get these classes from the link i pasted above

request.Merchant = merchantId;

request.MarketplaceIdList = new MarketplaceWebService.Model.IdList();

request.MarketplaceIdList.Id = new List<string>(new string[] { marketplaceId });

request.FeedContent = s;

request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent);

request.FeedContent.Position = 0;

request.PurgeAndReplace = true;

request.FeedType = “_POST_ORDER_FULFILLMENT_DATA_”;

SubmitFeedSample.InvokeSubmitFeed(service, request);

}

//———————————————————————————————————————————————-

//Now posting the various sub-function used in the code above:

public String GetFormattedTimestamp(DateTime dt)

{

DateTime utcTime;

if (dt.Kind == DateTimeKind.Local)

{

utcTime = new DateTime(

dt.Year,

dt.Month,

dt.Day,

dt.Hour,

dt.Minute,

dt.Second,

dt.Millisecond,

DateTimeKind.Local).ToUniversalTime();

}

else

{

// If DateTimeKind.Unspecified, assume UTC.

utcTime = dt;

}

return utcTime.ToString(“yyyy-MM-dd\\THH:mm:ss.fff\\Z”, CultureInfo.InvariantCulture);

}

//I needed this function to generate the xml document needed to post to amazon so it knows which order status to update.

public string GenerateInventoryDocument(string orderNumber, string merchantID,string datecurrent)

{

string myString = string.Empty;

myString = myString + “<?xml version=\”1.0\” ?>”;

myString =myString+ “<AmazonEnvelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xsi:noNamespaceSchemaLocation=\”amzn-envelope.xsd\”>”;

myString = myString + “<Header>”;

myString = myString + “<DocumentVersion>1.01</DocumentVersion>”;

myString = myString + “<MerchantIdentifier>” + merchantID + “</MerchantIdentifier>”;

myString = myString + “</Header>”;

myString = myString + “<MessageType>OrderFulfillment</MessageType>”;

//Add the individual messages to the document.

//As i had multiple order ids so splitting them and adding them to the xml one by one.

string[] splitter = null;

splitter = orderNumber.Split(‘,’);

if (splitter.Length > 0)

{

for (int i = 1; i <= splitter.Length; i++)

{

if (splitter[i-1] != “”)

{

this.CreateInventoryMessage(splitter[i - 1], i, ref myString, datecurrent);

}

}

}

myString = myString + “</AmazonEnvelope>”;

return myString;

}

private void CreateInventoryMessage(string orderID, int MessageID, ref string myString, string curdate)

{

myString = myString + “<Message>”;

myString = myString + “<MessageID>” + MessageID.ToString() + “</MessageID>”;

myString = myString + “<OperationType>Update</OperationType>”;

myString = myString + “<OrderFulfillment>”;

myString = myString + “<AmazonOrderID>” + orderID + “</AmazonOrderID>”;

myString = myString + “<FulfillmentDate>” + curdate + “</FulfillmentDate>”;

myString = myString + “</OrderFulfillment>”;

myString = myString + “</Message>”;

}

And the code ends here :) , it will work 100 % im sure and you do not need to search so much stuff to find all this and put together , that is what i have been through hope you find it usefull anything else just let me know,

Thank you for reading.

MR..XXXXXXXX

posted on 2013-03-19 14:18  啊峰  阅读(1540)  评论(1编辑  收藏  举报