1

嗨,我在玩 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();
    }
}
}

我所有的运营商帐户

4

1 回答 1

3

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

  1. 您在 Shipment API 调用 ( )中明确列出所有承运人帐户。shipmentTable.Add("carrier_accounts", allCarriersAccounts);鉴于您希望查看所有不必要的运营商的费率,因为如果您不发送该carrier_accounts属性,Shippo 将自动使用我们所有的帐户。因此,我建议删除此行以消除与此行相关的任何潜在错误。
  2. 我不知道您在 Shippo 上激活了哪些其他运营商,但这些其他运营商可能不支持美国国内发货。例如 DHL Express 只支持国际货件,Canada Post 只支持来自加拿大的货件等。

希望有帮助!如果您有更多问题,很乐意提供进一步的帮助。

于 2016-08-24T02:56:20.243 回答