/** * Cain CDA — Server-Side Commission Fix * ─────────────────────────────────────── * WPCode → PHP Snippet → "CDA Server Commission Fix" * Run on: Everywhere * * Intercepts the CDA submit REST request BEFORE it hits the original * plugin handler, captures commission fields from the request body, * stores them in a global, then saves them to post meta and regenerates * the invoice after the post is created. No JavaScript required. */ // ── Step 1: Capture commission data from REST request body ──────────────── add_filter( 'rest_pre_dispatch', 'cain_capture_commission_request', 1, 3 ); function cain_capture_commission_request( $result, $server, $request ) { $route = $request->get_route(); // Only intercept the CDA submit endpoint if ( strpos($route, 'cda/submit') === false ) { return $result; } // Pull every possible commission field from the request body $body = $request->get_body_params(); if ( empty($body) ) { // Try JSON body $json = $request->get_json_params(); if ( ! empty($json) ) $body = $json; } if ( empty($body) ) { // Try raw body $raw = $request->get_body(); if ( $raw ) { $decoded = json_decode($raw, true); if ( is_array($decoded) ) $body = $decoded; } } // Store in global for use after post is created $GLOBALS['cain_cda_commission'] = [ 'gross_commission' => floatval( $body['grossCommission'] ?? $body['gross_commission'] ?? $body['commission_amt'] ?? 0 ), 'agent_split' => floatval( $body['agentSplit'] ?? $body['agent_split'] ?? $body['split_pct'] ?? 0 ), 'agent_commission' => floatval( $body['agentGrossCommission'] ?? $body['agent_commission'] ?? 0 ), 'broker_fee' => floatval( $body['brokerFee'] ?? $body['broker_fee'] ?? $body['broker_commission'] ?? 0 ), 'transaction_fee' => floatval( $body['transactionFee'] ?? $body['transaction_fee'] ?? 0 ), 'eo_fee' => floatval( $body['eoFee'] ?? $body['eo_fee'] ?? 0 ), 'other1' => floatval( $body['other1'] ?? $body['other_deduction_1'] ?? 0 ), 'other2' => floatval( $body['other2'] ?? $body['other_deduction_2'] ?? 0 ), 'net_disbursement' => floatval( $body['netDisbursement'] ?? $body['net_disbursement'] ?? 0 ), ]; // Calculate anything missing $c = &$GLOBALS['cain_cda_commission']; if ( ! $c['agent_commission'] && $c['gross_commission'] && $c['agent_split'] ) { $c['agent_commission'] = $c['gross_commission'] * ( $c['agent_split'] / 100 ); } if ( ! $c['net_disbursement'] && $c['agent_commission'] ) { $c['net_disbursement'] = $c['agent_commission'] - $c['broker_fee'] - $c['transaction_fee'] - $c['eo_fee'] - $c['other1'] - $c['other2']; } return $result; // Don't block the original handler } // ── Step 2: After post is saved, write commission meta + new invoice ─────── add_action( 'save_post_cain_cda', 'cain_save_commission_and_invoice', 99, 3 ); function cain_save_commission_and_invoice( $post_id, $post, $update ) { if ( $post->post_status !== 'publish' ) return; if ( wp_is_post_revision($post_id) ) return; if ( wp_is_post_autosave($post_id) ) return; // Get captured commission data $c = $GLOBALS['cain_cda_commission'] ?? []; // Also check if commission is already in post meta (from a previous save) $stored_gross = floatval( get_post_meta($post_id, '_commission_amt', true) ?: get_post_meta($post_id, '_gross_commission', true) ?: 0 ); // Merge: prefer captured data, fall back to stored $gross = $c['gross_commission'] ?: $stored_gross; $split = $c['agent_split'] ?: floatval(get_post_meta($post_id, '_split_pct', true) ?: get_post_meta($post_id, '_agent_split', true) ?: 0); $acomm = $c['agent_commission'] ?: floatval(get_post_meta($post_id, '_agent_commission', true) ?: 0); $broker = $c['broker_fee'] ?: floatval(get_post_meta($post_id, '_broker_commission', true) ?: get_post_meta($post_id, '_broker_fee', true) ?: 0); $tx = $c['transaction_fee'] ?: floatval(get_post_meta($post_id, '_transaction_fee', true) ?: 0); $eo = $c['eo_fee'] ?: floatval(get_post_meta($post_id, '_eo_fee', true) ?: 0); $other1 = $c['other1'] ?: floatval(get_post_meta($post_id, '_other_deduction_1', true) ?: 0); $other2 = $c['other2'] ?: floatval(get_post_meta($post_id, '_other_deduction_2', true) ?: 0); $net = $c['net_disbursement'] ?: floatval(get_post_meta($post_id, '_net_disbursement', true) ?: 0); // Skip if no commission data at all if ( ! $gross && ! $acomm ) return; // Calculate missing values if ( ! $acomm && $gross && $split ) $acomm = $gross * ($split / 100); if ( ! $net && $acomm ) $net = $acomm - $broker - $tx - $eo - $other1 - $other2; // Save to post meta (correct keys) update_post_meta( $post_id, '_commission_amt', $gross ); update_post_meta( $post_id, '_gross_commission', $gross ); update_post_meta( $post_id, '_split_pct', $split ); update_post_meta( $post_id, '_agent_split', $split ); update_post_meta( $post_id, '_agent_commission', $acomm ); update_post_meta( $post_id, '_broker_commission', $broker ); update_post_meta( $post_id, '_broker_fee', $broker ); update_post_meta( $post_id, '_transaction_fee', $tx ); update_post_meta( $post_id, '_eo_fee', $eo ); update_post_meta( $post_id, '_other_deduction_1', $other1 ); update_post_meta( $post_id, '_other_deduction_2', $other2 ); update_post_meta( $post_id, '_net_disbursement', $net ); // ── Pull all other meta for invoice ─────────────────────────────────── $m = fn($k) => get_post_meta($post_id, '_' . $k, true) ?: ''; $agent_name = $m('agent_name') ?: get_the_title($post_id); $agent_email = $m('agent_email'); $agent_phone = $m('agent_phone'); $trec = $m('trec_license'); $tx_type = $m('transaction_type') ?: 'Sale'; $address = $m('property_address'); $city = $m('city'); $state = $m('state'); $zip = $m('zip'); $csz = trim("$city, $state $zip", ', '); $close_date = $m('closing_date'); $title_co = $m('title_company'); $title_ct = $m('title_contact'); $inv_co = $m('invoice_company') ?: $m('title_company'); $inv_email = $m('invoice_email'); $notes = $m('notes'); // Invoice number $inv_num = get_post_meta($post_id, '_invoice_number', true); if ( ! $inv_num ) { $inv_num = 'CDA-' . date('Ymd', strtotime($post->post_date)) . '-' . $post_id; update_post_meta($post_id, '_invoice_number', $inv_num); } $$ = fn($n) => $n > 0 ? '$' . number_format($n, 2) : '—'; $p = fn($n) => $n > 0 ? number_format($n, 1) . '%' : '—'; $t = fn($v) => $v ? esc_html($v) : '—'; // ── Build invoice HTML ───────────────────────────────────────────────── $html = ' Commission Invoice ' . esc_html($inv_num) . '
& COMPANY REAL ESTATE LLC

COMMISSION INVOICE

' . esc_html($inv_num) . '
' . date('F j, Y') . '
PENDING BROKER APPROVAL
AGENT INFORMATION
' . $t($agent_name) . '
' . $t($agent_email) . '
' . $t($agent_phone) . '
' . $t($trec) . '
PROPERTY & TRANSACTION
' . $t($address) . '
' . $t($csz) . '
' . esc_html($tx_type) . '
' . $t($close_date) . '
' . $t($title_co) . '
' . $t($title_ct) . '
COMMISSION BREAKDOWN
Gross Commission' . $$($gross) . '
Agent Split' . $p($split) . '
Agent Gross Commission' . $$($acomm) . '
' . ($broker > 0 ? '
Broker Fee- ' . $$($broker) . '
' : '') . ' ' . ($tx > 0 ? '
Transaction Fee- ' . $$($tx) . '
' : '') . ' ' . ($eo > 0 ? '
E&O Fee- ' . $$($eo) . '
' : '') . ' ' . ($other1 > 0 ? '
Other Deduction 1- ' . $$($other1) . '
' : '') . ' ' . ($other2 > 0 ? '
Other Deduction 2- ' . $$($other2) . '
' : '') . '
NET DISBURSEMENT' . $$($net) . '
' . ($inv_co || $inv_email ? '
INVOICE / BILL TO
' . $t($inv_co) . '
' . $t($inv_email) . '
' : '') . ' ' . ($notes ? '
NOTES
' . nl2br(esc_html($notes)) . '
' : '') . '
'; // Always overwrite update_post_meta($post_id, '_invoice_html', $html); // ── Emails ───────────────────────────────────────────────────────────── $headers = ['Content-Type: text/html; charset=UTF-8']; wp_mail(get_option('admin_email'), 'New CDA — ' . $agent_name . ' | ' . $address, $html, $headers); if ($agent_email && is_email($agent_email)) { wp_mail($agent_email, 'Your CDA Has Been Received — ' . $inv_num, $html, $headers); } } // ── Download handler — priority 1 ───────────────────────────────────────── remove_action('wp_ajax_cain_download_invoice', 'cain_download_invoice_handler'); add_action('wp_ajax_cain_download_invoice', 'cain_server_download', 1); add_action('wp_ajax_nopriv_cain_download_invoice', 'cain_server_download', 1); function cain_server_download() { $nonce = sanitize_text_field($_REQUEST['nonce'] ?? ''); $post_id = absint($_REQUEST['id'] ?? 0); if (!$post_id) wp_die('Invalid request.'); $ok = wp_verify_nonce($nonce, 'cain_invoice_' . $post_id) || wp_verify_nonce($nonce, 'cain_download_invoice') || (is_user_logged_in() && current_user_can('manage_options')); if (!$ok) wp_die('Link expired. Reload the CDA list and try again.'); $html = get_post_meta($post_id, '_invoice_html', true); if (empty($html)) { $p = get_post($post_id); if ($p) { cain_save_commission_and_invoice($post_id, $p, true); $html = get_post_meta($post_id, '_invoice_html', true); } } if (empty($html)) wp_die('Invoice not found.'); $inv_num = get_post_meta($post_id, '_invoice_number', true) ?: 'CDA-' . $post_id; header('Content-Type: text/html; charset=UTF-8'); header('Content-Disposition: attachment; filename="Invoice-' . sanitize_file_name($inv_num) . '.html"'); header('Cache-Control: no-cache'); echo $html; exit; } // Fresh nonce in REST response add_filter('rest_prepare_cain_cda', function($r, $p) { $d = $r->get_data(); $d['invoice_url'] = admin_url('admin-ajax.php') . '?action=cain_download_invoice&id=' . $p->ID . '&nonce=' . wp_create_nonce('cain_invoice_' . $p->ID); $d['invoice_number'] = get_post_meta($p->ID, '_invoice_number', true) ?: 'CDA-' . date('Ymd', strtotime($p->post_date)) . '-' . $p->ID; $r->set_data($d); return $r; }, 20, 2);

Form Submitted Successfully! We will contact you shortly.

Reverse Pre-Qual Analysis

Cain & Company Real Estate LLC | NMLS: 1980282

Ready to meet?

Ready to turn your real estate dreams into reality? Let’s connect and discuss your goals. Fill out the form below, and let the journey to your dream home or property investment begin!