braintree / braintree_php

Braintree PHP library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
547 stars 224 forks source link

foreach() argument must be of type array|object, null given when using $braintreeErrorResponse->__toString() to record error response as string #340

Closed akarmar closed 4 months ago

akarmar commented 4 months ago

General information

Issue description

foreach() argument must be of type array|object, null given is reported when CreditCardVerification with RiskData object/properties being null or Modification with properties being null. Our logic tries to get string response using $braintreeErrorResponse->__toString() method call in case of BrainTree error is seen while validating/payment through braintree api

    /*
     * Turn all attributes into a string
     *
     * @param array $attributes to be turned into a string
     *
     * @return string|false
     */
    public static function attributesToString($attributes)
    {
        $printableAttribs = [];
        // error is here that too it is nested
        --> foreach ($attributes as $key => $value) { <---
            if (is_array($value)) {
                --> // as this is called recursively here <---
                $pAttrib = self::attributesToString($value); 
            } elseif ($value instanceof DateTime) {
                $pAttrib = $value->format(DateTime::RFC850);
            } else {
                $pAttrib = $value;
            }
            $printableAttribs[$key] = sprintf('%s', $pAttrib);
        }
        return self::implodeAssociativeArray($printableAttribs);
    }
akarmar commented 4 months ago

Looks like the issue is already has been fixed in 6.16 version, will close the issue.