Magento 2.2.2启用FPC时Paygate支付网关重定向触发致命错误
Let's break down what's happening here: when Full Page Cache (FPC) is enabled, your Paygate module tries to call getCountryId() on a null object at line 328 of Paygate/Model/Paygate.php. This almost always means the code relies on dynamic user-specific data (like a customer's shipping address or quote info) that gets stripped out or isn't properly loaded when FPC caches the page. Here's how to fix it step by step:
1. Add Null Checks to the Paygate Code
First, patch the immediate error by adding safeguards around the problematic line. Open /home/vapour/public_html/app/code/Paygate/Paygate/Model/Paygate.php and look at line 328—it’s probably something like:
$countryId = $address->getCountryId();
Modify it to include null checks for both the address and quote objects (since the address comes from a quote):
// Ensure we're getting a valid quote first $quote = $this->getQuote(); // Or however the module retrieves the quote if ($quote) { $address = $quote->getShippingAddress() ?: $quote->getBillingAddress(); if ($address && $address->getCountryId()) { $countryId = $address->getCountryId(); // Proceed with your existing logic } else { // Handle missing address with user-friendly feedback throw new \Magento\Framework\Exception\LocalizedException( __('Unable to retrieve your shipping address. Please check your checkout details and try again.') ); } } else { throw new \Magento\Framework\Exception\LocalizedException( __('No active checkout session found. Please restart your checkout process.') ); }
This stops the fatal error and gives customers clear guidance if something goes wrong.
2. Exclude Paygate Routes from FPC
Payment pages should never be cached—they depend on dynamic user data. Tell Magento to skip caching Paygate-related URLs:
- Go to Stores > Configuration > Advanced > System > Full Page Cache
- Under Cached Pages, find the Exclude URLs field
- Add your Paygate route(s) here. Common examples include:
(Adjust paths to match your module's actual routing)/paygate/* /checkout/onepage/paygate/* - Save the configuration and flush the cache.
Alternatively, add this exclusion directly in the Paygate module's etc/cache.xml file (create it if it doesn't exist):
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd"> <type name="full_page"> <exclude> <path>/paygate/*</path> <path>/checkout/onepage/paygate/*</path> </exclude> </type> </config>
3. Fetch Quote Data from the Session, Not Cache
Many older third-party modules don’t handle FPC correctly because they pull data from cached page content instead of the user’s session. Update the Paygate module to retrieve the quote directly from the checkout session:
- Inject
\Magento\Checkout\Model\Sessioninto the Paygate model's constructor:protected $checkoutSession; public function __construct( // ... other dependencies ... \Magento\Checkout\Model\Session $checkoutSession, // ... ) { $this->checkoutSession = $checkoutSession; // ... parent constructor or other setup ... } - Then, whenever you need the quote, use:
$quote = $this->checkoutSession->getQuote();
This ensures you’re getting the active user’s quote, not a cached version from another session.
4. Clean Cache and Test Thoroughly
After making these changes, run the following commands to clear all caches:
bin/magento cache:clean bin/magento cache:flush
Test the payment flow multiple times with FPC enabled to confirm:
- No fatal errors occur when redirecting to Paygate
- The site stays fast (FPC remains active for non-payment pages)
- The payment process completes successfully
内容的提问来源于stack exchange,提问作者Andries Botha

