Change Order Status Depending On Payment_Id

Hi,

I am trying to change a order status depending on a payment_id. But i can't get the payment_id inside a controller. When i am testing i can get the order id, but the payment_id returns returns 0.

What am i doing wrong? See example below.

Thanks in advange

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ($mode == 'generate') {
    $order_id = empty($_REQUEST['order_id']) ? 0 : $_REQUEST['order_id'];
           
    $payment_id = db_get_field("SELECT payment_id FROM ?:orders WHERE order_id =?i", $_REQUEST['order_id']);
    
    $order_info = Tygh::$app['view']->getTemplateVars('order_info');
            
    $check_payment = array(12);
    
     if (in_array($order_info['payment_id'], $check_payment )) {
    fn_change_order_status($order_id, 'Q');
       fn_print_die($order_id, $check_payment ,$payment_id);
     } else {
     fn_change_order_status($order_id, 'C');

fn_print_die($order_id, $check_payment ,$payment_id);
}

    return array(CONTROLLER_STATUS_OK, "orders.details?order_id=$order_id".$suffix);
}

}

Not sure what does the "generate" mode mean, but try the following code

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ($mode == 'generate') {
        $order_id = empty($_REQUEST['order_id']) ? 0 : $_REQUEST['order_id'];
    $order_info = fn_get_order_info($order_id);;
      
    if (!empty($order_info['payment_id'])) {
        $check_payment = array(12);
    
        if (in_array($order_info['payment_id'], $check_payment )) {
            fn_change_order_status($order_id, 'Q');
            fn_print_die($order_id, $check_payment ,$payment_id);
        } else {
            fn_change_order_status($order_id, 'C');
            fn_print_die($order_id, $check_payment ,$payment_id);
        }
    }

    return array(CONTROLLER_STATUS_OK, "orders.details?order_id=$order_id".$suffix);
}

}

Each payment method passes the next order status in the payment method code ($pp_response['order_status']) so I'd suggest you edit the payment method if you want to alter it's behavior.