// Add to your register_routes() function register_rest_route( 'hemjey-pos/v1', '/onboard-student', [ 'methods' => 'POST', 'callback' => [ $this, 'onboard_student' ], 'permission_callback' => [ $this, 'verify_pos_permissions' ], ] ); /** * Fast Onboarding: Creates a WP user with Phone Number as Username */ public function onboard_student( WP_REST_Request $request ) { $params =$request->get_json_params(); $phone = sanitize_text_field($params['phone'] ); $name = sanitize_text_field($params['name'] ); $email = sanitize_email($params['email'] ); $ref = sanitize_text_field($params['referrer_phone'] ); if ( username_exists( $phone ) ) { return new WP_Error( 'exists', 'Student with this phone number already exists.', ['status' => 400] ); } $user_id = wp_insert_user([ 'user_login' => $phone, 'user_pass' => wp_generate_password( 12, false ), 'user_email' => $email, 'display_name' => $name, 'role' => 'customer' ]); if ( is_wp_error( $user_id ) ) return$user_id; update_user_meta( $user_id, 'billing_phone',$phone ); update_user_meta( $user_id, '_hemjey_wallet_balance', 0 ); // Link referral if provided if ( ! empty( $ref ) ) { $referrer = get_users(['meta_key' => 'billing_phone', 'meta_value' =>$ref, 'number' => 1]); if ( ! empty( $referrer ) ) { update_user_meta( $user_id, '_referred_by_id',$referrer[0]->ID ); } } return rest_ensure_response([ 'status' => 'success', 'user_id' => $user_id, 'message' => 'Student onboarded successfully.' ]); } /** * Updated Core Transaction Logic (Call this inside your process_transaction method) * Handles the 3% cashback and 10% / 2% referral commissions automatically. */ private function process_loyalty_and_commissions( $order, $customer_id,$wallet_deducted ) { $order_total =$order->get_total(); // 1. Give 3% Cashback to the Buyer $cashback =$order_total * 0.03; $this->process_wallet_credits( $customer_id,$wallet_deducted, $cashback,$order->get_id() ); $order->add_order_note( "Added ₦{$cashback} (3% cashback) to wallet." ); // 2. Handle Referral Commissions $referrer_id = get_user_meta($customer_id, '_referred_by_id', true ); if ( $referrer_id ) { // Check if this is the student's FIRST order $previous_orders = wc_get_orders([ 'customer_id' => $customer_id, 'status' => 'completed', 'exclude' => [ $order->get_id() ] ]); if ( empty( $previous_orders ) ) { // First order: 10% commission $commission =$order_total * 0.10; $note = "10% First-time referral commission"; } else { // Subsequent orders: 2% commission $commission =$order_total * 0.02; $note = "2% Recurring referral commission"; } $this->process_wallet_credits( $referrer_id, 0,$commission, $order->get_id() );$order->add_order_note( "Referrer (ID: {$referrer_id}) earned ₦{$commission} - {$note}." ); } return $cashback; // Return earned amount to display on POS } /** * Send Custom Email Receipt with Attendant Name and Balances */ private function send_student_receipt( $order, $customer_id,$attendant_name, $wallet_deducted,$cashback_earned ) { $user = get_userdata($customer_id ); $new_balance = get_user_meta($customer_id, '_hemjey_wallet_balance', true ); $to =$user->user_email; $subject = "Your Receipt - Hemjey Tech Campus Cafe"; $message = "Hello {$user->display_name},\n\n"; $message .= "Thank you for visiting Hemjey Tech Cafe today. You were attended to by: {$attendant_name}.\n\n"; $message .= "Transaction Total: ₦" . $order->get_total() . "\n"; if ( $wallet_deducted > 0 ) {$message .= "Paid via Wallet: ₦{$wallet_deducted}\n"; } $message .= "Cashback Earned (3%): ₦{$cashback_earned}\n"; $message .= "Current Wallet Balance: ₦{$new_balance}\n\n"; $message .= "Note: Your wallet balance can be used at any Hemjey Tech branch on campus!\n"; wp_mail( $to, $subject,$message ); }