'Session expired' ) ); } global $wpdb; $user_id = get_current_user_id(); $course_id = intval( $_SESSION['aimslms_checkout']['course_id'] ); $price = (float) get_post_meta( $course_id, '_aimslms_price', true ); $currency = aimslms_get_currency_code(); if ( $price <= 0 ) { wp_send_json_error( array( 'message' => 'Invalid price' ) ); } /* CREATE LOCAL ORDER */ $order_id = $this->aimslms_insert_order( array( 'user_id' => $user_id, 'course_id' => $course_id, 'order_total' => $price, 'defult_course_price' => $price, 'currency' => $currency, 'payment_method' => 'paypal', 'payment_method_title' => 'PayPal', 'order_status' => 'pending', ) ); // CREATE PAYMENT ROW. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id, 'user_id' => $user_id, 'course_id' => $course_id, 'amount' => $price, 'defult_course_price' => $price, 'currency' => $currency, 'payment_gateway' => 'paypal', 'status' => 'pending', 'transaction_date' => current_time( 'mysql' ), ) ); /* CREATE PAYPAL ORDER */ $paypal = AIMSLMS_Gateway_PayPal::create_order( $price, $order_id ); if ( ! empty( $paypal['error'] ) ) { wp_send_json_error( array( 'message' => $paypal['message'], ) ); } if ( ! empty( $paypal ) ) { $paypal_order_id = $paypal['id'] ?? ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'paypal_order_id' => $paypal_order_id, ), array( 'order_id' => $order_id ) ); foreach ( $paypal['links'] as $link ) { if ( in_array( $link['rel'], array( 'approve', 'payer-action' ), true ) ) { wp_send_json_success( array( 'approval_url' => esc_url_raw( $link['href'] ), 'order_id' => $order_id, ) ); } } } wp_send_json_error( array( 'message' => 'Approval link not found' ) ); } /** * Handles the redirect back from PayPal after a successful payment approval. */ public function aimslms_handle_paypal_return() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- External PayPal redirect, no nonce available. if ( empty( $_GET['aimslms_action'] ) || 'paypal_success' !== $_GET['aimslms_action'] ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- External PayPal redirect, no nonce available. if ( empty( $_GET['token'] ) ) { wp_die( esc_html__( 'Invalid PayPal response.', 'aims-infosoft-lms' ), '', array( 'response' => 400 ) ); } global $wpdb; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- External PayPal redirect, no nonce available. $paypal_order_id = sanitize_text_field( wp_unslash( $_GET['token'] ) ); $capture = AIMSLMS_Gateway_PayPal::capture_order( $paypal_order_id ); $capture_data = $capture['purchase_units'][0]['payments']['captures'][0] ?? array(); $method = $capture['payment_source'] ?? array(); $payment_method = 'unknown'; $payment_brand = null; if ( isset( $method['paypal'] ) ) { $payment_method = 'wallet'; $payment_brand = 'paypal'; } if ( isset( $method['card'] ) ) { $payment_method = 'card'; $payment_brand = strtolower( $method['card']['brand'] ?? '' ); } if ( isset( $method['bank'] ) ) { $payment_method = 'bank'; } $status = strtolower( $capture_data['status'] ?? 'failed' ); $order_id = intval( $capture_data['custom_id'] ) ?? ''; if ( ! $order_id ) { wp_die( esc_html__( 'Order ID missing from PayPal response.', 'aims-infosoft-lms' ), '', array( 'response' => 400 ) ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_orders WHERE id=%d", $order_id ) ); if ( ! $order ) { wp_die( esc_html__( 'Order not found.', 'aims-infosoft-lms' ), '', array( 'response' => 404 ) ); } $txn_id = ''; if ( ! empty( $capture_data['id'] ) ) { $txn_id = $capture_data['id']; } // UPDATE PAYMENT. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'payment_method' => $payment_method, 'payment_brand' => $payment_brand, 'status' => $status, 'transaction_id' => $txn_id, 'raw_response' => maybe_serialize( $capture ), ), array( 'order_id' => $order_id ) ); // UPDATE ORDER. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'order_status' => $status, 'transaction_id' => $txn_id, ), array( 'id' => $order_id ) ); /* ENROLL ONLY IF COMPLETED */ if ( 'completed' === $status ) { AIMSLMS_Enrollment::enroll( $order->user_id, $order->course_id, $order->id, $txn_id ); AIMSLMS_Payment_Emails::send_success_email( $order->user_id, array( 'course' => get_the_title( $order->course_id ), 'amount' => aimslms_format_price( $order->order_total ), 'gateway' => 'PayPal', 'txn_id' => $txn_id, ) ); } unset( $_SESSION['aimslms_checkout'] ); wp_safe_redirect( site_url( '/aimslms-order-received/?order=' . $order_id ) ); exit; } /** * Processes a Stripe payment for the current checkout session, creating * the local order/payment records and a Stripe payment intent. */ public function aimslms_process_stripe() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); global $wpdb; $user_id = get_current_user_id(); $course_id = isset( $_SESSION['aimslms_checkout']['course_id'] ) ? absint( $_SESSION['aimslms_checkout']['course_id'] ) : 0; $price = (float) get_post_meta( $course_id, '_aimslms_price', true ); $currency = aimslms_get_currency_code(); $token = isset( $_POST['stripe_token'] ) ? sanitize_text_field( wp_unslash( $_POST['stripe_token'] ) ) : ''; $payment_brand = ''; $payment_method = 'card'; if ( ! $course_id || $price <= 0 ) { wp_send_json_error( array( 'message' => 'Invalid request' ) ); } /* CREATE ORDER */ $order_id = $this->aimslms_insert_order( array( 'user_id' => $user_id, 'course_id' => $course_id, 'order_total' => $price, 'defult_course_price' => $price, 'currency' => $currency, 'payment_method' => $payment_method, 'payment_method_title' => 'Stripe', 'order_status' => 'pending', ) ); /* CREATE STRIPE PAYMENT INTENT */ $intent = AIMSLMS_Gateway_Stripe::create_payment_intent( $price, $currency, 'Course Purchase: ' . get_the_title( $course_id ), $order_id ); // CREATE PAYMENT. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id, 'user_id' => $user_id, 'course_id' => $course_id, 'amount' => $price, 'defult_course_price' => $price, 'currency' => $currency, 'payment_method' => $payment_method, 'payment_gateway' => 'stripe', 'payment_brand' => $payment_brand, 'stripe_payment_intent_id' => $intent->id, 'status' => 'pending', 'transaction_date' => current_time( 'mysql' ), ) ); wp_send_json_success( array( 'client_secret' => $intent->client_secret, 'order_id' => $order_id, ) ); } /** * Finalizes a Stripe payment intent after client-side confirmation, * marking the order/payment completed and enrolling the user on success. */ public function aimslms_finalize_stripe_payment() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); global $wpdb; $order_id = isset( $_POST['order_id'] ) ? intval( $_POST['order_id'] ) : 0; $intent_id = isset( $_POST['payment_intent_id'] ) ? sanitize_text_field( wp_unslash( $_POST['payment_intent_id'] ) ) : ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $payment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_payment WHERE order_id = %d AND payment_gateway = 'stripe'", $order_id ) ); if ( ! $payment ) { wp_send_json_error( array( 'message' => 'Payment not found' ) ); } if ( ! $this->aimslms_current_user_owns_order( $payment->user_id ) ) { wp_send_json_error( array( 'message' => 'Permission denied' ) ); } try { $intent = AIMSLMS_Gateway_Stripe::retrieve_payment_intent( $intent_id ); $intent_order_id = isset( $intent->metadata->order_id ) ? (string) $intent->metadata->order_id : ''; if ( '' === $intent_order_id || $intent_order_id !== (string) $order_id ) { wp_send_json_error( array( 'message' => 'Payment intent does not match this order' ) ); } if ( 'succeeded' === $intent->status ) { $charge_id = $intent->latest_charge; $charge = AIMSLMS_Gateway_Stripe::retrieve_payment_charge( $charge_id ); $payment_method = $charge->payment_method_details->type ?? 'card'; $payment_brand = $charge->payment_method_details->card->brand ?? ''; $last4 = $charge->payment_method_details->card->last4 ?? ''; // UPDATE PAYMENT. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'payment_method' => $payment_method, 'payment_brand' => $payment_brand, 'status' => 'completed', 'transaction_id' => $charge_id ?? '', 'raw_response' => maybe_serialize( $intent ), ), array( 'id' => $payment->id ) ); // UPDATE ORDER. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'payment_method' => $payment_method, 'order_status' => 'completed', 'transaction_id' => $charge_id ?? '', ), array( 'id' => $order_id ) ); /* ENROLL USER IMMEDIATELY */ AIMSLMS_Enrollment::enroll( $payment->user_id, $payment->course_id, $payment->order_id, $charge_id ); AIMSLMS_Payment_Emails::send_success_email( $payment->user_id, array( 'course' => get_the_title( $payment->course_id ), 'amount' => aimslms_format_price( $payment->amount ), 'gateway' => 'Stripe', 'txn_id' => $charge_id, ) ); unset( $_SESSION['aimslms_checkout'] ); wp_send_json_success( array( 'redirect' => site_url( '/aimslms-order-received/?order=' . $order_id ), ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'order_status' => 'failed' ), array( 'id' => $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'status' => 'failed' ), array( 'id' => $payment->id ) ); } wp_send_json_error( array( 'message' => 'Payment not completed' ) ); } catch ( Exception $e ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'order_status' => 'failed' ), array( 'id' => $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'status' => 'failed' ), array( 'id' => $payment->id ) ); wp_send_json_error( array( 'message' => $e->getMessage() ) ); } } /** * Retries a previously failed Stripe payment using a new card token. */ public function aimslms_retry_stripe() { check_ajax_referer( 'aimslms_auth_nonce', 'nonce' ); global $wpdb; $order_id = isset( $_POST['order_id'] ) ? intval( $_POST['order_id'] ) : 0; $token = isset( $_POST['stripe_token'] ) ? sanitize_text_field( wp_unslash( $_POST['stripe_token'] ) ) : ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_orders WHERE id=%d", $order_id ) ); if ( ! $order ) { wp_send_json_error( array( 'message' => 'Invalid order' ) ); } if ( ! $this->aimslms_current_user_owns_order( $order->user_id ) ) { wp_send_json_error( array( 'message' => 'Permission denied' ) ); } $charge = AIMSLMS_Gateway_Stripe::charge( $order->order_total, $token, 'Retry Order #' . $order_id ); if ( 'succeeded' === $charge->status ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'status' => 'completed', 'transaction_id' => $charge->id, ), array( 'order_id' => $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'order_status' => 'completed', ), array( 'id' => $order_id ) ); AIMSLMS_Enrollment::enroll( $order->user_id, $order->course_id, $order->id, $charge->id ); wp_send_json_success( array( 'message' => 'Payment successful' ) ); } wp_send_json_error( array( 'message' => 'Retry failed' ) ); } /** * Marks a local order/payment as failed after a client-reported failure. */ public function aimslms_failed_payment() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); global $wpdb; $order_id = isset( $_POST['order_id'] ) ? intval( $_POST['order_id'] ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_orders WHERE id=%d", $order_id ) ); if ( ! $order ) { wp_send_json_error( array( 'message' => 'Invalid order' ) ); } if ( ! $this->aimslms_current_user_owns_order( $order->user_id ) ) { wp_send_json_error( array( 'message' => 'Permission denied' ) ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_orders', array( 'order_status' => 'failed', ), array( 'id' => $order_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->update( $wpdb->prefix . 'aimslms_course_payment', array( 'status' => 'failed', ), array( 'order_id' => $order_id ) ); wp_send_json_success( array( 'message' => 'Payment failed' ) ); } /** * Checks whether the current user is allowed to act on an order owned by $owner_id. * * @param int $owner_id User ID that owns the order. * @return bool */ private function aimslms_current_user_owns_order( $owner_id ) { if ( ! is_user_logged_in() ) { return false; } if ( current_user_can( 'manage_options' ) ) { return true; } return get_current_user_id() === (int) $owner_id; } /** * Inserts a new order row, filling in defaults for missing fields. * * @param array $args Order field overrides merged over the defaults. * @return int Newly inserted order ID. */ private function aimslms_insert_order( $args = array() ) { global $wpdb; $defaults = array( 'user_id' => 0, 'course_id' => 0, 'order_total' => 0, 'defult_course_price' => 0, 'currency' => 'USD', 'payment_method' => '', 'payment_method_title' => '', 'created_via' => 'plugin', 'order_status' => 'pending', 'transaction_id' => '', ); $data = wp_parse_args( $args, $defaults ); $data['order_key'] = wp_generate_password( 20, false ); $data['created_at'] = current_time( 'mysql' ); $data['updated_at'] = current_time( 'mysql' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_orders', $data ); return $wpdb->insert_id; } /** * Saves the current user's billing address fields from the checkout form. */ public function aimslms_save_billing_address() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); if ( ! is_user_logged_in() ) { wp_send_json_error( array( 'message' => 'Login required' ) ); } $fields = array( '_aimslms_billing_phone' => 'billing_phone', '_aimslms_billing_country' => 'billing_country', '_aimslms_billing_state' => 'billing_state', '_aimslms_billing_city' => 'billing_city', '_aimslms_billing_postcode' => 'billing_postcode', '_aimslms_billing_address_1' => 'billing_address_1', '_aimslms_billing_address_2' => 'billing_address_2', '_aimslms_billing_company' => 'billing_company', '_aimslms_billing_vat' => 'billing_vat', ); foreach ( $fields as $meta_key => $post_key ) { if ( empty( $_POST[ $post_key ] ) ) { wp_send_json_error( array( 'message' => 'All billing fields are required.', ) ); } update_user_meta( get_current_user_id(), $meta_key, sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) ); } wp_send_json_success( array( 'message' => 'Billing saved' ) ); } /** * Tests connectivity/credentials for a payment gateway from the admin settings screen. */ public function aimslms_test_gateway_connection() { if ( ! isset( $_POST['nonce'] ) ) { wp_send_json_error( array( 'message' => 'Nonce missing' ) ); } check_ajax_referer( 'aimslms_test_gateway', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Permission denied', ) ); } $gateway = isset( $_POST['gateway'] ) ? sanitize_text_field( wp_unslash( $_POST['gateway'] ) ) : ''; $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : 'sandbox'; $client_id = isset( $_POST['client_id'] ) ? sanitize_text_field( wp_unslash( $_POST['client_id'] ) ) : ''; $secret = isset( $_POST['secret'] ) ? sanitize_text_field( wp_unslash( $_POST['secret'] ) ) : ''; switch ( $gateway ) { case 'paypal': $result = AIMSLMS_Gateway_PayPal::test_connection( $mode, $client_id, $secret ); break; case 'stripe': $result = AIMSLMS_Gateway_Stripe::test_connection( $mode, $client_id, $secret ); break; case 'razorpay': $result = AIMSLMS_Gateway_Razorpay::test_connection( $mode, $client_id, $secret ); break; default: wp_send_json_error( array( 'message' => 'Invalid gateway', 'mode' => 'sandbox', ) ); } if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), 'mode' => $result->get_error_data()['mode'] ?? 'sandbox', ) ); } wp_send_json_success( array( 'message' => 'Connection successful', 'mode' => $result['mode'], ) ); } /** * Creates a local pending order and a matching Razorpay order for the * current checkout session. */ public function aimslms_create_razorpay_order() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); global $wpdb; $course_id = isset( $_SESSION['aimslms_checkout']['course_id'] ) ? absint( $_SESSION['aimslms_checkout']['course_id'] ) : 0; $price = (float) get_post_meta( $course_id, '_aimslms_price', true ); $currency = aimslms_get_currency_code(); $user_id = get_current_user_id(); $inr_price = aimslms_convert_usd_to_inr( $price, $currency, 'INR' ); $amount_paise = (int) round( $inr_price * 100 ); if ( ! $course_id || $price <= 0 ) { wp_send_json_error( array( 'message' => 'Invalid request' ) ); } $order_id = $this->aimslms_insert_order( array( 'user_id' => $user_id, 'course_id' => $course_id, 'order_total' => $amount_paise, 'defult_course_price' => $price, 'currency' => 'INR', 'payment_method' => 'razorpay', 'payment_method_title' => 'Razorpay', 'order_status' => 'pending', ) ); $is_test = get_option( 'aimslms_razorpay_payment_mode' ) === '1'; $mode = $is_test ? 'sandbox' : 'live'; $razorpay_order = AIMSLMS_Gateway_Razorpay::create_order( $amount_paise, $currency, $order_id, $mode ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id, 'user_id' => $user_id, 'course_id' => $course_id, 'amount' => $amount_paise, 'defult_course_price' => $price, 'currency' => 'INR', 'payment_method' => 'razorpay', 'payment_gateway' => 'razorpay', 'status' => 'pending', 'razorpay_order_id' => $razorpay_order['id'], 'transaction_date' => current_time( 'mysql' ), ) ); unset( $_SESSION['aimslms_checkout'] ); wp_send_json_success( array( 'razorpay_order_id' => $razorpay_order['id'], 'order_id' => $order_id, 'amount' => $amount_paise, 'currency' => 'INR', 'redirect' => site_url( '/aimslms-order-received/?order=' . $order_id ), ) ); } /** * Handles the redirect back from PayPal after a cancelled checkout, * deleting the pending local order and payment records. */ public function aimslms_handle_paypal_cancel() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- External PayPal redirect, no nonce available. if ( empty( $_GET['aimslms_action'] ) || 'paypal_cancel' !== $_GET['aimslms_action'] ) { return; } global $wpdb; $user_id = get_current_user_id(); if ( ! $user_id ) { return; } // Get latest pending order for this user. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_orders WHERE user_id = %d AND order_status = 'pending' ORDER BY id DESC LIMIT 1", $user_id ) ); if ( $order ) { // Delete payment row first (child table). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order->id ), array( '%d' ) ); // Delete order row. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_orders', array( 'id' => $order->id ), array( '%d' ) ); } unset( $_SESSION['aimslms_checkout'] ); // Redirect back to clean checkout page. $checkout_page_id = get_option( 'aimslms_checkout_page_id' ); $checkout_url = get_permalink( $checkout_page_id ); wp_safe_redirect( $checkout_url . '?cancelled=1' ); exit; } /** * Deletes a local order and its associated payment row (used to clean up * abandoned PayPal/Stripe attempts). */ public function aimslms_delete_payment() { check_ajax_referer( 'aimslms_checkout_nonce', 'nonce' ); global $wpdb; $order_id = isset( $_POST['order_id'] ) ? intval( $_POST['order_id'] ) : 0; if ( ! $order_id ) { wp_send_json_error( array( 'message' => 'Invalid Order ID' ) ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aimslms_course_orders WHERE id=%d", $order_id ) ); if ( ! $order ) { wp_send_json_error( array( 'message' => 'Invalid Order ID' ) ); } if ( ! $this->aimslms_current_user_owns_order( $order->user_id ) ) { wp_send_json_error( array( 'message' => 'Permission denied' ) ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_orders', array( 'id' => $order_id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $wpdb->delete( $wpdb->prefix . 'aimslms_course_payment', array( 'order_id' => $order_id ), array( '%d' ) ); wp_send_json_success( array( 'message' => 'Payment failed and records removed.' ) ); } } new AIMSLMS_Payment_Ajax();