magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Undefined Array Key "gold" in PayPal Braintree Config

Open steveberrill opened this issue 1 year ago • 11 comments

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

  • [X] 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”.

steveberrill avatar Aug 07 '24 18:08 steveberrill

Hi @steveberrill. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Aug 07 '24 18:08 m2-assistant[bot]

I had this exact same issue upon upgrade from 2.4.6-p6 to 2.4.7-p1, in sandbox testing on development environment it did not occur but in my case it only occurred in production (... customers were unable to place orders regardless if they selected the PayPal payment method or not, that was an interesting hour I'm never getting back!) The workaround that appears to have resolved it was Stores > Configuration > Sales > Payment Methods > Additional Payment Solutions > PayPal Braintree > Configure > PayPal through Braintree > Styling > Checkout Page > PayPal Button and changing the button configuration, then Pay Later Messaging > Reset to Recommended Defaults. Finally, Save Config (and flushing magento cache). That resolved the error, and I was able to return to the button configuration and restore previous settings (including "Gold" color selection) without issue.

The key to reproducing this error might be the upgrade from 2.4.6 to 2.4.7 where PayPal credit was configured and enabled in 2.4.6 prior to the upgrade.

n2diving-dgx avatar Aug 12 '24 12:08 n2diving-dgx

Hi @engcom-Delta. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Aug 13 '24 03:08 m2-assistant[bot]

Hi @steveberrill,

Thanks for your reporting and collaboration. We have verified the issue in magento 2.4.7-p1, but we are unable to reproduce the issue. Kindly refer the screenshots.

Steps to reproduce

  1. Install magento 2.4.7-p1 image

  2. Enable & configure paypal/module-braintree-core: 4.6.1-p1

  3. Set PayPal Button color to Gold and apply image

  4. Access frontend

  5. Add an item to cart and navigate to checkout

  6. Observe the Paypal button in gold. image

  7. No Error displays in checkout related to button.

  8. Change the Paypal button color again e.g. Black and apply image

  9. Access frontend again and navigate to checkout. Button is displaying as expected with no error related to button. image

Please let us know if any other steps are needed to be performed. Thanks.

engcom-Delta avatar Aug 14 '24 09:08 engcom-Delta

Again... The key to reproducing this error might be the upgrade from 2.4.6 to 2.4.7 where PayPal credit was configured and enabled in 2.4.6 prior to the upgrade. The attempt to reproduce did not perform the upgrade first

n2diving-dgx avatar Aug 15 '24 07:08 n2diving-dgx

Yes as I have upgraded from 2.4.5-p6 to 2.4.7-p1 on 3 installations and produced the same error.

steveberrill avatar Aug 15 '24 10:08 steveberrill

Hi @steveberrill,

Thanks for your reporting and collaboration. We have verified the issue and it is reproducible. Hence, Confirming the issue.

Steps to Reproduce

  1. Install magento 2.4.6-p5 image

  2. Configure Braintree Payments - PayPal

  3. Configure PayPal button color to gold image

  4. Upgrade 2.4.6-p5 to 2.4.7-p2 image

  5. Access frontend and navigate to checkout.

  6. Observe checkout is blank and error displays in log. image image

Thanks.

engcom-Delta avatar Aug 28 '24 09:08 engcom-Delta

Unfortunately, not enough information was provided to create a Jira ticket. Please make sure you added the following label(s): Reproduced on 2.4.x, ^Area:.*

Once all required labels are present, please add Issue: Confirmed label again.

github-jira-sync-bot avatar Aug 28 '24 09:08 github-jira-sync-bot

Unfortunately, not enough information was provided to create a Jira ticket. Please make sure you added the following label(s): Reproduced on 2.4.x, ^Area:.*

Once all required labels are present, please add Issue: Confirmed label again.

github-jira-sync-bot avatar Aug 28 '24 11:08 github-jira-sync-bot

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-12849 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Aug 28 '24 11:08 github-jira-sync-bot

:white_check_mark: Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

m2-assistant[bot] avatar Aug 28 '24 11:08 m2-assistant[bot]