## Filter: wpd_ai_payment_gateway_fee_meta_keys

Filter the list of order meta keys that Alpha Insights checks for payment gateway fees. This allows you to add support for custom payment gateways or modify which meta keys are checked.

### Description

Alpha Insights automatically detects payment gateway fees by checking specific order meta keys. This filter allows you to add custom meta keys for payment gateways that aren't supported by default, or remove keys that you don't want to track.

#### Location

File: `includes/classes/WPDAI_Order_Calculator.php`

Method: `WPDAI_Order_Calculator::calculate_payment_gateway_cost()`

Line: ~510

### Parameters

Parameter | Type | Description
--- | --- | ---
$meta_keys | array | Array of order meta keys to check for payment gateway fees (default: common gateway meta keys)

#### Return

Type: array

Description: Modified array of meta key strings

### Default Meta Keys

Alpha Insights checks these meta keys by default:

- _stripe_fee
- _paypal_fee
- PayPal Transaction Fee
- HitPay_fees
- _wcpay_transaction_fee

### Example Usage

### Add Custom Payment Gateway Meta Keys

    add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'add_custom_gateway_keys' );
    function add_custom_gateway_keys( $meta_keys ) {
        // Add meta keys for custom payment gateways
        $custom_keys = array(
            '_square_fee',
            '_authorize_net_fee',
            '_braintree_fee',
            '_custom_gateway_fee'
        );
        return array_merge( $meta_keys, $custom_keys );
    }

### Remove Specific Gateway Keys

    add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'remove_specific_gateway_keys' );
    function remove_specific_gateway_keys( $meta_keys ) {
        // Remove keys you don't want to track
        $keys_to_remove = array( 'PayPal Transaction Fee' );
        return array_diff( $meta_keys, $keys_to_remove );
    }

### Replace Entire List

    add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'replace_gateway_keys_list' );
    function replace_gateway_keys_list( $meta_keys ) {
        // Use only your specific gateway keys
        return array(
            '_stripe_fee',
            '_square_fee',
            '_custom_gateway_fee'
        );
    }

### Add Gateway Keys Based on Active Plugins

    add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'add_keys_for_active_gateways' );
    function add_keys_for_active_gateways( $meta_keys ) {
        // Check which payment gateways are active and add their keys
        if ( class_exists( 'WC_Square' ) ) {
            $meta_keys[] = '_square_fee';
        }

        if ( class_exists( 'WC_Authorize_Net' ) ) {
            $meta_keys[] = '_authorize_net_fee';
        }

        if ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'woocommerce-braintree/woocommerce-braintree.php' ) ) {
            $meta_keys[] = '_braintree_fee';
        }

        return $meta_keys;
    }

### Best Practices

- Use array_merge() to add keys without losing defaults
- Use array_diff() to remove specific keys
- Check your payment gateway documentation for the correct meta key names
- Test with actual orders to verify keys are being detected
- Meta keys are case-sensitive - use exact names
- Some gateways store fees as negative values, which Alpha Insights handles automatically

### Finding Your Gateway's Meta Key

To find the meta key your payment gateway uses:

1. Create a test order with the payment gateway
2. Go to WooCommerce → Orders → Edit the order
3. Scroll down to "Order Meta" section
4. Look for keys containing "fee" or the gateway name
5. Common patterns: `_{gateway}_fee`, `{Gateway} Fee`

### Important Notes

- Meta keys are case-sensitive
- Alpha Insights checks all keys in the array for each order
- If multiple keys exist, the first non-zero value found is used
- Fees stored as negative values are automatically converted to positive costs
- This only affects fee detection, not fee calculation
- Changes apply to new calculations, not historical data

### Related Filters

- wpd_ai_order_payment_gateway_cost_default_value - Filter the default payment gateway cost value
- wpd_ai_calculate_cost_profit_by_order - Filter complete profit calculation results

### Related Classes

- `WPDAI_Order_Calculator` - Order profit calculation

### Debugging

    add_filter( 'wpd_ai_payment_gateway_fee_meta_keys', 'debug_gateway_meta_keys', 999 );
    function debug_gateway_meta_keys( $meta_keys ) {
        error_log( 'Alpha Insights Payment Gateway Meta Keys: ' . implode( ', ', $meta_keys ) );
        return $meta_keys;
    }

To see what meta keys exist on an order:

    // Add to functions.php temporarily
    add_action( 'woocommerce_admin_order_data_after_order_details', 'debug_order_meta_keys' );
    function debug_order_meta_keys( $order ) {
        $all_meta = get_post_meta( $order->get_id() );
        error_log( 'Order #' . $order->get_id() . ' Meta Keys: ' . print_r( array_keys( $all_meta ), true ) );
    }