spree-contrib / spree_active_shipping

Spree integration for Shopify's active_shipping gem.
http://guides.spreecommerce.org
134 stars 206 forks source link

USPS updated service names again #103

Closed tristanpemble closed 11 years ago

tristanpemble commented 11 years ago

This is what I get now:

rterbush commented 11 years ago

Thanks for this heads up @tristanpemble

Completely forgot they were changing the API. It appears that the breakage goes beyond just the service names though. I cannot checkout on my store with any USPS shipping methods enabled, even after updating these names.

radar commented 11 years ago

Um, yay?

How much of the API was changed? Does shopify/active_shipping have an open issue on this yet?

APIs should never change. It frustrates me that this new API wasn't released as completely separate version. It's not like that is actually difficult to do.

mrpollo commented 11 years ago

it appears active_shipping has no issues related to the USPS API. I'm going to report this and work on adapting to the new API.

rterbush commented 11 years ago

After digging into this a bit tonight, it appears that USPS is changing the service_name based on an estimated delivery time. For example, the below rate_has returned in retreive_rates method for a local (same zipcode) mailing for me, returns 1-Day across the board.

{ "USPS Library Mail"=>369,
 "USPS Media Mail"=>388,
 "USPS Priority Mail 1-Day Flat Rate Envelope"=>560,
 "USPS Priority Mail 1-Day Window Flat Rate Envelope"=>560,
 "USPS Priority Mail 1-Day Gift Card Flat Rate Envelope"=>560,
 "USPS Priority Mail 1-Day Small Flat Rate Envelope"=>560,
 "USPS Priority Mail 1-Day Legal Flat Rate Envelope"=>575,
 "USPS Priority Mail 1-Day Small Flat Rate Box"=>580,
 "USPS Priority Mail 1-Day Padded Flat Rate Envelope"=>595,
 "USPS Standard Post"=>745,
 "USPS Priority Mail 1-Day"=>745,
 "USPS Priority Mail 1-Day Medium Flat Rate Box"=>1235,
 "USPS Priority Mail 1-Day Large Flat Rate Box"=>1685,
 "USPS Priority Mail Express 1-Day Padded Flat Rate Envelope Hold For Pickup"=>
  1995,
 "USPS Priority Mail Express 1-Day Padded Flat Rate Envelope"=>1995,
 "USPS Priority Mail Express 1-Day Legal Flat Rate Envelope Hold For Pickup"=>
  1995,
 "USPS Priority Mail Express 1-Day Legal Flat Rate Envelope"=>1995,
 "USPS Priority Mail Express 1-Day Flat Rate Envelope Hold For Pickup"=>1995,
 "USPS Priority Mail Express 1-Day Flat Rate Envelope"=>1995,
 "USPS Priority Mail Express 1-Day"=>2295,
 "USPS Priority Mail Express 1-Day Hold For Pickup"=>2295,
 "USPS Priority Mail Express 1-Day Flat Rate Boxes"=>3995,
 "USPS Priority Mail Express 1-Day Flat Rate Boxes Hold For Pickup"=>3995}

There is a service_code that is returned from the API service call which I think we may need to figure out how to use. Hopefully that may make these calcs a bit more robust.

@service_code="28",
    @service_name="USPS Priority Mail 1-Day Small Flat Rate Box",
    @total_price=nil>,
   #<ActiveMerchant::Shipping::RateEstimate:0x0000000a9e4c60
    @carrier="USPS",
    @currency="USD",
    @delivery_date=nil,
    @delivery_range=[],
    @destination=EVERGREEN, CO, 804394337
United States,
    @origin=Evergreen, CO, 80439
United States,
    @package_rates=
     [{:package=>
        #<ActiveMerchant::Shipping::Package:0x0000000718ec80
         @currency=nil,
         @cylinder=false,
         @dimensions=
          [#<Quantified::Length: 0 inches>,
           #<Quantified::Length: 0 inches>,
           #<Quantified::Length: 0 inches>],
         @gift=false,
         @inches=[0, 0, 0],
         @options={:units=>:imperial},
         @unit_system=:imperial,
         @value=nil,
         @weight=#<Quantified::Mass: 64 ounces>>,
       :rate=>595}],

I'm not clear where we would map the service_codes just yet.

Enough for me today. Will check in tomorrow to see what the solution was. :-)

rterbush commented 11 years ago

Actually, code is pretty simple to generate a rate_hash of service_codes and rates. Need some help figure out how to translate the codes to shipping_methods.

{"7"=>369,
 "6"=>388,
 "16"=>560,
 "40"=>560,
 "38"=>560,
 "42"=>560,
 "44"=>575,
 "28"=>580,
 "29"=>595,
 "4"=>745,
 "1"=>745,
 "17"=>1235,
 "22"=>1685,
 "63"=>1995,
 "62"=>1995,
 "31"=>1995,
 "30"=>1995,
 "27"=>1995,
 "13"=>1995,
 "3"=>2295,
 "2"=>2295,
 "55"=>3995,
 "56"=>3995}
def retrieve_rates(origin, destination, packages)
          begin
            response = carrier.find_rates(origin, destination, packages)
            # turn this beastly array into a nice little hash
            rates = response.rates.collect do |rate|
              service_code = rate.service_code
              [service_code, rate.price]
            end
            rate_hash = Hash[*rates.flatten]
tristanpemble commented 11 years ago

Changing the service names in the translation files helped for express mail, but I'm still unable to get ordinary priority mail to work through that method

mrpollo commented 11 years ago

Here is a list of the new MailService response values ( from the official update document on USPS )

Appendix A – RateV3 and RateV4 Class ID and MailService response values

Notes:

Class ID - MailService 0 - First-Class Mail® Large Envelope 0 - First-Class Mail® Letter 0 - First-Class Mail® Parcel 0 - First-Class Mail® Postcards 1 - Priority Mail {0} 2 - Priority Mail Express {0} Hold For Pickup 3 - Priority Mail Express {0} 4 - Standard Post® 6 - Media Mail® 7 - Library Mail 13 - Priority Mail Express {0} Flat Rate Envelope 15 - First-Class Mail® Large Postcards 16 - Priority Mail {0} Flat Rate Envelope 17 - Priority Mail {0} Medium Flat Rate Box 22 - Priority Mail {0} Large Flat Rate Box 23 - Priority Mail Express {0} Sunday/Holiday Delivery 25 - Priority Mail Express {0} Sunday/Holiday Delivery Flat Rate Envelope 27 - Priority Mail Express {0} Flat Rate Envelope Hold For Pickup 28 - Priority Mail {0} Small Flat Rate Box 29 - Priority Mail {0} Padded Flat Rate Envelope 30 - Priority Mail Express {0} Legal Flat Rate Envelope 31 - Priority Mail Express {0} Legal Flat Rate Envelope Hold For Pickup 32 - Priority Mail Express {0} Sunday/Holiday Delivery Legal Flat Rate Envelope 33 - Priority Mail {0} Hold For Pickup 34 - Priority Mail {0} Large Flat Rate Box Hold For Pickup 35 - Priority Mail {0} Medium Flat Rate Box Hold For Pickup 36 - Priority Mail {0} Small Flat Rate Box Hold For Pickup 37 - Priority Mail {0} Flat Rate Envelope Hold For Pickup 38 - Priority Mail {0} Gift Card Flat Rate Envelope 39 - Priority Mail {0} Gift Card Flat Rate Envelope Hold For Pickup 40 - Priority Mail {0} Window Flat Rate Envelope 41 - Priority Mail {0} Window Flat Rate Envelope Hold For Pickup 42 - Priority Mail {0} Small Flat Rate Envelope 43 - Priority Mail {0} Small Flat Rate Envelope Hold For Pickup 44 - Priority Mail {0} Legal Flat Rate Envelope 45 - Priority Mail {0} Legal Flat Rate Envelope Hold For Pickup 46 - Priority Mail {0} Padded Flat Rate Envelope Hold For Pickup 47 - Priority Mail {0} Regional Rate Box A 48 - Priority Mail {0} Regional Rate Box A Hold For Pickup 49 - Priority Mail {0} Regional Rate Box B 50 - Priority Mail {0} Regional Rate Box B Hold For Pickup 53 - First-Class Package Service Hold For Pickup 55 - Priority Mail Express {0} Flat Rate Boxes 56 - Priority Mail Express {0} Flat Rate Boxes Hold For Pickup 57 - Priority Mail Express {0} Sunday/Holiday Delivery Flat Rate Boxes 58 - Priority Mail {0} Regional Rate Box C 59 - Priority Mail {0} Regional Rate Box C Hold For Pickup 61 - First-Class Package Service 62 - Priority Mail Express {0} Padded Flat Rate Envelope 63 - Priority Mail Express {0} Padded Flat Rate Envelope Hold For Pickup 64 - Priority Mail Express {0} Sunday/Holiday Delivery Padded Flat Rate Envelope

Appendix B – IntlRate and IntlRateV2 Service ID and SvcDescription response values

Service ID - SvcDescription 1 - Priority Mail Express International 2 - Priority Mail International® 4 - Global Express Guaranteed® (GXG) 5 - Global Express Guaranteed® Document 6 - Global Express Guaranteed® Non-Document Rectangular 7 - Global Express Guaranteed® Non-Document Non-Rectangular 8 - Priority Mail International® Flat Rate Envelope 9 - Priority Mail International® Medium Flat Rate Box 10 - Priority Mail Express International Flat Rate Envelope 11 - Priority Mail International® Large Flat Rate Box 12 - USPS GXG Envelopes 13 - First-Class Mail® International Letter 14 - First-Class Mail® International Large Envelope 15 - First-Class Package International Service 16 - Priority Mail International® Small Flat Rate Box 17 - Priority Mail Express International Legal Flat Rate Envelope 18 - Priority Mail International® Gift Card Flat Rate Envelope 19 - Priority Mail International® Window Flat Rate Envelope 20 - Priority Mail International® Small Flat Rate Envelope 21 - First-Class Mail® International Postcard 22 - Priority Mail International® Legal Flat Rate Envelope 23 - Priority Mail International® Padded Flat Rate Envelope 24 - Priority Mail International® DVD Flat Rate priced box 25 - Priority Mail International® Large Video Flat Rate priced box 26 - Priority Mail Express International Flat Rate Boxes 27 - Priority Mail Express International Padded Flat Rate Envelope

rterbush commented 11 years ago

Nice work @mrpollo

So we are going to need to revert to using service codes since the strings are dynamic. Seems that would be a real improvement generally so we are not subject to these service names changing. It actually would be a nice improvement if we could display the dynamic part of this string into the shipping method options for the customer since often choosing a more expensive service does not mean that it gets there any quicker.

Is it possible that we can just prepend these service codes to the description and do the match on those first 2 chars?. I've not yet found the code that is doing that match.

GeekOnCoffee commented 11 years ago

@maxstudener I believe this is the issue you were seeing

rterbush commented 11 years ago

I have a working solution to this. Not sure it is the most elegant approach, but it does seem to work. I'll push the PR shortly to get some feedback. Essentially, for description method in all calculators, I am prefixing that string with #:

In the active_shipping/base.rb I am matching on the results with:

          rate = rates_result[self.class.description.gsub(/:.*$/, '')]

If someone has concerns or a better approach, please speak up.

machty commented 11 years ago

I was able to restore Priority and Express with @mrpollo's PR, but seem reason First Class still seems to be missing as an option... has anyone else had this problem? Is there something I need to do to support First Class again?

rterbush commented 11 years ago

@machty Domestic First Class Parcel is working for me. Be sure you have deleted tmp/cache in case you are picking up an old order.

I've moved this to production and it seems to be working as expected.

machty commented 11 years ago

@rterbush thanks for the advice; it turned out to be a red herring.. was trying to ship items over the 13 ounce first class limit.

jumph4x commented 11 years ago

Thank you for this work! Sent it to 1-2-stable as well.