• Resolved zethodderskov

    (@zethodderskov)


    Getting the error:

    
    
    [09-Jun-2021 22:26:48 UTC] PHP Fatal error:  Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, stdClass given in /public_html/wp-content/plugins/dibs-easy-for-woocommerce/classes/class-dibs-post-checkout.php:80
    Stack trace:
    #0 /public_html/wp-includes/class-wp-hook.php(294): DIBS_Post_Checkout->dibs_order_completed()
    #1 /public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters()
    #2 /public_html/wp-includes/plugin.php(484): WP_Hook->do_action()

    It’s an object and not an array, so these lines (starting on line 79):

    
    `// Error handling.
    if ( null !== $request ) {
    	if ( array_key_exists( 'chargeId', $request ) ) { // Payment success.
    		// Translators: Nets Charge ID.
    		$wc_order->add_order_note( sprintf( __( 'Payment charged in Nets Easy with charge ID %s', 'dibs-easy-for-woocommerce' ), $request->chargeId ) ); // phpcs:ignore
    
    		update_post_meta( $order_id, '_dibs_charge_id', $request->chargeId ); // phpcs:ignore
    	} elseif ( array_key_exists( 'errors', $request ) ) { // Response with errors.
    		if ( array_key_exists( 'instance', $request->errors ) ) { // If return is empty.
    			$message = $request->errors->instance[0];
    		} elseif ( array_key_exists( 'amount', $request->errors ) ) { // If total amount is wrong.
    			$message = $request->errors->amount[0];
    		} else {
    			$message = wp_json_encode( $request->errors );
    		}
    
    		$this->charge_failed( $wc_order, true, $message );
    
    	} elseif ( array_key_exists( 'code', $request ) && '1001' === $request->code ) { // Set order as completed if order has already been charged.
    		// @todo - set status to on hold if WC order total and Nets order total don't match.
    		$wc_order->add_order_note( sprintf( __( 'Nets error message: %s', 'dibs-easy-for-woocommerce' ), $request->message ) ); // phpcs:ignore
    	} else {
    		$this->charge_failed( $wc_order );
    	}
    } else {
    	$this->charge_failed( $wc_order );
    }
    

    … has to be replaced with this:

    
    // Error handling.
    if ( null !== $request ) {
    	if ( isset( $request->chargeId ) ) { // Payment success.
    		// Translators: Nets Charge ID.
    		$wc_order->add_order_note( sprintf( __( 'Payment charged in Nets Easy with charge ID %s', 'dibs-easy-for-woocommerce' ), $request->chargeId ) ); // phpcs:ignore
    
    		update_post_meta( $order_id, '_dibs_charge_id', $request->chargeId ); // phpcs:ignore
    	} elseif ( isset( $request->errors ) ) { // Response with errors.
    		if ( array_key_exists( 'instance', $request->errors ) ) { // If return is empty.
    			$message = $request->errors->instance[0];
    		} elseif ( array_key_exists( 'amount', $request->errors ) ) { // If total amount is wrong.
    			$message = $request->errors->amount[0];
    		} else {
    			$message = wp_json_encode( $request->errors );
    		}
    
    		$this->charge_failed( $wc_order, true, $message );
    
    	} elseif ( isset( $request->code ) && '1001' === $request->code ) { // Set order as completed if order has already been charged.
    		// @todo - set status to on hold if WC order total and Nets order total don't match.
    		$wc_order->add_order_note( sprintf( __( 'Nets error message: %s', 'dibs-easy-for-woocommerce' ), $request->message ) ); // phpcs:ignore
    	} else {
    		$this->charge_failed( $wc_order );
    	}
    } else {
    	$this->charge_failed( $wc_order );
    }
    

    I’m not sure if $request->errors is an object or and array, so I’m not sure if they should be changed as well.

The topic ‘Array_key_exists on object in class-dibs-post-checkout.php:80’ is closed to new replies.