HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dreamsrent-wp-demo/wp-content/themes/dreamsrent/inc/paypal-vendor-payment.php
<?php

add_filter('woocommerce_paypal_args', 'redirect_payment_to_vendor', 10, 2);

function redirect_payment_to_vendor($paypal_args, $order) {
    $custom = $paypal_args['custom'] ?? '';
    
    // Decode if it's a JSON string
    if (is_string($custom) && str_starts_with($custom, '{')) {
        $custom_data = json_decode($custom, true);
        if (json_last_error() === JSON_ERROR_NONE && isset($custom_data['order_key'])) {
            $order_key = $custom_data['order_key'];
        } else {
            error_log('❌ Failed to decode custom field: ' . $custom);
            return $paypal_args;
        }
    } else {
        $order_key = $custom;
    }

    $order_id = wc_get_order_id_by_order_key($order_key);
    if (!$order_id) {
        error_log('❌ Could not resolve order ID from order key: ' . $order_key);
        return $paypal_args;
    }

    $order = wc_get_order($order_id);
    if (!$order) {
        error_log('❌ Could not load order from ID: ' . $order_id);
        return $paypal_args;
    }

    foreach ($order->get_items() as $item) {
        $product_id = $item->get_product_id();
        $vendor_id = get_post_field('post_author', $product_id);
        $vendor_email = get_user_meta($vendor_id, 'vendor_paypal_email', true);

        error_log("🔍 Vendor ID: $vendor_id");
        error_log("🔍 Vendor PayPal Email: $vendor_email");

        if (is_email($vendor_email)) {
            $paypal_args['business'] = $vendor_email;
            error_log("✅ PayPal payment will be sent to: $vendor_email");
            break;
        } else {
            error_log("❌ Invalid or missing vendor PayPal email: $vendor_email");
        }
    }

    return $paypal_args;
}