Failed transaction throwing an error on line 305

I declare $order = new WC_Order($order_id) before if($transaction['status'] == 'Approved') to solve the problem