shippo api没有退回活跃运营商的所有费率

时间:2016-08-23 16:57:27

标签: c# shippo

嗨我正在玩shippo api。我有5个默认运营商都处于活动状态且处于测试模式。问题是当我创建一个货物并检查所有费率时,我只是获得USPS费率,我更喜欢所有运营商的费率并比较成本。有任何想法吗 ?此致

确定有那些要求它的代码

using System;
using Shippo;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Linq;


namespace ShippoApiTest
{
class Program
{
    static void Main(string[] args)
    {
        string shippoPrivateAuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

        try
        {
            APIResource resource = new APIResource(shippoPrivateAuthToken);

            Hashtable carriers = new Hashtable();
            carriers.Add("results", "20");
            var carriersAccounts = resource.AllCarrierAccount(carriers);

            foreach (var item in carriersAccounts.Data)
                Console.WriteLine(item.Carrier + " ----> " + item.AccountId + " -----> " + item.Active);

            // to address 
            Hashtable toAddressTable = new Hashtable();
            toAddressTable.Add("object_purpose", "PURCHASE");
            toAddressTable.Add("name", "Mr. Hippo");
            toAddressTable.Add("company", "Shippo");
            toAddressTable.Add("street1", "955 Benecia Avenue");
            toAddressTable.Add("city", "Sunnyvale");
            toAddressTable.Add("state", "CA");
            toAddressTable.Add("zip", "94085");
            toAddressTable.Add("country", "US");
            toAddressTable.Add("phone", "555 341 9393");
            toAddressTable.Add("email", "support@goshipppo.com");

            // from address 
            Hashtable fromAddressTable = new Hashtable();
            fromAddressTable.Add("object_purpose", "PURCHASE");
            fromAddressTable.Add("name", "Ms Hippo");
            fromAddressTable.Add("company", "San Diego Zoo");
            fromAddressTable.Add("street1", "503 Pine Street");
            fromAddressTable.Add("city", "San Diego");
            fromAddressTable.Add("state", "GA");
            fromAddressTable.Add("zip", "31312");
            fromAddressTable.Add("country", "US");
            fromAddressTable.Add("email", "hippo@goshipppo.com");
            fromAddressTable.Add("phone", "619 231 1515");
            fromAddressTable.Add("metadata", "Customer ID 123456");


            // parcel 
            Hashtable parcelTable = new Hashtable();
            parcelTable.Add("length", "5");
            parcelTable.Add("width", "5");
            parcelTable.Add("height", "5");
            parcelTable.Add("distance_unit", "in");
            parcelTable.Add("weight", "2");
            parcelTable.Add("mass_unit", "lb");

            // shipment 
            Hashtable shipmentTable = new Hashtable();
            shipmentTable.Add("address_to", toAddressTable);
            shipmentTable.Add("address_from", fromAddressTable);
            shipmentTable.Add("parcel", parcelTable);
            shipmentTable.Add("object_purpose", "PURCHASE");
            shipmentTable.Add("async", false);

            var allCarriersAccounts = carriersAccounts.Data.Select(c => c.ObjectId).ToArray();

            shipmentTable.Add("carrier_accounts", allCarriersAccounts);

            // create Shipment object 
            Console.WriteLine("Creating Shipment object..");
            Shipment shipment = resource.CreateShipment(shipmentTable);

            // select desired shipping rate according to your business logic 
            // we simply select the first rate in this example              
            //Rate rate = shipment.RatesList[0];
            //var rates = resource.AllRates(shipmentTable);

            foreach (var rate in shipment.RatesList)
            {
                Console.WriteLine((string)rate.Amount);
                Console.WriteLine((string)rate.AmountLocal);
                Console.WriteLine((string)rate.Currency);
                Console.WriteLine((string)rate.CurrencyLocal);
                Console.WriteLine((string)rate.Provider);
                Console.WriteLine((bool)rate.Trackable);
                Console.WriteLine((long)rate.Days);
                Console.WriteLine((string)rate.DurationTerms);
                Console.WriteLine((bool)rate.Insurance);

                Console.WriteLine((bool)rate.Insurance);
                Console.WriteLine((string)rate.ServicelevelName);
                Console.WriteLine((string)rate.ServicelevelTerms);

                Console.WriteLine("Attributes: ");
                foreach (var attr in new JArray(rate.Attributes))
                    foreach (var token in attr.Children())
                        Console.Write(token);

                Console.WriteLine("\n\n\n");
            }

            var currentRate = shipment.RatesList[0];

            Console.WriteLine("Getting shipping label..");
            Hashtable transactionParameters = new Hashtable();
            transactionParameters.Add("rate", currentRate.ObjectId);
            transactionParameters.Add("async", false);
            Transaction transaction = resource.CreateTransaction(transactionParameters);

            if (((String)transaction.ObjectStatus).Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Label url : " + transaction.LabelURL);
                Console.WriteLine("Tracking number : " + transaction.TrackingNumber);
            }
            else
            {
                Console.WriteLine("An Error has occured while generating your label. Messages : " + transaction.Messages);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }

        Console.ReadLine();
    }
}
}

All my carriers accounts

1 个答案:

答案 0 :(得分:3)

可能有不同的原因导致您没有看到其他费率。首先想到的是:

  1. 您在Shipment API调用(shipmentTable.Add("carrier_accounts", allCarriersAccounts);)中明确列出所有运营商帐户。鉴于您希望查看所有不必要的运营商的费率,因为如果您不发送carrier_accounts属性,Shippo将自动使用我们的所有帐户。因此,我建议删除此行以消除与此行相关的任何潜在错误。
  2. 我不知道您在Shippo上激活了哪些其他运营商,但这些其他运营商可能不支持美国国内运输。例如。 DHL Express仅支持国际货件,加拿大仅邮寄来自加拿大的货物等。
  3. 希望有所帮助!如果您有更多问题,很乐意提供进一步的帮助。