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