karrioapi / karrio

Programmable Shipping API (self-hosted)
https://karrio.io
Apache License 2.0
457 stars 90 forks source link

Properly support USPS Commercial Base and Commercial Plus #258

Open nahall opened 1 year ago

nahall commented 1 year ago

USPS supports Commercial Base and Commercial Plus rates for both domestic and international. Right now the Karrio SDK tries to determine if you are requesting Commercial Base or Commercial Plus rates by looking at the service code, but that doesn't really work very well, especially for International, as you are just trying to request that USPS returns ALL the rates, so you don't want to have to manipulate the service types just to get it to set the CommercialPlusFlag in the USPS request.

Since Commercial Base or Commercial Plus rates are something that requires approval from USPS, it seems much better to just make a carrier flag (for USPS and USPS International) indicating that you have Commercial Base or Commercial Plus rates when you set up the carrier gateway. Then the SDK would just have to look at that flag to determine which rates to request rather than having to parse the service type to try to figure it out, and it would be easy to just request all the rates from USPS and get back the proper pricing.

nahall commented 1 year ago

I was just checking this and, unless I'm missing something, I don't think this was actually implemented. The problem is, when you rate with USPS International it returns a bunch of different services and rates but the USPS API needs the commercial flag passed when the request is made. So the USPS API wants it to look like:

<IntlRateV2Request USERID="xxxxxxxxxx">
     <Revision>2</Revision>
     <Package ID="0">
         <Pounds>0</Pounds>
         <Ounces>29.6</Ounces>
         <MailType>PACKAGE</MailType>
         <ValueOfContents></ValueOfContents>
         <Country>Taiwan</Country>
         <Width>8</Width>
         <Length>12</Length>
         <Height>3</Height>
         <OriginZip>90210</OriginZip>
         <CommercialFlag>Y</CommercialFlag>
         <CommercialPlusFlag>Y</CommercialPlusFlag>
         <AcceptanceDateTime>2023-07-12T21:04:49.574160+00:00</AcceptanceDateTime>
         <DestinationPostalCode>48126</DestinationPostalCode>
     </Package>
</IntlRateV2Request>

The way sdk/extensions/usps/karrio/providers/usps/rate.py is written currently, it is checking the "services" string name to see if the word "commercial" is in it. That isn't ideal because we don't really know what services USPS will provide back to us so it's not ideal to have to pass a bunch of different services in. It would be easier to just have a flag when the usps_international gateway is configured, alongside the username and password, to indicate if you have access to commercial rates.

danh91 commented 1 year ago

Hi @nahall,

I see this was closed when I merged your PR. But I don't think it was addressed by the PR 🤔. Is that correct? Let me know and I will reopen.

nahall commented 1 year ago

Yes, that is correct, the PR I had made in November did not deal with this issue. Thanks.