Here are the changed methods in USPSComputationMethod
1)
private string CreateRequest(string username, string password, ShipmentPackage shipmentPackage)
{
var usedMeasureWeight = MeasureManager.GetMeasureWeightBySystemKeyword(MEASUREWEIGHTSYSTEMKEYWORD);
if (usedMeasureWeight == null)
throw new NopException(string.Format("USPS shipping service. Could not load \"{0}\" measure weight", MEASUREWEIGHTSYSTEMKEYWORD));
var usedMeasureDimension = MeasureManager.GetMeasureDimensionBySystemKeyword(MEASUREDIMENSIONSYSTEMKEYWORD);
if (usedMeasureDimension == null)
throw new NopException(string.Format("USPS shipping service. Could not load \"{0}\" measure dimension", MEASUREDIMENSIONSYSTEMKEYWORD));
int length = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalLength(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
int height = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalHeight(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
int width = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalWidth(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
decimal weight = MeasureManager.ConvertWeight(ShippingManager.GetShoppingCartTotalWeigth(shipmentPackage.Items, shipmentPackage.Customer), MeasureManager.BaseWeightIn, usedMeasureWeight);
if (length < 1)
length = 1;
if (height < 1)
height = 1;
if (width < 1)
width = 1;
//if (weight < 1)
// weight = 1;
string zipPostalCodeFrom = shipmentPackage.ZipPostalCodeFrom;
string zipPostalCodeTo = shipmentPackage.ShippingAddress.ZipPostalCode;
//valid values for testing. http://testing.shippingapis.com/ShippingAPITest.dll
//Zip to = "20008"; Zip from ="10022"; weight = 2;
int pounds = Convert.ToInt32(weight);
//we don't use ounce
int ounces = Convert.ToInt32((weight - pounds) * 16.0M);
//int ounces = 0;
//if (pounds < 1)
// pounds = 1;
string requestString = string.Empty;
bool isDomestic = IsDomesticRequest(shipmentPackage);
if (isDomestic)
{
#region domestic request
var sb = new StringBuilder();
sb.AppendFormat("<RateV3Request USERID=\"{0}\" PASSWORD=\"{1}\">", username, password);
if ((!IsPackageTooHeavy(pounds, ounces)) && (!IsPackageTooLarge(length, height, width)))
{
var packageSize = GetPackageSize(length, height, width);
// RJH get all XML strings not commented out for USPSStrings.
// RJH V3 USPS Service must be Express, Express SH, Express Commercial, Express SH Commercial, First Class, Priority, Priority Commercial, Parcel, Library, BPM, Media, ALL or ONLINE;
sb.Append("<Package ID=\"0\">");
sb.Append("<Service>All</Service>");
sb.AppendFormat("<ZipOrigination>{0}</ZipOrigination>", zipPostalCodeFrom);
sb.AppendFormat("<ZipDestination>{0}</ZipDestination>", zipPostalCodeTo);
sb.AppendFormat("<Pounds>{0}</Pounds>", pounds);
sb.AppendFormat("<Ounces>{0}</Ounces>", ounces);
sb.AppendFormat("<Size>{0}</Size>", packageSize);
sb.Append("<Machinable>FALSE</Machinable>");
sb.Append("</Package>");
sb.Append("</RateV3Request>");
return "API=RateV3&XML=" + sb.ToString();
}
else
{
int totalPackages = 1;
int totalPackagesDims = 1;
int totalPackagesWeights = 1;
if (IsPackageTooHeavy(pounds, ounces))
{
decimal DecPounds = OuncesToPounds(PoundsToOunces(pounds) + ounces);
totalPackagesWeights = Convert.ToInt32(Math.Ceiling(DecPounds / (decimal)MAXPACKAGEWEIGHT));
}
if (IsPackageTooLarge(length, height, width))
{
totalPackagesDims = Convert.ToInt32(Math.Ceiling((decimal)TotalPackageSize(length, height, width) / (decimal)108));
}
totalPackages = totalPackagesDims > totalPackagesWeights ? totalPackagesDims : totalPackagesWeights;
if (totalPackages == 0)
totalPackages = 1;
int pounds2 = pounds / totalPackages;
//we don't use ounces
int ounces2 = ounces / totalPackages;
int height2 = height / totalPackages;
int width2 = width / totalPackages;
int length2 = length / totalPackages;
if (pounds2 < 1)
pounds2 = 1;
if (height2 < 1)
height2 = 1;
if (width2 < 1)
width2 = 1;
if (length2 < 1)
length2 = 1;
var packageSize = GetPackageSize(length2, height2, width2);
for (int i = 0; i < totalPackages; i++)
{
sb.AppendFormat("<Package ID=\"{0}\">", i.ToString());
// sb.AppendFormat("<Service>{0}</Service>", USPSService.All);
sb.AppendFormat("<Service>ALL</Service>");
sb.AppendFormat("<ZipOrigination>{0}</ZipOrigination>", zipPostalCodeFrom);
sb.AppendFormat("<ZipDestination>{0}</ZipDestination>", zipPostalCodeTo);
sb.AppendFormat("<Pounds>{0}</Pounds>", pounds2);
sb.AppendFormat("<Ounces>{0}</Ounces>", ounces2);
sb.AppendFormat("<Size>{0}</Size>", packageSize);
sb.Append("<Machinable>FALSE</Machinable>");
sb.Append("</Package>");
}
}
sb.Append("</RateV3Request>");
requestString = "API=RateV3&XML=" + sb.ToString();
#endregion
}
else
{
#region international request
var sb = new StringBuilder();
sb.AppendFormat("<IntlRateRequest USERID=\"{0}\" PASSWORD=\"{1}\">", username, password);
if ((!IsPackageTooHeavy(pounds)) && (!IsPackageTooLarge(length, height, width)))
{
string mailType = "Package"; //Package, Envelope
sb.Append("<Package ID=\"0\">");
// No use of pounds in this classes
sb.Append("<Pounds>0</Pounds>");
ounces = PoundsToOunces(pounds);
sb.AppendFormat("<Ounces>{0}</Ounces>", ounces);
sb.Append("<Machinable>FALSE</Machinable>");
sb.AppendFormat("<MailType>{0}</MailType>", mailType);
sb.Append("<GXG>");
sb.AppendFormat("<Length>{0}</Length>", length);
sb.AppendFormat("<Width>{0}</Width>", width);
sb.AppendFormat("<Height>{0}</Height>", height);
sb.Append("<POBoxFlag>N</POBoxFlag>");
sb.Append("<GiftFlag>N</GiftFlag>");
sb.Append("</GXG>");
sb.AppendFormat("<Country>{0}</Country>", shipmentPackage.ShippingAddress.Country.Name);
sb.Append("</Package>");
}
else
{
int totalPackages = 1;
int totalPackagesDims = 1;
int totalPackagesWeights = 1;
if (IsPackageTooHeavy(pounds))
{
totalPackagesWeights = Convert.ToInt32(Math.Ceiling((decimal)pounds / (decimal)MAXPACKAGEWEIGHT));
}
if (IsPackageTooLarge(length, height, width))
{
totalPackagesDims = Convert.ToInt32(Math.Ceiling((decimal)TotalPackageSize(length, height, width) / (decimal)108));
}
totalPackages = totalPackagesDims > totalPackagesWeights ? totalPackagesDims : totalPackagesWeights;
if (totalPackages == 0)
totalPackages = 1;
int pounds2 = pounds / totalPackages;
//we don't use ounces
int ounces2 = ounces / totalPackages;
int height2 = height / totalPackages;
int width2 = width / totalPackages;
int length2 = length / totalPackages;
if (pounds2 < 1)
pounds2 = 1;
if (height2 < 1)
height2 = 1;
if (width2 < 1)
width2 = 1;
if (length2 < 1)
length2 = 1;
for (int i = 0; i < totalPackages; i++)
{
string mailType = "Package"; //Package, Envelope
sb.AppendFormat("<Package ID=\"{0}\">", i.ToString());
// No use of pounds in this classes
sb.Append("<Pounds>0</Pounds>");
ounces2 = PoundsToOunces(pounds2);
sb.AppendFormat("<Ounces>{0}</Ounces>", ounces2);
sb.Append("<Machinable>FALSE</Machinable>");
sb.AppendFormat("<MailType>{0}</MailType>", mailType);
sb.Append("<GXG>");
sb.AppendFormat("<Length>{0}</Length>", length2);
sb.AppendFormat("<Width>{0}</Width>", width2);
sb.AppendFormat("<Height>{0}</Height>", height2);
sb.Append("<POBoxFlag>N</POBoxFlag>");
sb.Append("<GiftFlag>N</GiftFlag>");
sb.Append("</GXG>");
sb.AppendFormat("<Country>{0}</Country>", shipmentPackage.ShippingAddress.Country.Name);
sb.Append("</Package>");
}
}
sb.Append("</IntlRateRequest>");
requestString = "API=IntlRate&XML=" + sb.ToString();
#endregion
}
return requestString;
}