// --- 3. المعالج الرئيسي (Webhook Handler) ---
function ipmp_meps_webhook_handler( WP_REST_Request $request ) {
    global $wpdb;

    // استخراج البيانات
    $meps_data = $request->get_json_params();
    if ( empty( $meps_data ) || ! isset( $meps_data['cart_id'] ) ) {
        return new WP_REST_Response( array( 'message' => 'بيانات غير صالحة' ), 400 );
    }

    $cart_id    = sanitize_text_field( $meps_data['cart_id'] );
    $table_name = $wpdb->prefix . 'ipmp_payment_sessions';

    // البحث عن الجلسة
    $session = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE cart_id = %s LIMIT 1", $cart_id ) );

    if ( ! $session ) {
        return new WP_REST_Response( array( 'message' => 'Payment session not found' ), 404 );
    }

    $user_id = intval( $session->user_id );
    $response_status = isset( $meps_data['payment_result']['response_status'] ) ? strtoupper( sanitize_text_field( $meps_data['payment_result']['response_status'] ) ) : '';

    if ( $response_status === 'A' ) {
        
        // أ. تحديث الجلسة إلى SUCCESS
        $wpdb->update(
            $table_name,
            array( 'payment_status' => 'SUCCESS', 'updated_at' => current_time( 'mysql' ) ),
            array( 'cart_id' => $cart_id )
        );

        // ب. تفعيل اشتراك الطالب
        if ( $user_id > 0 ) {
            update_user_meta( $user_id, 'ipmp_subscription_status', 'active' );
            update_user_meta( $user_id, 'ipmp_is_paid', '1' );
            update_user_meta( $user_id, 'subscription_start', current_time( 'Y-m-d' ) );
            update_user_meta( $user_id, 'subscription_expires', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 60 days' ) ) );
        }

        // جـ. تحديث عداد الكوبون (إدراج منطق الكوبون هنا)
        if ( ! empty( $session->coupon_code ) ) {
            $coupon_posts = get_posts( array(
                'post_type'   => 'payment_coupon',
                'title'       => $session->coupon_code,
                'numberposts' => 1
            ));
            
            if ( ! empty( $coupon_posts ) ) {
                $coupon_id = $coupon_posts[0]->ID;
                $current_used = intval( get_post_meta( $coupon_id, '_coupon_used', true ) );
                update_post_meta( $coupon_id, '_coupon_used', $current_used + 1 );
                
                // تسجيل أن هذا المستخدم استخدم هذا الكوبون
                $email_key = '_used_by_' . md5( $session->customer_email );
                update_post_meta( $coupon_id, $email_key, '1' );
            }
        }

        return new WP_REST_Response( array( 'message' => 'Webhook Processed Successfully' ), 200 );

    } else {
        
        $wpdb->update(
            $table_name,
            array( 'payment_status' => 'FAILED', 'updated_at' => current_time( 'mysql' ) ),
            array( 'cart_id' => $cart_id )
        );

        return new WP_REST_Response( array( 'message' => 'Payment Failed or Rejected' ), 200 );
    }
}<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://intelligentpmp.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://intelligentpmp.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-posts-product-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-posts-portfolio-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-posts-wpr_templates-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-taxonomies-product_cat-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-taxonomies-portfolio-cat-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-taxonomies-portfolio-tag-1.xml</loc></sitemap><sitemap><loc>https://intelligentpmp.com/wp-sitemap-taxonomies-portfolio-filter-1.xml</loc></sitemap></sitemapindex>
