Zen Cart Logo
Forums / Addon Shipping Modules / USPS Shipping (RESTful) (USPSr)

USPS Shipping (RESTful) (USPSr)

Sticky

Views: 54,870

Results 1 to 20 of 558
21 Dec 2024, 5:44 AM
#1
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

USPS Shipping (RESTful) (USPSr)

USPS Shipping (RESTful) for Zen Cart

This module provides sellers a chance to offer United States Postal Service (USPS) shipping rates to customers during checkout. This is done by pulling the rates directly from the USPS' REST API.

This module supports versions 1.5.8 onward innately. (Support from 1.5.7 and backward is not necessarily guaranteed but is plausible. Read the Installation steps below for more details.) This script was primarily written with PHP8 in mind. (It might have problems working with PHP7.)

There isn't much difference between the two versions. However, if you managed to get the WebTools version to work, CONTINUE TO USE THAT VERSION.

USPS API Documentation: https://developers.usps.com/apis
*This API takes advantage of four API's: Domestic Prices 3.0, International Prices 3.0, Service Standards 3.0, and OAuth 3.0.
*

ZenCart Plugins Download: https://www.zen-cart.com/downloads.php?do=file&id=2395 (Pending) (or use the Releases function on the GitHub repository)

GitHub Repository: https://github.com/retched/ZC-USPSRestful

*** This thread is only for the USPSr version of the USPS module. For assistance with the original USPS module which uses the WebTools API, you should post in its megathread: https://www.zen-cart.com/showthread.php?227284-USPS-Shipping-Module-Support-Thread*

14 Jan 2025, 3:19 AM
#2
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
971
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

i tried to install in zen210 through plugin module and it error on shipping modules say if you were adding data press the back button and shipping modules stop showing after last 1. same thing happens with the web tools version also.
no error log is produced.

14 Jan 2025, 5:26 AM
#3
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

jimmie:

i tried to install in zen210 through plugin module and it error on shipping modules say if you were adding data press the back button and shipping modules stop showing after last 1. same thing happens with the web tools version also.
no error log is produced.

This sounds like a cookie issue. What do you mean by "error on shipping modules"? Also be sure that if you have the problem on WebTools, you make a post on the WebTools version as well.

14 Jan 2025, 11:58 AM
#4
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
971
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

retched:

This sounds like a cookie issue. What do you mean by "error on shipping modules"? Also be sure that if you have the problem on WebTools, you make a post on the WebTools version as well.

when i go to shipping modules message at the top says to refresh page
when looking at modules all the other shipping modules configuration settings are missing

14 Jan 2025, 1:33 PM
#5
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

Are you sure there are no error logs generated? What other modules/add ons do you have? What browser add-ons do you have?

15 Jan 2025, 3:47 AM
#6
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

Found the problem:

[ALTER TABLE orders MODIFY shipping_method varchar(255) NOT NULL DEFAULT '']

This happens when the module is enabled seems something changed it to TEXT and both my module and the WebTools module tries to revert it back to VARCHAR(255). I'm going to modify my module to check to see if the column is either 255 or TEXT.

17 Jan 2025, 7:41 PM
#7
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

**Incoming Changes for Version 0.2.0

Added**

  • Module will now disable itself if it sees that SHIPPING_ORIGIN_ZIP is not a valid 5-digit or 9-digit ZIP Code.
  • Additional warnings are now present on the module line in the listing of modules.
  • Added a note to the Debug Log about what the shipping origin zip code setting is.

Changed

  • Changed the module's installation message with regards to the measurements. (Going forward, if the store owner changes the setting for SHIPPING_WEIGHT_UNITS at the time of installation, the message for the default measurements will also change.)
  • Changed the value of "Estimate Time" to "Estimate Transit Time" for the MODULE_SHIPPING_USPSR_DISPLAY_TRANSIT constant for clarity. During the upgrade, if "Estimate Time" is the option, it will change to "Estimate Transit Time" automatically.
  • Changed the encapsulated plugin's "description" to read a bit better. (This has no function on the operation of the plugin but it was irritating to look at as it referenced parts not available in the regular parts of the module.)

Fixed

  • [BUG] Resolved issue [#4]: (Loading the file via Plugin Manager generates a crash). This was resolved by an emergency patch to the release files but is being properly deployed now. (This patch has been undone as the module is now active on the ZenCart Plugins Database.)
  • [BUG] Resolved issue [#5]: Stops error message complaining about a NULL value for $order->delivery['street_address'] when searching if a delivery address is either a PO_BOX or STREET address. This normally happens when you try to use the module on something like the Shipping Estimator where there is no address set.
  • [BUG] Resolved issue [#7]: First Class Mail International Service missing from quotes. This was apparently repaired by the USPS API Tech Team. The quote is now reinstated alongside other international quotes. Additionally, both quotes now will carry the estimated value of the cart alongside it. This is important as there are no limits on domestic shipments but there is a limit of roughly $400 for First Class Mail Package Service and a limit of about $600 to as much $5000 for other services (depending on the country). If the request is too much, the quote will not return anything as it is outside of the range. Additionally, international insurance is now properly applied where necessary.
  • [BUG] Resolved issue [#8]: Shipping Modules listing crashes when module loads. This happens when the TABLE_ORDERS has a shipping_method column set to TEXT and is filled with larger data. Originally the module, during the _check() process, would see if the column was set to be a VARCHAR(255). If it was set to anything but a VARCHAR(255), the module would try to issue an ALTER TABLE SQL command and force it to be a VARCHAR(255). This would backfire as ZenCart would catch a MYSQL error on the tune of "Data too big" and just crash the entire backend. This change will now ignore the column if it's set to anything bigger than a VARCHAR (namely TEXT, MEDIUMTEXT or BLOB).
  • [BUG] Fixed double spacing present on all quoted process names. (These extra spaces weren't visible anywhere else other than the logfile and maybe the raw source code too if you looked hard enough for it.)

Removed

  • Dropped International Return Receipt as that service has been deprecated since January 19, 2025.
  • Deleted the changelog attached to the release directory and removed the reference from the encapsulated manifest file. Going forward, the manifest should link to the releases tab on the repository at GitHub. The 0.1.0 release manifest also has been changed to match.

===
Going forward, in addition to posting it on the Repository, an encapsulated and non-encapsulated version of the module will be posted to the directory.

11 Feb 2025, 3:40 PM
#8
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

I am trying to install the current version of the USPS shipping module into a new 2.1.0 application.

The README in the plugin download states it is installed using the new Encapsulated Plugin Manager.

How do you add the USPS plugin and install it in the Plugin Manager?

12 Feb 2025, 5:21 AM
#9
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

jodean:

I am trying to install the current version of the USPS shipping module into a new 2.1.0 application.

The README in the plugin download states it is installed using the new Encapsulated Plugin Manager.

How do you add the USPS plugin and install it in the Plugin Manager?

Hi, sorry the language wasn't clear.

When you download the module, either from the ZenCart Plugin Database or off the repository, you'll be given two choices: download the Encapsulated OR download the non-Encapsulated. Download the encapsulated version and there will be a Zip Folder that contains a folder named 'zc_plugin'. Extract the contents of that directory into the matching zc_plugin directory on your ZenCart installation. Visit your ZenCart Admin area and then visit Modules > Plugin Manager and there should be a new option named "USPS Shipping (w/REST API) for Zen Cart". Click on that row, then choose install. And if all worked well, you will see that it has been installed as well as a new option available under Modules > Shipping.

12 Feb 2025, 2:10 PM
#10
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

retched:

Hi, sorry the language wasn't clear.When you download the module, either from the ZenCart Plugin Database or off the repository, you'll be given two choices: download the Encapsulated OR download the non-Encapsulated. Download the encapsulated version and there will be a Zip Folder that contains a folder named 'zc_plugin'. Extract the contents of that directory into the matching zc_plugin directory on your ZenCart installation. Visit your ZenCart Admin area and then visit Modules > Plugin Manager and there should be a new option named "USPS Shipping (w/REST API) for Zen Cart". Click on that row, then choose install. And if all worked well, you will see that it has been installed as well as a new option available under Modules > Shipping.

I just tried to download USPS again from the Zencart Plugins and it does not prompt for to download Encapsulated or non-Encapsulated.

12 Feb 2025, 3:50 PM
#11
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

I was able to download a Encapsualted version from Gethub.

12 Feb 2025, 4:11 PM
#12
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

I used the same setup that my 2.0.1 version uses successfully and I get an error in the new USPSr 0.2.1 version.

Error: Missing or malformed access token.

Is the error referring to the USPS API Consumer Key?

I am using the same USPS API Consumer Key in both Zencart versions.

​[12-Feb-2025 16:38:27 Europe/Berlin] Request URI: /tennesseetraditionalflies.com/index.php?main_page=checkout_shipping, IP address: ::1, Language id 1#0 C:\xampp\htdocs\tennesseetraditionalflies.com\zc_plugins\USPSRestful\v0.2.1\catalog\includes\modules\shipping\uspsr.php(1706): zen_debug_error_handler()
#1 C:\xampp\htdocs\tennesseetraditionalflies.com\zc_plugins\USPSRestful\v0.2.1\catalog\includes\modules\shipping\uspsr.php(376): uspsr->getBearerToken()
#2 C:\xampp\htdocs\tennesseetraditionalflies.com\includes\classes\shipping.php(257): uspsr->quote()
#3 C:\xampp\htdocs\tennesseetraditionalflies.com\includes\modules\pages\checkout_shipping\header_php.php(185): shipping->quote()
#4 C:\xampp\htdocs\tennesseetraditionalflies.com\index.php(35): require('C:\xampp\htdocs...')
--> PHP Warning: Undefined array key "access_token" in C:\xampp\htdocs\tennesseetraditionalflies.com\zc_plugins\USPSRestful\v0.2.1\catalog\includes\modules\shipping\uspsr.php on line 1706.

12 Feb 2025, 5:59 PM
#13
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

Hi. You're using one of the unstable versions. 0.2.1 isn't released yet. Can you try to download one of the releases? 0.2.0 is the current version. Meanwhile I'll look into why that error is coming up at all.

12 Feb 2025, 6:07 PM
#14
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

Additionally, did you also set up your module by providing it a API Key and Secret in the ZenCart admin panel?

That line

        $token = $body['access_token'];

is asking for the access_token from the response.

If possible, can you enable debugging and send me the log file that's generated from the USPSr module?

12 Feb 2025, 7:03 PM
#15
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

retched:

Additionally, did you also set up your module by providing it a API Key and Secret in the ZenCart admin panel?

That line

    $token = $body['access_token'];
> 
> is asking for the access_token from the response.
> 
> If possible, can you enable debugging and send me the log file that's generated from the USPSr module?

Yes, I used the same setup from my Zencart 2.0.1

Below is the debug.

2025-02-12 19:38:53:
Requesting Token from USPS
Token Request
{
"grant_type": "client_credentials",
"client_id": "725TENNE5719",
"client_secret": "lgf@USPS9491",
"scope": "domestic-prices addresses international-prices service-standards shipments"
}


2025-02-12 19:38:53:
==================================


TOKEN RESPONSE FROM USPS:


{
"error": "invalid_client",
"error_description": "InvalidApiKey: The client application credentials provided in the request are missing, invalid, inactive or not approved for access.",
"error_uri": "https://datatracker.ietf.org/doc/html/rfc6749#page-45"
}




---------------------------------
CommErr (should be 0): 0 -


==================================


USPS Country - $order->delivery[country][iso_code_2]: US


2025-02-12 19:38:53:


USPSRestful Configuration Report
=========================================================
USPSr build: 0.2.1


Quote Request Rate Type: Retail
Quote from main_page: checkout_shipping
Display Transit Time: No
Site Weights Based in: lbs (Pounds)
Site Measurements Based in: Inches
Shipping ZIP Code Origin: 37135


Cart Weight: 0.01 lbs
Total Quote Weight: 0.0165 lbs. , Pounds: 0, Ounces: 0.264
Maximum: 50 Tare Rates: Small/Medium: 65:0 Large: 0:2
Order Handling method: Box, Handling fee Domestic (Order): $1.25, Handling fee International (Order): $5.00


Services Selected:
Domestic Methods: Ground Advantage, Priority
International Methods: - None -


Categories Excluded from Media Mail: - None -
Zip Codes Allowed for USPS Connect : - None -
Add-Ons Enabled (Domestic): - None -
Add-Ons Enabled (International): - None -


Assumed Domestic Package Size - Length: 8.625, Width: 5.375, Height: 1.625
Assumed International Package Size - Length: 8.625, Width: 5.375, Height: 1.625
Are Packages are Machinable? : Irregular
Sort the returned quotes by: Price-LowToHigh


Order is eligible for Media Mail ? YES
Order SubTotal: $3.00
Order Total: $3.29
Uninsurable Portion: $0.00
Insurable Value: $3.29


2025-02-12 19:38:53: Sending Services request to USPS
2025-02-12 19:38:54:
==================================


REQUEST FROM STORE:


{
"originZIPCode": "37135",
"destinationZIPCode": "37135",
"weight": 0.0165,
"length": 8.625,
"width": 5.375,
"height": 1.625,
"mailClasses": [
"USPS_GROUND_ADVANTAGE",
"PRIORITY_MAIL",
"PRIORITY_MAIL_EXPRESS",
"MEDIA_MAIL"
],
"priceType": "RETAIL",
"extraServices": [
],
"itemValue": 3.2924999999999995,
"mailingDate": "2025-02-13"
}




---------------------------------
CommErr (should be 0): 0 -


==================================


USPS Country - $order->delivery[country][iso_code_2]: US


2025-02-12 19:38:54:
==================================
RAW JSON FROM USPS:


{
"apiVersion": "/prices/v3",
"error": {
"code": "401",
"message": "Missing or malformed access token.",
"errors": [
{
"title": "invalid_token",
"detail": "The access token presented with the request is missing or malformed (not a JWT).",
"source": "Access Token"
}
]
}
}




2025-02-12 19:38:54:
===============================================
Reviewing selected method options...
Array
(
[0] => Array
(
[method] => USPS Ground Advantage Machinable Single-piece
[handling] => 1.30
)


[1] => Array
(
[method] => Priority Mail Machinable Single-piece
[handling] => 1.30
)


)


2025-02-12 19:38:54:
===============================================
Building options...


2025-02-12 19:38:54:
===============================================
Revoking Bearer Token...


2025-02-12 19:38:54: Revoking a Token from USPS
Revocation Request
{
"client_id": "725TENNE5719",
"client_secret": "lgf@USPS9491",
"revoke_token": null,
"token_type": "access_token"
}


2025-02-12 19:38:54:
==================================


TOKEN RESPONSE FROM USPS:


{
"error": "invalid_client",
"error_description": "InvalidApiKey: The client application credentials provided in the request are missing, invalid, inactive or not approved for access.",
"error_uri": "https://datatracker.ietf.org/doc/html/rfc6749#page-45"
}




---------------------------------
CommErr (should be 0): 0 -


==================================


USPS Country - $order->delivery[country][iso_code_2]: US
12 Feb 2025, 7:10 PM
#16
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

jodean:

"client_id": "725TENNE5719",
"client_secret": "lgf@USPS9491",

Yup, like I thought. You're using the old WebTools API login details. As stated on the readme:

I already have a USERID and PASSWORD under the old system, but I'm getting error messages while I try to retrieve quotes.
The older USERID and PASSWORD are not valid for the new system. You will need to provision new credentials under the new USPS API system. Additionally, you SHOULD create an entire new USPS Business Account provided that you don't already have one for your business.

You may want to head over to this page and read how to obtain valid API Credentials: https://github.com/retched/ZC-USPSRestful/wiki/Getting-Started#installing

Let me know if you need help. Send me a DM or @retched on Discord.

12 Feb 2025, 7:56 PM
#17
jodean avatar

jodean

Totally Zenned

Join Date:
May 2011
Location:
Tennessee
Posts:
511
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

retched:

Yup, like I thought. You're using the old WebTools API login details. As stated on the readme:

I already have a USERID and PASSWORD under the old system, but I'm getting error messages while I try to retrieve quotes.
The older USERID and PASSWORD are not valid for the new system. You will need to provision new credentials under the new USPS API system. Additionally, you SHOULD create an entire new USPS Business Account provided that you don't already have one for your business.

You may want to head over to this page and read how to obtain valid API Credentials: https://github.com/retched/ZC-USPSRestful/wiki/Getting-Started#installing

Let me know if you need help. Send me a DM or @retched on Discord.

You were on target. I missed the instructions to set up a new USPS account. Now it works

Thanks

12 Feb 2025, 8:02 PM
#18
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: USPS Shipping (RESTful) (USPSr)

jodean:

You were on target. I missed the instructions to set up a new USPS account. Now it works

Thanks

No problem. There will be an update coming out soon(?) that will disable the module if it can't grab a key. Writing up now.

14 Feb 2025, 7:45 PM
#19
spyderrobotics avatar

spyderrobotics

New Zenner

Join Date:
Oct 2007
Posts:
75
Plugin Contributions:
0

Re: USPS Shipping (RESTful) (USPSr)

Using 1.5.7c and installed the non-encapsulated version. Couple of issues. In the admin panel the module didn't show and it gave an error because it was looking for uspsr.php in the languages>english>modules>shipping folder. The file is actually named lang.uspsr.php. So I just renamed it to uspsr.php and then the module showed up and I was able to configure it. The module works but it also is not grabbing some of the names correctly. For instance when you get a quote it would show MODULE_SHIPPING_USPSR_TEXT_SHORT_TITLE (Ground Advantage) to the user.

I edited line 245 in the module and just changed it to $this->title = 'United States Postal Service';

It does something similar with the name of the module under the admin>module>shipping.

I was able to edit those to get things looking right. Not sure why its not getting the data from the other files. My issue is with the other usps module you could select the weight for each shipping type. For instance I only want up to 1 lb for ground advantage and priority limited to 1 lb or more. Is there a way to put that option back in or any idea where to edit the files to manually put that back in?

14 Feb 2025, 8:37 PM
#20
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: USPS Shipping (RESTful) (USPSr)

spyderrobotics:

Using 1.5.7c and installed the non-encapsulated version. Couple of issues. In the admin panel the module didn't show and it gave an error because it was looking for uspsr.php in the languages>english>modules>shipping folder. The file is actually named lang.uspsr.php. So I just renamed it to uspsr.php and then the module showed up and I was able to configure it. The module works but it also is not grabbing some of the names correctly. For instance when you get a quote it would show MODULE_SHIPPING_USPSR_TEXT_SHORT_TITLE (Ground Advantage) to the user.

I edited line 245 in the module and just changed it to $this->title = 'United States Postal Service';

It does something similar with the name of the module under the admin>module>shipping.

I was able to edit those to get things looking right. Not sure why its not getting the data from the other files. My issue is with the other usps module you could select the weight for each shipping type. For instance I only want up to 1 lb for ground advantage and priority limited to 1 lb or more. Is there a way to put that option back in or any idea where to edit the files to manually put that back in?
Right, the USPS/RESTful provides only new-style language files (i.e. lang.uspsr.php); that form of language-file was introduced in zc158.