Description
Preconditions and environment
Environment:
Magento Version: 2.4.7-p1
paypal/module-braintree-core: 4.6.1-p1
PHP Version: 8.3
When using the paypal/module-braintree-core module in Magento 2, an Undefined array key "gold" warning is triggered during checkout. This occurs in the getCreditButtonColor() method of the PayPal\Braintree\Gateway\Config\PayPal\Config class.
Code Example:
The issue is located in the getCreditButtonColor() method in the Config.php file:
public function getCreditButtonColor(string $area = self::BUTTON_AREA_CART, string $type = 'credit'): ?string
{
$value = $this->getButtonStyle($area, self::KEY_BUTTON_COLOR, $type);
$options = $this->creditColorSource->toRawValues();
return $options[$value]; // Causes the warning if $value is not a valid key
}
Proposed Fix:
Update the method to check if the key exists in the $options array before accessing it:
public function getCreditButtonColor(string $area = self::BUTTON_AREA_CART, string $type = 'credit'): ?string
{
$value = $this->getButtonStyle($area, self::KEY_BUTTON_COLOR, $type);
$options = $this->creditColorSource->toRawValues();
return isset($options[$value]) ? $options[$value] : null; // Safely handle missing keys
}
Steps to reproduce
Ensure that the paypal/module-braintree-core module is installed.
Configure button colors in the Magento admin panel for the Braintree payment method.
Proceed to checkout on the frontend.
Observe the error in the logs or directly on the checkout page.
Expected Behavior:
The method should gracefully handle cases where the specified color key (e.g., "gold") does not exist in the configuration, returning null or a default value instead of causing a warning.
Expected result
The method should gracefully handle cases where the specified color key (e.g., "gold") does not exist in the configuration, returning null or a default value instead of causing a warning.
Actual result
An Undefined array key "gold" warning is triggered, leading to a potential disruption in the checkout process and a cluttered error log.
Additional information
- The issue occurs when the configuration value for button colors contains keys not present in the $options array.
- Adding error handling for undefined array keys helps prevent similar issues in the future.
Release note
No response
Triage and priority
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.