MNG Kargo Entegrasyon | C#

Merhaba, MNG Kargo API’lerini kullanmakla ilgili ufak bir çalışmam oldu, buradan paylaşmak istedim.

internal class Program
{
    static void Main(string[] args)
    {
        MngGirisIstek objMngGiris = new MngGirisIstek()
        {
            customerNumber = "MNG'den verilen müşteri numarası", // Degisecek
            identityType = 1,
            password = "MNG'den verilen şifre"// Degisecek
        };
        string mngTokenCevap = ApiIstekGonder("https://testapi.mngkargo.com.tr/mngapi/api/", "token", "", objMngGiris);

        Order _order = new Order();
        _order.referenceId = "TekilFaturaNo-001"; // Degisecek
        _order.barcode = "BBB00001";
        _order.billOfLandingId = "İrsaliye 1";
        _order.isCOD = 0;
        _order.codAmount = 0;
        _order.shipmentServiceType = 1;
        _order.packagingType = 3;
        _order.content = "İçerik 1";
        _order.smsPreference1 = 0;
        _order.smsPreference2 = 0;
        _order.smsPreference3 = 0;
        _order.paymentType = 1;
        _order.deliveryType = 1;
        _order.description = "Açıklama 1";
        _order.marketPlaceShortCode = "";
        _order.marketPlaceSaleCode = "";
        _order.pudoId = "Açıklama 1";

        OrderPieceList _orderpiecelist = new OrderPieceList();
        _orderpiecelist.barcode = "MalzemeNo-001";// Degisecek
        _orderpiecelist.desi = 2;
        _orderpiecelist.kg = 2;
        _orderpiecelist.content = "Malzeme Açıklama-001";

        Recipient _recipient = new Recipient();
        _recipient.customerId = "";
        _recipient.refCustomerId = "";
        _recipient.cityCode = 0;
        _recipient.cityName = "Adana";
        _recipient.districtName = "Pozantı";
        _recipient.districtCode = 0;
        _recipient.address = "Merkez Cami yanı";
        _recipient.bussinessPhoneNumber = "5050000000";
        _recipient.email = "musterieposta@zamkinos.net";
        _recipient.taxOffice = "";
        _recipient.taxNumber = "";
        _recipient.fullName = "Zaim Akınöz";
        _recipient.homePhoneNumber = "5050000000";
        _recipient.mobilePhoneNumber = "5050000000";

        MngCreateOrderIstek objMngCreateOrderIstek = new MngCreateOrderIstek()
        {
            order = _order,
            orderPieceList = new List<OrderPieceList>() { _orderpiecelist },
            recipient = _recipient
        };
        string mngCreateOrderCevap = ApiIstekGonder("https://testapi.mngkargo.com.tr/mngapi/api/standardcmdapi/", "createOrder", mngTokenCevap, objMngCreateOrderIstek);
}
   public class MngGirisIstek
    {
        public string customerNumber { get; set; }
        public string password { get; set; }
        public int identityType { get; set; }
    }
    public class MngGirisCevap
    {
        public string jwt { get; set; }
        public string refreshToken { get; set; }
        public string jwtExpireDate { get; set; }
        public string refreshTokenExpireDate { get; set; }
    }

    public static string ApiIstekGonder<T>(string apiAdres, string metotAdi, string token, T paramNesne)
    {
        using (HttpClient client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0, 0, 360);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("x-ibm-client-secret", "MNG'den verilen secret key"); // Degisecek
            client.DefaultRequestHeaders.Add("x-ibm-client-id", "MNG'den verilen client ID"); // Degisecek
            if (!string.IsNullOrEmpty(token)) 
            {
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            }
            HttpContent content = new StringContent(JsonConvert.SerializeObject(paramNesne), Encoding.UTF8, "application/json");
            HttpResponseMessage message = client.PostAsync(apiAdres + metotAdi, content).Result;
            if (message.IsSuccessStatusCode)
            {
                if (string.IsNullOrEmpty(token))
                {
                    return JsonConvert.DeserializeObject<MngGirisCevap>(message.Content.ReadAsStringAsync().Result).jwt;
                }
                else
                {
                    return message.Content.ReadAsStringAsync().Result;
                }
            }
            else
            {
                Console.WriteLine("Hata var!!!! ( " + message.Content.ToString() + " ) ");
            }
            return "";
        }}
public class Order
{
    public string referenceId { get; set; }
    public string barcode { get; set; }
    public string billOfLandingId { get; set; }
    public int isCOD { get; set; }
    public int codAmount { get; set; }
    public int shipmentServiceType { get; set; }
    public int packagingType { get; set; }
    public string content { get; set; }
    public int smsPreference1 { get; set; }
    public int smsPreference2 { get; set; }
    public int smsPreference3 { get; set; }
    public int paymentType { get; set; }
    public int deliveryType { get; set; }
    public string description { get; set; }
    public string marketPlaceShortCode { get; set; }
    public string marketPlaceSaleCode { get; set; }
    public string pudoId { get; set; }
}

public class OrderPieceList
{
    public string barcode { get; set; }
    public int desi { get; set; }
    public int kg { get; set; }
    public string content { get; set; }
}

public class Recipient
{
    public string customerId { get; set; }
    public string refCustomerId { get; set; }
    public int cityCode { get; set; }
    public string cityName { get; set; }
    public string districtName { get; set; }
    public int districtCode { get; set; }
    public string address { get; set; }
    public string bussinessPhoneNumber { get; set; }
    public string email { get; set; }
    public string taxOffice { get; set; }
    public string taxNumber { get; set; }
    public string fullName { get; set; }
    public string homePhoneNumber { get; set; }
    public string mobilePhoneNumber { get; set; }
}

public class MngCreateOrderIstek
{
    public Order order { get; set; }
    public List<OrderPieceList> orderPieceList { get; set; }
    public Recipient recipient { get; set; }
}

Selamlar.

Leave a Reply

Your email address will not be published.