Advertisement
mithereal

Untitled

Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. ## incoming data
  2. {
  3. "id": "123abc"
  4. "Recipient": {
  5. "FirstName": "xxx",
  6. "LastName": "xxx",
  7. "Address": "12435",
  8. "City": "tucson",
  9. "State": "az",
  10. },
  11. "LineItems": [
  12. {
  13. "ItemName": "test item",
  14. "Quantity" : 1
  15. }
  16. ]
  17. }
  18.  
  19. ## models
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Web;
  24.  
  25. namespace RainWorx.FrameWorx.MVC.Models.Shipping_Easy
  26. {
  27.     public class EasyShipOrder
  28.     {
  29.         public string id { get; set; }
  30.         public EasyShipRecipient Recipient { get; set; }
  31.         public List<EasyShipLineItem> LineItems { get; set; }
  32.     }
  33. }
  34.  
  35. using System;
  36. using System.Collections.Generic;
  37. using System.Linq;
  38. using System.Web;
  39.  
  40. namespace RainWorx.FrameWorx.MVC.Models.Shipping_Easy
  41. {
  42.     [Serializable]
  43.     public class EasyShipRecipient
  44.     {
  45.         public string FirstName { get; set; }
  46.         public string LastName { get; set; }
  47.         public string Address { get; set; }
  48.         public string City { get; set; }
  49.         public string State { get; set; }
  50.    
  51.     }
  52. }
  53.  
  54. using System;
  55. using System.Collections.Generic;
  56. using System.Linq;
  57. using System.Web;
  58.  
  59. namespace RainWorx.FrameWorx.MVC.Models.Shipping_Easy
  60. {
  61.     public class EasyShipLineItem
  62.     {
  63.         public string ItemName { get; set; }
  64.         public int Quantity { get; set; }
  65.     }
  66. }
  67.  
  68. ## controller fun
  69. [AcceptVerbs(HttpVerbs.Post)]
  70.         public ActionResult ShippingEasy(string Data)
  71.         {
  72.             String baseUrl = "https://app.shippingeasy.com";
  73.             String EasyShipMode = Resources.EasyShip.Mode;
  74.  
  75.             // from Settings > API Credentials
  76.             String apiKey = "";
  77.             String apiSecret = "";
  78.  
  79.             // from Settings > Stores
  80.             String storeApiKey = "";
  81.  
  82.             JavaScriptSerializer serializer = new JavaScriptSerializer();
  83.  
  84.             Request.InputStream.Position = 0;
  85.  
  86.             var input = new StreamReader(Request.InputStream).ReadToEnd();
  87.  
  88.             var order = serializer.Deserialize<Models.Shipping_Easy.EasyShipOrder>(input);
  89.             var recipient = order.Recipient;
  90.             var lineitems = order.LineItems;
  91.  
  92.             if (EasyShipMode == "test")
  93.             {
  94.                 apiKey = Resources.EasyShip.DemoClientApiKey;
  95.                 apiSecret = Resources.EasyShip.DemoClientSecret;
  96.                 storeApiKey = Resources.EasyShip.DemoClientStoreApiKey;
  97.             }
  98.             else
  99.             {
  100.                 apiKey = Resources.EasyShip.ProductionClientApiKey;
  101.                 apiSecret = Resources.EasyShip.ProductionClientSecret;
  102.                 storeApiKey = Resources.EasyShip.ProductionClientStoreApiKey;
  103.             }
  104.  
  105.                 var client = new Client(apiKey, apiSecret, baseUrl);
  106.  
  107.             var result = client.CreateOrder(storeApiKey, new Order
  108.             {
  109.                 ExternalOrderIdentifier = order.id,
  110.                 OrderedAt = DateTime.Now,
  111.                 Recipients =
  112.                     {
  113.                         new Recipient
  114.                         {
  115.                             FirstName = recipient.FirstName,
  116.                             LastName = recipient.LastName,
  117.                             Address = recipient.Address,
  118.                             City = recipient.City,
  119.                             State = recipient.State,
  120.  
  121.                             LineItems =  lineitems.Select(x => new ShippingEasy.LineItem{ ItemName = x.ItemName, Quantity = x.Quantity}).ToList()
  122.  
  123.  
  124.                            
  125.                         }
  126.                     },
  127.             });
  128.              ShippingEasy.HttpResponse response = result.HttpResponse;
  129.  
  130.             return View();
  131.  
  132.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement